/
fsloader.py
executable file
·735 lines (655 loc) · 24.2 KB
/
fsloader.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
from singleshot.storage import FilesystemEntity, FileInfo
from singleshot.ssconfig import read_config
from singleshot.jpeg import JpegHeader, calculate_box
from singleshot.model import ContainerItem, ImageItem, MONTHS, DynamicContainerItem
import mmap
from singleshot.properties import dtfromtimestamp, Local
import time
import os
from datetime import datetime, date
from cStringIO import StringIO
import cPickle as pickle
#import pickle
import sys
from urlparse import urljoin
import fnmatch
import logging
LOG = logging.getLogger('singleshot')
class FSLoader(object):
def __init__(self, store):
self.store = store
self.config = store.config
def handles(self, finfo):
return False
class ImageFSLoader(FSLoader):
def handles(self, fileinfo):
return self.store.handler.handles(fileinfo)
def load_path(self, path, fileinfo):
if path.endswith(fileinfo.extension):
path = path[:-len(fileinfo.extension)]
img = ImageItem()
img.path = path
img.aliases = (path + fileinfo.extension,)
img.rawimagepath = fileinfo.path
img.filename = fileinfo.filename
self.store.handler.load_metadata(img, fileinfo)
img.modify_time = fileinfo.mtime
if not img.publish_time:
p = month_dir(fileinfo.dirname)
if p:
year, month, t = p
img.publish_time = t
else:
img.publish_time = dtfromtimestamp(fileinfo.mtime)
if not img.title.strip():
img.title = img.filename
rootzor = fileinfo.dirname[len(self.store.root)+1:]
img.viewfilepath = os.path.join(self.store.view_root, rootzor)
return img
def month_dir(fspath):
p2, p1 = os.path.split(fspath)
p2 = os.path.basename(p2)
try:
month = int(p1)
year = int(p2)
except ValueError:
return None
if month > 0 and month < 13:
return year, month, datetime(year, month, 01, 00, 00, 00, tzinfo=Local)
else:
return None
class DirectoryFSLoader(FSLoader):
def __init__(self, store, is_item):
super(DirectoryFSLoader, self).__init__(store)
self._is_item = is_item
def handles(self, fileinfo):
return fileinfo.isdir
def override_template(self, fspath, name):
path = os.path.join(fspath, name)
if os.path.exists(path):
return path
else:
return self.store.find_template(name)
def load_path(self, path, fileinfo):
d = ContainerItem()
config = read_config(os.path.join(fileinfo.path, '_album.cfg'),
{ 'album': { 'title' : '',
'highlightimage' : '',
'order' : 'dir,title',
}
})
d.order = config.get('album', 'order')
d.viewpath = self.override_template(fileinfo.path, 'album.html')
d.imageviewpath = self.override_template(fileinfo.path, 'view.html')
d.title = config.get('album', 'title')
d.modify_time = fileinfo.mtime
d.publish_time = d.modify_time
if not d.title:
mdir = month_dir(fileinfo.path)
if mdir:
year, month, d.publish_time = mdir
d.title = '%s %d' % (MONTHS[month-1], year)
else:
d.title = fileinfo.basename
d.path = path
contents = []
l = len(self.store.root)
for name in os.listdir(fileinfo.path):
fspath1 = os.path.join(fileinfo.path, name)
if self.config.ignore_path(fspath1):
continue
elif os.path.isdir(fspath1):
pass
elif not self._is_item(FileInfo(fspath1)):
continue
path1 = fspath1[l:]
contents.append(path1)
d.contents = contents
return d
class ItemLoader(object):
def load_item(self, path):
return None
def walk(self):
def walk(load, item):
todo = [item]
while todo:
item = todo.pop()
assert item != None
yield item
if item.iscontainer:
for path in item.contents:
item = load(path)
assert item != None
todo.append(item)
return walk(self.load_item, self.load_item('/'))
class MemoizeLoader(ItemLoader):
def __init__(self, load_item=None):
if load_item:
self._load_item = load_item
self._cache = {}
def load_item(self, path):
try:
return self._cache[path]
except KeyError:
v = self._load_item(path)
self._cache[path] = v
return v
class FilesystemLoader(ItemLoader):
def __init__(self, store):
self.store = store
self.config = store.config
self.imgloader = ImageFSLoader(store)
self.loaders = [self.imgloader,
DirectoryFSLoader(store, self.imgloader.handles)]
self._cache = {}
def load_item(self, path):
if path.endswith('/') and len(path) > 1:
path = path[:-1]
if path.startswith(self.store.root):
fspath = path
path = path[len(self.store.root):]
elif not path:
path = '/'
finfo = FileInfo(self.store.root, path[1:])
if self.config.ignore_path(finfo.path):
return None
try:
item = self._cache[path]
except KeyError:
item = None
if not item:
mtime = 0
else:
mtime = item.modify_time
if not mtime or mtime > finfo.mtime:
item = self._load_path(path, finfo)
self._cache[path] = item
if item:
for alias in item.aliases:
self._cache[alias] = item
return item
def _load_path(self, path, finfo):
for loader in self.loaders:
if loader.handles(finfo):
return loader.load_path(path, finfo)
def both(exts):
while True:
n = exts.next()
yield n
yield n.upper()
if not finfo.extension:
xpath = finfo.path
for ext in both(iter(self.imgloader.extensions)):
finfo = FileInfo(xpath + ext)
if finfo.exists:
if self.imgloader.handles(finfo):
return self.imgloader.load_path(path, finfo)
return None
class ImageSize(FilesystemEntity):
"""
An ImageSize is an ImageItem rendered at a particular size.
It may or may not represent something already in the cache.
"""
def __init__(self,
store=None,
image=None,
size=None,
height=None,
width=None,
mtime=None):
self.image = image
self.height = height
self.width = width
self.size = size
self.imagemtime = mtime
self.store = store
self.config = store.config
pn, fn = os.path.split(image.path)
bn, ext = os.path.splitext(image.filename)
filename = '%s-%s.jpg' % (bn,
size)
self.href = self.store.full_href(urljoin(pn[1:] + '/', filename))
path = os.path.join(image.viewfilepath, filename)
super(ImageSize, self).__init__(path)
def _get_uptodate(self):
if not self.exists:
return False
elif self.imagemtime > self.mtime:
return False
else:
return True
uptodate = property(_get_uptodate)
def ensure(self):
if self.uptodate:
return
self.generate()
def generate(self):
if not os.path.exists(self.image.viewfilepath):
os.makedirs(self.image.viewfilepath)
self.store.handler.generate(source=self.image.rawimagepath,
dest=self.path,
size=self)
def expire(self):
if self.exists:
os.remove(self.path)
class ImageSizes(dict):
def __init__(self, image):
self.__image = image
store = image.store
w, h = image.width, image.height
available = []
self.config = store.config
for box in self.availableSizes:
szname = self.sizeNames[box]
width, height = calculate_box(box, w, h)
s = os.stat(image.rawimagepath).st_mtime
sz = ImageSize(store=store,
image=image,
size=box,
width=width,
height=height,
mtime=s)
if not sz.uptodate:
sz.expire()
setattr(self, '%sSize' % szname, sz)
self[box] = sz
self[szname] = sz
setattr(self, 'has%sSize' % szname, True)
def _get_files(self):
# get files for all of the
# existing sizes for this image
pattern = r'(__filter_.+_)?%s-([1-9][0-9]+).jpg' % (re.escape(self.__image.filename),)
rxp = re.compile(pattern)
vp = self.__image.viewfilepath
paths = [os.path.join(vp, filename) for filename in os.listdir(vp) if rxp.match(filename)]
return paths
def _get_availableSizes(self):
return self.config.availableSizes
def _get_sizeNames(self):
return self.config.sizeNames
availableSizes = property(_get_availableSizes)
sizeNames = property(_get_sizeNames)
files = property(_get_files)
class Tag(object):
__slots__ = ('name', 'count', 'imagemap')
def __init__(self, name):
self.name = name
self.count = 0
self.imagemap = 0L
def add(self, imageid):
self.count += 1
self.imagemap |= 1L << imageid
class ImageYear(object):
__slots__ = ('year', 'months', 'count')
def __init__(self, year):
self.year = year
self.months = [0] * 12
self.count = 0
def add(self, month):
self.months[month-1] += 1
self.count += 1
class ItemData(object):
pass
class PickleCacheStore(object):
def __init__(self, store, load_itemdata=None):
self.store = store
self._cachepath = os.path.join(store.view_root, '.itemcache')
self._data = False
self._load_itemdata = load_itemdata
self._itemmap = {}
self._directories = []
self.__uptodatecheck = 0
def uptodate(self):
n = time.time()
path = self._cachepath
if (n - self.__uptodatecheck) < 10.:
return True
try:
self.__uptodatecheck = n
LOG.debug('Rechecking directories')
t = os.stat(path).st_mtime
outofdate = [item for item in self._directories if os.stat(item).st_mtime > t]
return not bool(outofdate)
except:
pass
return False
def _store(self, itemdata):
s = StringIO()
pickle.dump(itemdata, s,
pickle.HIGHEST_PROTOCOL)
f = open(self._cachepath, 'w')
f.write(s.getvalue())
f.close()
def _read(self):
n = time.time()
f = open(self._cachepath, 'r')
try:
self._prepare_data(pickle.load(f))
finally:
f.close()
LOG.info('Loaded itemcache %.2fms', (time.time() - n)*1000.)
def _initcache(self):
n = time.time()
itemdata = self._load_itemdata()
self._store(itemdata)
self._prepare_data(itemdata)
self.__uptodatecheck = time.time()
LOG.info('Initialized itemcache %.2fms', (time.time() - n)*1000.)
def ready(self):
if not self._data:
try:
self._read()
except IOError:
pass
if not self._data:
self._initcache()
if not self.uptodate():
self._initcache()
def _prepare_data(self, data):
self._directories = []
self._data = data
self._itemlist = data._itemlist
self._itemmap = {}
self._years = data._years
for item in data._itemlist:
assert item != None
if item.iscontainer:
self._directories.append(os.path.join(self.store.root,
item.path[1:]))
self._itemmap[item.path] = item
for alias in item.aliases:
self._itemmap[alias] = item
self.tags = data.tags
self._allimages = data._allimages
def all_tags(self):
self.ready()
return self.tags.items()
def load_item(self, path):
self.ready()
try:
return self._itemmap[path]
except KeyError:
return None
def _query(self, tags):
self.ready()
imgmap = self._allimages
excmap = 0L
for tag in tags:
tag = self.prepare_tag(tag)
try:
if tag[0] == '!':
tag = tag[1:]
try:
excmap |= self.tags[tag].imagemap
except KeyError:
pass
else:
imgmap &= self.tags[tag].imagemap
except KeyError:
pass
if imgmap == self._allimages:
imgmap = 0L
return imgmap, excmap
def query_tag(self, tags):
imgmap, excmap = self._query(tags)
result = []
for idx in xrange(len(self._itemlist)):
imgmask = 1L << idx
if imgmap & imgmask and not excmap & imgmask:
result.append(self._itemlist[idx].path)
return result
def most_tags(self):
self.ready()
x = [(unicode(tag.name), tag) for tag in self.tags.values() if tag.count > 3 and ':' not in tag.name]
return x
def recent_images(self, count=10):
self.ready()
imgs = [(item.publish_time, item.path) for item in self._itemlist if isinstance(item, ImageItem)]
imgs.sort()
imgs.reverse()
if count > len(imgs):
count = len(imgs)
return [img[1] for img in imgs[:count]]
def recent_items(self, count=10):
event = None
items = []
for item in self.recent_images(1000):
item = self.load_item(item)
if event and event in item.keywords:
continue
else:
event = None
eventalbum = None
for keyword in item.keywords:
if keyword.startswith('event:'):
evtkey = keyword[6:]
eventalbum = '/albums/events/%s' % evtkey
if eventalbum:
items.append(eventalbum)
event = keyword
else:
items.append(item.path)
if len(items) == count:
break
return items
def image_years(self):
self.ready()
return self._years.values()
def image_year(self, year):
self.ready()
return self._years[year]
def prepare_tag(self, tag):
tag = tag.lower()
tag = tag.replace(' ', '')
return tag
CacheStore = PickleCacheStore
class SingleshotLoader(ItemLoader):
def __init__(self, store, parent=None):
self._parent = parent
self._load_item = parent.load_item
self.store = store
self._data = CacheStore(self.store,
load_itemdata=self._load_raw)
self.all_tags = self._data.all_tags
self.most_tags = self._data.most_tags
self.query = self._data.query_tag
self.recent_images = self._data.recent_images
self.recent_items = self._data.recent_items
self.image_year = self._data.image_year
self.image_years = self._data.image_years
self._albums = AlbumData(self.store)
def _load_raw(self):
data = ItemData()
data.tags = {}
data._itemlist = []
data._years = {}
data._allimages = 0L
load_item = self._load_item
data._itemlist.append(load_item('/'))
for item in self._parent.walk():
assert item != None
data._itemlist.append(item)
if isinstance(item, ImageItem):
itemid = len(data._itemlist) - 1
path = item.path
data._allimages |= 1L << itemid
def record_keywords():
def record_keyword(tag):
tag = self._data.prepare_tag(tag)
if not tag:
return
try:
data.tags[tag].add(itemid)
except KeyError:
t = Tag(tag)
t.add(itemid)
data.tags[tag] = t
for tag in item.keywords:
record_keyword(tag)
d = item.publish_time
try:
yearrecord = data._years[d.year]
except KeyError:
yearrecord = ImageYear(d.year)
data._years[d.year] = yearrecord
yearrecord.add(d.month)
record_keyword('publish:%04d-%02d' % (d.year, d.month))
record_keyword('publish:%04d' % (d.year))
record_keyword('publish:%04d-%02d-%02d' % (d.year, d.month, d.day))
if item.camera_model:
record_keyword('camera:%s' % item.camera_model)
record_keywords()
return data
def dynacontainer(self, path, title, **kw):
return DynamicContainerItem(self.store, path, title, **kw)
def load_item(self, path):
store = self.store
if path.startswith('/albums'):
n = path[8:]
data = self._albums.get(n)
if not data:
return None
def absify(path):
if isinstance(path, str):
return path
else:
return '/albums/' + path.path
def cfunc():
if data.tag:
return self.query(data.tag.split('/'))
else:
return [absify(al) for al in data.children]
if data.tag:
order = '-publishtime'
else:
order = ''
return self.dynacontainer(path,
data.title,
contentsfunc=cfunc,
order=order,
caption=data.caption,
**data.info)
elif path.startswith('/recent'):
km = path.split('/')[1:]
count = 200
if len(km) == 2:
try:
count = int(km[1])
except:
pass
if count > 5000:
count = 5000
return self.dynacontainer(path,
'Recent photos',
contentsfunc=lambda : self.recent_images(count),
order='-publishtime')
elif path.startswith('/keyword'):
if len(path) > 9:
kwquery = path[9:]
tags = kwquery.split('/')
d = self.dynacontainer(path,
kwquery,
contentsfunc=lambda : self.query(tags),
order='-publishtime')
else:
mosttags = self.most_tags()
mosttags.sort()
contents = ['/keyword/' + tag for tag, o in mosttags]
d = self.dynacontainer(path,
'Keywords',
contents=contents,
viewpath=store.find_template('keywords.html'),
order = '')
return d
elif path.startswith('/bydate'):
ym = path.split('/')[1:]
if len(ym) == 1:
contents = ['/bydate/' + str(year.year) for year in self.image_years()]
count = reduce(lambda x,y:x+y, [year.count for year in self.image_years()])
contents.sort()
contents.reverse()
return self.dynacontainer(path,
'Browse by date',
contents=contents,
count=count,
viewpath=store.find_template('bydate.html'),
order='')
elif len(ym) == 2:
try:
year = int(ym[1])
except ValueError:
return None
yr = self.image_year(year)
contents = ['/bydate/%04d/%02d' % (year, idx+1) for idx, count in enumerate(yr.months) if count > 0]
contents.sort()
contents.reverse()
return self.dynacontainer(path,
str(year),
contents=contents,
count=yr.count,
viewpath=store.find_template('bydate.html'),
order='')
elif len(ym) == 3:
try:
year = int(ym[1])
month = int(ym[2])
yearo = self.image_year(year)
except ValueError:
return None
except KeyError:
return None
return self.dynacontainer(path,
'%s %d' % (MONTHS[month-1],
year),
count=yearo.months[month-1],
contentsfunc=lambda : self.query(['publish:%04d-%02d' % (year, month)]))
else:
return None
else:
return self._data.load_item(path)
class DynamicAlbum(object):
def __init__(self, name, tag=None, title='', caption='', children=(), **kw):
self.path = name
self.name = name
if not title:
self.title = name
else:
self.title = title
self.tag = tag
self.caption = caption
self.children = children
self.info = kw
class AlbumData(object):
def __init__(self, store):
self._sets = {}
self.store = store
self._loaded = False
def _load(self):
path = os.path.join(self.store.root, '_singleshot.py')
if os.path.exists(path):
gl = {'ALBUMS' : self,
'Album' : DynamicAlbum}
lc = {}
execfile(path, gl, lc)
def add(self, set):
self._sets[set.path] = set
def define_albums(self, title, albums):
def fix_paths(parentpath, set):
self.add(set)
set.path = parentpath
if set.children:
for s in set.children:
if isinstance(s, DynamicAlbum):
if parentpath:
s.path = parentpath + '/' + s.path
fix_paths(s.path, s)
root = []
for set in albums:
root.append(set)
rootset = DynamicAlbum('', title=title, children=root)
fix_paths('', rootset)
def get(self, path):
if not self._loaded:
self._loaded = True
self._load()
return self._sets.get(path)