more work
This commit is contained in:
parent
16af8ffb5c
commit
d72fd47a35
1 changed files with 106 additions and 27 deletions
133
brain.py
133
brain.py
|
@ -1,6 +1,6 @@
|
|||
import os, sys
|
||||
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 string import lower, split
|
||||
from urllib import unquote
|
||||
|
@ -11,15 +11,15 @@ import re
|
|||
|
||||
conf = {}
|
||||
|
||||
#conf['private'] = '/srv/git/documents'
|
||||
conf['private'] = '/srv/git/repo'
|
||||
#conf['repo'] = '/srv/git/documents'
|
||||
conf['repo'] = '/srv/git/repo'
|
||||
conf['ext_bundles'] = ['.pages', '.sparsebundle']
|
||||
conf['ext_render'] = []
|
||||
conf['ext_edit'] = ['.md','.txt','.rb','.py','.pl','.sh']
|
||||
|
||||
|
||||
@route('/private')
|
||||
@route('/private/')
|
||||
@route('/private/<name:path>')
|
||||
@route('/repo')
|
||||
@route('/repo/')
|
||||
@route('/repo/<name:path>')
|
||||
def public(name=''):
|
||||
tree = []
|
||||
|
||||
|
@ -27,7 +27,7 @@ def public(name=''):
|
|||
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):
|
||||
|
@ -40,9 +40,9 @@ def public(name=''):
|
|||
l = split(n, '/')
|
||||
tree.append('<li>')
|
||||
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:
|
||||
tree.append('<a href="/private/">..</a>')
|
||||
tree.append('<a href="/repo/">..</a>')
|
||||
tree.append('</li>')
|
||||
|
||||
dirs = []
|
||||
|
@ -52,12 +52,12 @@ def public(name=''):
|
|||
if not f.startswith('.') and not f.startswith('_'):
|
||||
if isdir(os.path.join(path,f)):
|
||||
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('</li>')
|
||||
else:
|
||||
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('</li>')
|
||||
|
||||
|
@ -94,7 +94,7 @@ def public(name=''):
|
|||
|
||||
|
||||
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, \
|
||||
title=filename+extension)
|
||||
|
@ -103,7 +103,7 @@ def public(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:
|
||||
|
||||
|
@ -147,7 +147,7 @@ def upload(path=''):
|
|||
|
||||
@route('/upload', method="POST")
|
||||
def do_upload():
|
||||
repo = Repo(conf['private'])
|
||||
repo = Repo(conf['repo'])
|
||||
|
||||
path = request.forms.path
|
||||
|
||||
|
@ -157,10 +157,10 @@ def do_upload():
|
|||
|
||||
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)):
|
||||
os.mkdir(os.path.join(conf['private'],path))
|
||||
if not os.path.exists(os.path.join(conf['repo'],path)):
|
||||
os.mkdir(os.path.join(conf['repo'],path))
|
||||
|
||||
overwritten = False
|
||||
if os.path.exists(new_file):
|
||||
|
@ -170,8 +170,8 @@ def do_upload():
|
|||
w.write(u.file.read())
|
||||
w.close()
|
||||
|
||||
content = ['<p><a href="/private/'+path+'/'+filename+'">' + filename + '</a> uploaded to\
|
||||
<a href="/private/'+path+'">/' + path + '</a>']
|
||||
content = ['<p><a href="/repo/'+path+'/'+filename+'">' + filename + '</a> uploaded to\
|
||||
<a href="/repo/'+path+'">/' + path + '</a>']
|
||||
|
||||
if overwritten:
|
||||
content.append(' (original overwritten).</p>')
|
||||
|
@ -181,7 +181,7 @@ def do_upload():
|
|||
if not overwritten or (overwritten and not repo.git.diff(new_file) == ""):
|
||||
repo.git.add(new_file)
|
||||
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('.'):
|
||||
commit_result += '.'
|
||||
content.append(commit_result)
|
||||
|
@ -225,22 +225,22 @@ def delete(path = ''):
|
|||
@route('/delete', method='POST')
|
||||
def delete():
|
||||
body = []
|
||||
repo = Repo(conf['private'])
|
||||
repo = Repo(conf['repo'])
|
||||
path = request.forms.path
|
||||
directory,filename = os.path.split(path)
|
||||
directory = '/'+directory
|
||||
message = request.forms.message
|
||||
result = False
|
||||
|
||||
if os.path.exists(os.path.join(conf['private'],path)):
|
||||
if os.path.exists(os.path.join(conf['repo'],path)):
|
||||
try:
|
||||
repo.git.rm(path, r=True)
|
||||
result = repo.git.commit(path, m=" '"+re.sub("'","\\'",message)+"'")
|
||||
except:
|
||||
if not isdir(os.path.join(conf['private'],path)):
|
||||
os.remove(os.path.join(conf['private'],path))
|
||||
if not isdir(os.path.join(conf['repo'],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>')
|
||||
|
||||
|
@ -261,10 +261,89 @@ def edit(path=''):
|
|||
def edit():
|
||||
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('/')
|
||||
def index():
|
||||
return "Nothing to see here."
|
||||
redirect('/repo')
|
||||
|
||||
if __name__ == "__main__":
|
||||
run(host="dev.amdavidson.com", port=8000, reloader=True)
|
||||
|
|
Loading…
Reference in a new issue