more work

This commit is contained in:
Andrew Davidson 2012-04-20 15:30:30 -07:00
parent 16af8ffb5c
commit d72fd47a35

133
brain.py
View file

@ -1,6 +1,6 @@
import os, sys import os, sys
from git import * from git import *
from bottle import route, run, request, template, static_file from bottle import route, run, request, template, static_file, redirect
from os.path import isdir from os.path import isdir
from string import lower, split from string import lower, split
from urllib import unquote from urllib import unquote
@ -11,15 +11,15 @@ import re
conf = {} conf = {}
#conf['private'] = '/srv/git/documents' #conf['repo'] = '/srv/git/documents'
conf['private'] = '/srv/git/repo' conf['repo'] = '/srv/git/repo'
conf['ext_bundles'] = ['.pages', '.sparsebundle'] conf['ext_bundles'] = ['.pages', '.sparsebundle']
conf['ext_render'] = [] conf['ext_render'] = []
conf['ext_edit'] = ['.md','.txt','.rb','.py','.pl','.sh']
@route('/repo')
@route('/private') @route('/repo/')
@route('/private/') @route('/repo/<name:path>')
@route('/private/<name:path>')
def public(name=''): def public(name=''):
tree = [] tree = []
@ -27,7 +27,7 @@ def public(name=''):
filename = os.path.basename(filename) filename = os.path.basename(filename)
path = os.path.join(conf['private'], name.lstrip('../')) path = os.path.join(conf['repo'], name.lstrip('../'))
if os.path.exists(path): if os.path.exists(path):
@ -40,9 +40,9 @@ def public(name=''):
l = split(n, '/') l = split(n, '/')
tree.append('<li>') tree.append('<li>')
if len(l) > 1: if len(l) > 1:
tree.append('<a href="/private/'+'/'.join(l[0:-1])+'">..</a>') tree.append('<a href="/repo/'+'/'.join(l[0:-1])+'">..</a>')
else: else:
tree.append('<a href="/private/">..</a>') tree.append('<a href="/repo/">..</a>')
tree.append('</li>') tree.append('</li>')
dirs = [] dirs = []
@ -52,12 +52,12 @@ def public(name=''):
if not f.startswith('.') and not f.startswith('_'): if not f.startswith('.') and not f.startswith('_'):
if isdir(os.path.join(path,f)): if isdir(os.path.join(path,f)):
dirs.append('<li>') dirs.append('<li>')
dirs.append('<a href="/private/'+os.path.join(name,f)+'">'+f+'</a>') dirs.append('<a href="/repo/'+os.path.join(name,f)+'">'+f+'</a>')
dirs.append('<a href="/delete/'+os.path.join(name,f)+'">(delete)</a>') dirs.append('<a href="/delete/'+os.path.join(name,f)+'">(delete)</a>')
dirs.append('</li>') dirs.append('</li>')
else: else:
files.append('<li>') files.append('<li>')
files.append('<a href="/private/'+os.path.join(name,f)+'">'+f+'</a>') files.append('<a href="/repo/'+os.path.join(name,f)+'">'+f+'</a>')
files.append('<a href="/delete/'+os.path.join(name,f)+'">(delete)</a>') files.append('<a href="/delete/'+os.path.join(name,f)+'">(delete)</a>')
files.append('</li>') files.append('</li>')
@ -94,7 +94,7 @@ def public(name=''):
if lower(extension) in ext_images: if lower(extension) in ext_images:
content = '<img src="/private/'+os.path.join(name)+'"/>' content = '<img src="/repo/'+os.path.join(name)+'"/>'
return template('templates/yield', content = content, \ return template('templates/yield', content = content, \
title=filename+extension) title=filename+extension)
@ -103,7 +103,7 @@ def public(name=''):
print "This is a file: " + unquote(name) print "This is a file: " + unquote(name)
return static_file(unquote(name), root=conf['private']) return static_file(unquote(name), root=conf['repo'])
else: else:
@ -147,7 +147,7 @@ def upload(path=''):
@route('/upload', method="POST") @route('/upload', method="POST")
def do_upload(): def do_upload():
repo = Repo(conf['private']) repo = Repo(conf['repo'])
path = request.forms.path path = request.forms.path
@ -157,10 +157,10 @@ def do_upload():
filename = u.filename filename = u.filename
new_file = os.path.join(conf['private'],path,filename) new_file = os.path.join(conf['repo'],path,filename)
if not os.path.exists(os.path.join(conf['private'],path)): if not os.path.exists(os.path.join(conf['repo'],path)):
os.mkdir(os.path.join(conf['private'],path)) os.mkdir(os.path.join(conf['repo'],path))
overwritten = False overwritten = False
if os.path.exists(new_file): if os.path.exists(new_file):
@ -170,8 +170,8 @@ def do_upload():
w.write(u.file.read()) w.write(u.file.read())
w.close() w.close()
content = ['<p><a href="/private/'+path+'/'+filename+'">' + filename + '</a> uploaded to\ content = ['<p><a href="/repo/'+path+'/'+filename+'">' + filename + '</a> uploaded to\
<a href="/private/'+path+'">/' + path + '</a>'] <a href="/repo/'+path+'">/' + path + '</a>']
if overwritten: if overwritten:
content.append(' (original overwritten).</p>') content.append(' (original overwritten).</p>')
@ -181,7 +181,7 @@ def do_upload():
if not overwritten or (overwritten and not repo.git.diff(new_file) == ""): if not overwritten or (overwritten and not repo.git.diff(new_file) == ""):
repo.git.add(new_file) repo.git.add(new_file)
content.append("<p>Git: ") content.append("<p>Git: ")
commit_result = repo.git.commit(new_file,m=" '"+re.sub("'","\\'",message)+"'") commit_result = repo.git.commit(new_file,m=message)
if not commit_result.endswith('.'): if not commit_result.endswith('.'):
commit_result += '.' commit_result += '.'
content.append(commit_result) content.append(commit_result)
@ -225,22 +225,22 @@ def delete(path = ''):
@route('/delete', method='POST') @route('/delete', method='POST')
def delete(): def delete():
body = [] body = []
repo = Repo(conf['private']) repo = Repo(conf['repo'])
path = request.forms.path path = request.forms.path
directory,filename = os.path.split(path) directory,filename = os.path.split(path)
directory = '/'+directory directory = '/'+directory
message = request.forms.message message = request.forms.message
result = False result = False
if os.path.exists(os.path.join(conf['private'],path)): if os.path.exists(os.path.join(conf['repo'],path)):
try: try:
repo.git.rm(path, r=True) repo.git.rm(path, r=True)
result = repo.git.commit(path, m=" '"+re.sub("'","\\'",message)+"'") result = repo.git.commit(path, m=" '"+re.sub("'","\\'",message)+"'")
except: except:
if not isdir(os.path.join(conf['private'],path)): if not isdir(os.path.join(conf['repo'],path)):
os.remove(os.path.join(conf['private'],path)) os.remove(os.path.join(conf['repo'],path))
body.append('<p>' + path + ' deleted from <a href="/private'+directory+'">'+directory+'</a>.</p>') body.append('<p>' + path + ' deleted from <a href="/repo'+directory+'">'+directory+'</a>.</p>')
if result: body.append('<p>Git: '+result+'</p>') if result: body.append('<p>Git: '+result+'</p>')
@ -261,10 +261,89 @@ def edit(path=''):
def edit(): def edit():
return 'This is the edit post page.' return 'This is the edit post page.'
@route('/new', method='GET')
@route('/new/<path:path>', method='GET')
def new(path = False):
body = []
body.append('<style type="text/css">\
td.r { text-align: left; vertical-align:top;}\
td.c { text-align: center; }\
td.l { text-align: right; vertical-align:top;}\
</style>')
body.append('<h1>Create a new file</h1>')
body.append('<form style="width:500px;" method="post" action="/new" enctype="multipart/form-data">')
body.append('<table>')
body.append('<tr><td class="l"><label for="name">Filename:</label></td>')
body.append('<td class="r"><input type="text" name="name" style="width:250px"/></td></tr>')
body.append('<tr><td class="l"><label for="path">Destination:</label></td>')
body.append('<td class="r"><input type="text" name="path" style="width:250px"')
if path: body.append('value="'+path+'" ')
body.append('/></td></tr>')
body.append('<tr><td class="l"><label for="content">Content:</label></td>')
body.append('<td class="c" style="width:100%"><textarea name="content" cols="80" rows="80"></textarea></td></tr>')
body.append('<tr><td class="l"><label for="message">Commit Message</label></td>')
body.append('<td class="r"><textarea name="message" cols="50" rows="7"></textarea></td></tr>')
body.append('<tr><td class="c"><input type="submit" name="submit"></td></tr>')
body.append('</table>')
body.append('</form>')
return template('templates/yield', content='\n'.join(body), \
title='New')
@route('/new', method="POST")
def new():
filename = request.forms.name
directory = request.forms.path
content = request.forms.content
message = request.forms.message
full_path = os.path.join(conf['repo'], directory, filename)
w = open(full_path, 'w')
w.write(content)
w.close()
repo = Repo(conf['repo'])
commit = False
overwritten = False
if os.path.exists(os.path.join(directory,filename)):
overwritten = True
diff = repo.git.diff(os.path.join(directory,filename))
if not overwritten or (overwritten and not diff == ''):
repo.git.add(os.path.join(directory, filename))
commit = repo.git.commit(os.path.join(directory, filename),m=message)
body = []
body.append('<p>'+filename+' added to <a href="/repo/'+directory+'">'+(directory if not directory == '' else '/')+'</a></p>')
if commit:
body.append('<p>Git: '+commit+'</p>')
return template('templates/yield', content='\n'.join(body), \
title=filename + ' created')
@route('/rename/<path:path>', method="GET")
@route('/rename', method="GET")
def rename():
return 'This is the rename page.'
@route('/rename', method="POST")
def rename():
return 'This is the rename post page.'
@route('/api')
def api():
return 'Someday there will be an API here.'
@route('/') @route('/')
def index(): def index():
return "Nothing to see here." redirect('/repo')
if __name__ == "__main__": if __name__ == "__main__":
run(host="dev.amdavidson.com", port=8000, reloader=True) run(host="dev.amdavidson.com", port=8000, reloader=True)