diff options
author | Nico von Geyso <Nico.Geyso@FU-Berlin.de> | 2012-08-14 20:30:19 +0200 |
---|---|---|
committer | Nico von Geyso <Nico.Geyso@FU-Berlin.de> | 2012-08-14 20:30:19 +0200 |
commit | 4486c6d1a591ad08e47f1d14aa381fb1fe2037a4 (patch) | |
tree | 3669c9dac08151179c7d19479d8b6f1ff77db6b1 | |
parent | e7ad17229dd7d5f9e80a6fe32799c927d4546973 (diff) | |
download | klausuren-4486c6d1a591ad08e47f1d14aa381fb1fe2037a4.tar.gz klausuren-4486c6d1a591ad08e47f1d14aa381fb1fe2037a4.tar.bz2 klausuren-4486c6d1a591ad08e47f1d14aa381fb1fe2037a4.zip |
refactoring
-rw-r--r-- | app.py | 7 | ||||
-rw-r--r-- | fit.py | 95 | ||||
-rw-r--r-- | munin.py | 15 |
3 files changed, 63 insertions, 54 deletions
@@ -43,7 +43,7 @@ class UploadForm(Form): def validate_exam(form, field): exts = app.config['ALLOWED_EXTENSIONS'] - ext = map(lambda x: field.data.filename.endswith(x), exts) + ext = map(field.data.filename.endswith, exts) if not any(ext): raise ValidationError(u'Ungültiger Dateityp') @@ -60,7 +60,6 @@ class UploadForm(Form): - @app.route('/<study>/upload', methods=['GET', 'POST']) def upload(study): form = UploadForm() @@ -117,6 +116,8 @@ def study_index(study, module=None): return render_template('module_list.html', study = study, modules=fit[study].get_modules()) + + @app.route('/') def index(): get_img_path = lambda x: os.path.join('images', x +'.png') @@ -127,6 +128,8 @@ def index(): studies = studies ) + + @app.route('/403') @app.errorhandler(403) def forbidden(): @@ -9,6 +9,56 @@ class Fit: def __init__(self, path): self.repo = init_repository(path, True) + + def get_file(self, oid): + """ Returns the actual data of a git object """ + return self.repo[oid].data + + + def get_modules(self): + """ returns a list of all modules """ + return [x[0] for x in self._list()] + + + def get_module(self, module): + """ gets all entries for a module grouped by years """ + years = self._list(module) + return [(year[0], self._list(os.path.join(module, year[0]))) for year in years] + + def add_file(self, data, path): + """ Inserts the given file in the git tree and commits the changes """ + try: + commit = self._get_last_commit() + parents = [commit.oid] + root = commit.tree.oid + + except: + parents = [] + root = None + + + blob_oid = self.repo.create_blob(data) + tree = self._insert_node(blob_oid, path, root) + author = committer = Signature('Fit', 'Fit@fit.de', int(time.time()), 120) + + commit = self.repo.create_commit( + 'HEAD', + author, + committer, + 'added %s' % path, + tree, + parents + ) + + # save the actual head sha for dump git http protocol + # similiar to `git update-server-info` + info_refs_path = os.path.join(self.repo.path, 'info', 'refs') + with open(info_refs_path, 'w') as f: + f.write('%s\trefs/heads/master\n' % b2a_hex(commit).decode('ascii')) + + return b2a_hex(blob_oid).decode('ascii') + + def _insert_node(self, node_oid, path, root_oid): """ Inserts a new Node in a Git Tree graph """ if root_oid: @@ -73,43 +123,6 @@ class Fit: return self.repo[head.oid] - def add_file(self, data, path): - """ Inserts the given file in the git tree and commits the changes """ - try: - commit = self._get_last_commit() - parents = [commit.oid] - root = commit.tree.oid - - except: - parents = [] - root = None - - - blob_oid = self.repo.create_blob(data) - tree = self._insert_node(blob_oid, path, root) - author = committer = Signature('Fit', 'Fit@fit.de', int(time.time()), 120) - - commit = self.repo.create_commit( - 'HEAD', - author, - committer, - 'added %s' % path, - tree, - parents - ) - - info_refs_path = os.path.join(self.repo.path, 'info', 'refs') - with open(info_refs_path, 'w') as f: - f.write('%s\trefs/heads/master\n' % b2a_hex(commit).decode('ascii')) - - return b2a_hex(blob_oid).decode('ascii') - - - def get_file(self, oid): - """ Returns the actual data of a git object """ - return self.repo[oid].data - - def _list(self, path=None): """ Lists all entries for a given path """ try: @@ -120,17 +133,11 @@ class Fit: for p in path.split('/'): tree = tree[p].to_object() - return ((x.name, x.hex) for x in tree) + return [(x.name, x.hex) for x in tree] except: return [] - def get_modules(self): - return (x[0] for x in self._list()) - - def get_module(self, module): - years = self._list(module) - return [(year[0], self._list(os.path.join(module, year[0]))) for year in years] #fit = Fit('static/fit.git') #fit.add_file('main.c', 'alp3/2007/main.c') @@ -1,21 +1,20 @@ import sys +from itertools import chain from app import fit -def select_entries(module_list): - return map(lambda x: x[1], module_list) - - if __name__ == "__main__": entries = {} - for name, instance in fit.items(): - entries[name] = map(lambda x: select_entries(instance.get_module(x)), instance.get_modules()) if len(sys.argv) > 1 and sys.argv[1] == 'config': print 'graph_title Klausuren' print 'graph_vlabel Amount' for label in fit.keys(): print '%s.label %s' % (label, label.capitalize()) + else: - for label, value in entries.items(): - print '%s.value %s' %(label, sum(map(lambda x: len(x), value))) + for label, value in fit.items(): + # fetch all file entries and chain them as a list of tuples + module_list = chain(*map(value.get_module, value.get_modules())) + amount = sum([len(x[1]) for x in module_list]) + print '%s.value %d' % (label, amount) |