-
Notifications
You must be signed in to change notification settings - Fork 3
/
satl.py
188 lines (143 loc) · 4.75 KB
/
satl.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
from os import makedirs, path, symlink, listdir, stat
import hashlib
import jsonplus as json
import datetime
import shutil
class NotFound(Exception):
pass
class KeyRequired(Exception):
pass
class Satl(object):
"""In persian bucket is Satl
In this utility you can save documents in files without any dependencies
"""
store_path = 'satl'
data_path = store_path + '/data'
keyword_path = store_path + '/keywords'
def __init__(self, key=None, _id=None, data=None):
if key:
self._id = 'satl_' + self.key_generate(key)
elif _id:
self._id = _id
else:
# @TODO maybe in future generate _id can be helpfull
raise KeyRequired
if data:
self.data = data
else:
self.data = {}
@property
def path(self):
return self.get_path(_id=self._id)
def update_date(self):
return datetime.fromtimestamp(stat(self.path).st_ctime)
def create_date(self):
return datetime.fromtimestamp(stat(self.path).st_mtime)
@property
def pk(self):
return self._id
@classmethod
def keyword_path(cls, keyword):
return '%s/%s' % (cls.keyword_path, cls.key_generate(keyword))
@classmethod
def get_path(cls, key=None, _id=None):
if key and not _id:
_id = cls.id_generate(key)
return '%s/%s' % (cls.data_path, _id)
def set_keywords(self, keywords):
self.keywords = keywords
def set_data(self, data):
self.data = data
def relate_keyword(self, keyword):
keyword_path = self.keyword_path(keyword)
if not path.exists(keyword_path):
makedirs('%s/' % keyword_path)
symlink(self.path, keyword_path)
def unrelate_keyword(self, keyword):
raise NotImplementedError
def rerelate_keywords(self):
raise NotImplementedError
def _prepare_storage(self):
if not path.exists(self.store_path):
makedirs(self.store_path)
if not path.exists(self.data_path):
makedirs('%s/' % self.data_path)
if not path.exists(self.path):
makedirs('%s/' % self.path)
def save(self):
self._prepare_storage()
with open('%s/data.json' % self.path, 'w') as f:
f.write(json.dumps(self.data))
def attach_file_object(self, file_body, name):
self._prepare_storage()
if not path.exists(self.path + '/files'):
makedirs(self.path + '/files')
f = open('%s/files/%s' % (self.path, name), 'wb')
f.write(file_body)
f.close()
def attach_file_path(self, file_path):
self._prepare_storage()
if not path.exists(self.path + '/files'):
makedirs(self.path + '/files')
shutil.copy2(file_path, self.path + '/files/')
def files(self):
self._prepare_storage()
path_file = self.path + '/files/'
if not path.exists(path_file):
makedirs(path_file)
return self._query(path_file)
def count_files(self):
self._prepare_storage()
path_file = self.path + '/files/'
if not path.exists(path_file):
makedirs(path_file)
return len(listdir(path_file))
def load(self):
path_file = '%s/data.json' % self.path
if not path.exists(path_file):
raise NotFound
with open(path_file, 'r') as f:
data = json.loads(f.read())
self.data = data
return self.data
def get(self, key, force_get=False):
if self.data == {} or force_get:
self.load()
return self.data[key]
@classmethod
def _query(cls, path_file):
for item in listdir(path_file):
yield Satl(_id=item)
@classmethod
def is_exists(cls, key):
path_file = cls.get_path(key=key)
if not path.exists(path_file):
return False
return True
@classmethod
def filter_by_keyword(cls, keyword):
path_file = cls.keyword_path(keyword)
if not path.exists(path_file):
raise NotFound
return cls._query(path_file)
@classmethod
def filter_by_date(cls, keyword):
raise NotImplementedError
@classmethod
def all(cls):
path_file = cls.data_path
if not path.exists(path_file):
makedirs('%s' % path_file)
return cls._query(path_file)
@classmethod
def count(cls):
path_file = cls.data_path
if not path.exists(path_file):
makedirs('%s' % path_file)
return len(listdir(path_file))
@staticmethod
def id_generate(key):
return 'satl_' + hashlib.sha1(key).hexdigest()
@staticmethod
def key_generate(key):
return hashlib.sha1(key).hexdigest()