-
Notifications
You must be signed in to change notification settings - Fork 5
/
filestore.py
253 lines (238 loc) · 10.1 KB
/
filestore.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
from models import *
from uuid import uuid4
from shutil import copy, rmtree
from time import time
import io
import json
import os
import ov2
import itn
class FileStore():
def __init__(self, datapath = './bin/data/'):
self.datapath = datapath
self.poiDataPath = datapath + 'pois/'
self.routeDataPath = datapath + 'routes/'
if not os.path.exists(datapath):
os.makedirs(datapath)
os.makedirs(self.poiDataPath)
os.makedirs(self.routeDataPath)
def all_pois(self):
poiList = []
lists = os.listdir(self.poiDataPath)
for folder in lists:
path = self.poiDataPath + folder
with io.open('%s/meta.json' % path) as metafile:
meta = json.loads(metafile.read())
if not meta['deleted']:
container = POIContainer(meta['name'])
container.id = folder
container.content = []
if os.path.exists('%s/poi.ov2' % path):
with io.open('%s/poi.ov2' % path, 'rb') as stream:
container.content = ov2.get_pois_from_stream(stream)
poiList.append(container)
return poiList
def create_container(self, name = "Nuevo mapa", pois = [], poi_bin_string = None):
uid = uuid4().hex
container = POIContainer(name)
container.id = uid
path = '%s/%s/' % ( self.poiDataPath, uid )
os.mkdir(path)
ts = int(time())
with io.open('%s/meta.json' % path, 'w') as metafile:
metafile.write(unicode(json.dumps({
'name': name,
'created_at': ts,
'updated_at': ts,
'deleted': False
})))
if len(pois) > 0:
with io.open('%s/poi.ov2' % path, 'wb') as stream:
ov2.save_pois_to_stream(stream, pois)
if poi_bin_string:
with io.open('%s/poi.ov2' % path, 'wb') as stream:
stream.write(poi_bin_string)
with io.open('%s/poi.ov2' % path, 'rb') as stream:
container.content = ov2.get_pois_from_stream(stream)
return container
def get_container(self, uid, evenDelete = False):
path = '%s/%s/' % ( self.poiDataPath, uid )
if os.path.exists(path):
with io.open('%s/meta.json' % path) as metafile:
meta = json.loads(metafile.read())
if not meta['deleted'] or evenDelete:
container = POIContainer(meta['name'])
container.id = uid
container.content = []
if os.path.exists('%s/poi.ov2' % path):
with io.open('%s/poi.ov2' % path, 'rb') as stream:
container.content = ov2.get_pois_from_stream(stream)
return container
else:
return None
raise Exception('Malformed meta.json file at [%s]' % uid)
raise Exception('Unknown UID')
def save_container(self, uid, name, content):
path = '%s/%s/' % ( self.poiDataPath, uid )
if os.path.exists(path):
meta = None
ts = int(time())
with open('%s/meta.json' % path) as metafile:
meta = json.loads(metafile.read())
if meta:
with io.open('%s/meta.json' % path, 'w') as metafile:
metafile.write(unicode(json.dumps({
'name': name,
'created_at': meta['created_at'],
'updated_at': ts,
'deleted': meta['deleted']
})))
with io.open('%s/poi.ov2' % path, 'wb') as stream:
ov2.save_pois_to_stream(stream, content)
return
raise Exception('Malformed meta.json file at [%s]' % uid)
raise Exception('Unknown UID')
def delete_container(self, uid, force = False):
path = '%s/%s/' % ( self.poiDataPath, uid )
if os.path.exists(path):
if force:
rmtree(path)
return
meta = None
ts = int(time())
with io.open('%s/meta.json' % path) as metafile:
meta = json.loads(metafile.read())
if meta:
with io.open('%s/meta.json' % path, 'w') as metafile:
metafile.write(unicode(json.dumps({
'name': meta['name'],
'created_at': meta['created_at'],
'updated_at': ts,
"deleted": True
})))
return
raise Exception('Malformed meta.json file at [%s]' % uid)
raise Exception('Unknown UID')
def get_stream_ov2(self, uid):
class StreamOV2():
def __init__(self, datapath = None, uid = None):
self.uid = uid
self.poiDataPath = datapath
self.stream = None
def __enter__(self):
self.stream = io.open('%s/%s/poi.ov2' % ( self.poiDataPath, self.uid ), 'rb', buffering = 4096)
return self.stream
def __exit__(self, ex_type, ex_value, traceback):
if self.stream:
self.stream.close()
return StreamOV2(self.poiDataPath, uid)
def _load_route(self, folder):
path = self.routeDataPath + folder
route = None
with io.open('%s/meta.json' % path) as metafile:
meta = json.loads(metafile.read())
if not meta['deleted']:
route = Route(meta['name'])
route.id = folder
route.content = []
if os.path.exists('%s/route.json' % path):
with io.open('%s/route.json' % path, 'rb') as stream:
route.content = json.load(stream)
return route
def all_routes(self):
routeList = []
lists = os.listdir(self.routeDataPath)
for folder in lists:
path = self.routeDataPath + folder
with io.open('%s/meta.json' % path) as metafile:
meta = json.loads(metafile.read())
if not meta['deleted']:
route = Route(meta['name'])
route.id = folder
route.content = []
if os.path.exists('%s/route.json' % path):
with io.open('%s/route.json' % path, 'rb') as stream:
route.content = json.load(stream)
routeList.append(route)
return routeList
def create_route(self, name = "Nueva ruta", pois = [], poi_id = None):
uid = uuid4().hex
if poi_id != None:
path = '%s/%s/' % ( self.poiDataPath, poi_id )
cpPath = '%s/%s/' % ( self.poiDataPath, uid )
if os.path.exists(path):
copy(path, cpPath)
return self._load_route(uid)
else:
raise Exception('Unknown UID')
container = Route(name)
container.id = uid
path = '%s/%s/' % ( self.routeDataPath, uid )
os.mkdir(path)
ts = int(time())
with io.open('%s/meta.json' % path, 'w') as metafile:
metafile.write(unicode(json.dumps({
'name': name,
'created_at': ts,
'updated_at': ts,
'deleted': False
})))
if len(pois) > 0:
with io.open('%s/route.json' % path, 'w') as stream:
stream.write(unicode(json.dumps(pois)))
return container
def save_route(self, uid, name, content):
path = '%s/%s/' % ( self.routeDataPath, uid )
if os.path.exists(path):
meta = None
ts = int(time())
with open('%s/meta.json' % path) as metafile:
meta = json.loads(metafile.read())
if meta:
with io.open('%s/meta.json' % path, 'w') as metafile:
metafile.write(unicode(json.dumps({
'name': name,
'created_at': meta['created_at'],
'updated_at': ts,
'deleted': meta['deleted']
})))
with io.open('%s/route.json' % path, 'w') as stream:
stream.write(unicode(json.dumps(content, default = lambda o: o.__dict__)))
with io.open('%s/route.itn' % path, 'w') as stream:
itn.save_route_to_stream(stream, content)
return
raise Exception('Malformed meta.json file at [%s]' % uid)
raise Exception('Unknown UID')
def delete_route(self, uid):
path = '%s/%s/' % ( self.routeDataPath, uid )
if os.path.exists(path):
meta = None
ts = int(time())
with io.open('%s/meta.json' % path) as metafile:
meta = json.loads(metafile.read())
if meta:
with io.open('%s/meta.json' % path, 'w') as metafile:
metafile.write(unicode(json.dumps({
'name': meta['name'],
'created_at': meta['created_at'],
'updated_at': ts,
"deleted": True
})))
return
raise Exception('Malformed meta.json file at [%s]' % uid)
raise Exception('Unknown UID')
def get_stream_itn(self, uid):
class StreamITN():
def __init__(self, datapath = None, uid = None):
self.uid = uid
self.routeDataPath = datapath
self.stream = None
def __enter__(self):
self.stream = io.open('%s/%s/route.itn' % ( self.routeDataPath, self.uid ), 'r', buffering = 4096)
return self.stream
def __exit__(self, ex_type, ex_value, traceback):
if self.stream:
self.stream.close()
return StreamITN(self.routeDataPath, uid)
def shutdown(self):
pass