From d72fd47a35ddab517a3897cb13c32cb4bd3a6696 Mon Sep 17 00:00:00 2001 From: Andrew Davidson Date: Fri, 20 Apr 2012 15:30:30 -0700 Subject: [PATCH] more work --- brain.py | 133 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 106 insertions(+), 27 deletions(-) diff --git a/brain.py b/brain.py index 57c36f5..e47e9e3 100644 --- a/brain.py +++ b/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/') +@route('/repo') +@route('/repo/') +@route('/repo/') 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('
  • ') if len(l) > 1: - tree.append('..') + tree.append('..') else: - tree.append('..') + tree.append('..') tree.append('
  • ') 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('
  • ') - dirs.append(''+f+'') + dirs.append(''+f+'') dirs.append('(delete)') dirs.append('
  • ') else: files.append('
  • ') - files.append(''+f+'') + files.append(''+f+'') files.append('(delete)') files.append('
  • ') @@ -94,7 +94,7 @@ def public(name=''): if lower(extension) in ext_images: - content = '' + content = '' 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 = ['

    ' + filename + ' uploaded to\ - /' + path + ''] + content = ['

    ' + filename + ' uploaded to\ + /' + path + ''] if overwritten: content.append(' (original overwritten).

    ') @@ -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("

    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('

    ' + path + ' deleted from '+directory+'.

    ') + body.append('

    ' + path + ' deleted from '+directory+'.

    ') if result: body.append('

    Git: '+result+'

    ') @@ -261,10 +261,89 @@ def edit(path=''): def edit(): return 'This is the edit post page.' +@route('/new', method='GET') +@route('/new/', method='GET') +def new(path = False): + + body = [] + + body.append('') + + body.append('

    Create a new file

    ') + body.append('
    ') + body.append('') + body.append('') + body.append('') + body.append('') + body.append('') + body.append('') + body.append('') + body.append('') + body.append('') + body.append('') + body.append('
    ') + body.append('
    ') + + + 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('

    '+filename+' added to '+(directory if not directory == '' else '/')+'

    ') + if commit: + body.append('

    Git: '+commit+'

    ') + + return template('templates/yield', content='\n'.join(body), \ + title=filename + ' created') + + +@route('/rename/', 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)