diff options
Diffstat (limited to 'app/backend.py')
-rw-r--r-- | app/backend.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/app/backend.py b/app/backend.py new file mode 100644 index 0000000..25a5506 --- /dev/null +++ b/app/backend.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- + +import os +import magic + +class Storage: + def __init__(self, root_path): + self.root = root_path + + def _join(self, *arg): + return os.path.join(self.root, *arg) + + def get_file(self, module, year, name): + with open(self._join(module, year, name), 'r') as f: + data = f.read() + mime = magic.Magic(mime=True) + mime_type = mime.from_buffer(data[:1024]) + return mime_type, data + + def get_modules(self): + return [o for o in os.listdir(self.root) if os.path.isdir(self._join(o))] + + def get_module(self, module): + for root, dirs, files in os.walk(self._join(module)): + if len(dirs) == 0: + splitted = root.split(os.path.sep) + if len(splitted) > 1: + year = splitted[-1] + module = splitted[-2] + if year.isdigit(): + yield((year, files))#, os.path.join(root,f)) + + def add_file(self, data, path): + pass |