xinclude: added pic and latests plugins
[treecutter.git] / xinclude / photoalbum.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 import os
5 import sys
6 import glob
7 from shutil import rmtree,copytree
8 from PIL import Image
9
10 from lxml import etree
11 from lxml.builder import ElementMaker
12
13 from treecutter import constants as const
14
15 class PhotoAlbum(object):
16     def __init__(self, uri):
17         self.uri = uri
18         self.filelist = []
19
20     def thumbnail(self,infile,size):
21         file, ext = os.path.splitext(infile)
22         im = Image.open(infile)
23         im.thumbnail(size, Image.ANTIALIAS)
24         im.save(file + "."+str(size(0))+"x"+str(size(1))+, "JPG")
25
26     def files(self):
27         rootdirs = [self.uri+'/'+d for d in dirs]
28         for rootdir,d in zip(rootdirs,dirs):
29             if os.path.exists(d):
30                 rmtree(d)
31             copytree(rootdir, d)
32         for d in dirs:
33             for root, subdir, files in os.walk(d):
34                 for file in files:
35                     self.filelist.append(os.path.join(root,file))
36
37     def db_xml(self):
38         db = ElementMaker(namespace=const.DB_NS, nsmap=const.NSMAP)
39         co = db.para()
40         for f in self.filelist:
41             co.append(db.filename(db.link(f,**{const.XLINK+"href": f})))
42         return db.para(co)
43
44 def recursively_empty(e):
45     if e.text:
46         return False
47     return all((recursively_empty(c) for c in e.iterchildren()))
48
49 def clean_db(xml):
50     context = etree.iterwalk(xml)
51     for action, elem in context:
52         parent = elem.getparent()
53         if recursively_empty(elem):
54             parent.remove(elem)
55
56 if __name__ == "__main__":
57     for arg in sys.argv[1:]:
58         al = arg.split("=",1)
59         if al[0] == "lang":
60             lang = al[1]
61         if al[0] == "xptr":
62             argument = al[1]
63
64     album = PhotoAlbum(argument)
65     album.files()
66     axml = album.db_xml()
67     clean_db(axml)
68
69     #print(etree.tostring(cxml, pretty_print=True))
70     #sys.stdout.write(out.encode('utf-8'))
71     sys.stdout.write(etree.tostring(axml,encoding='UTF-8',pretty_print=True))