1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
|