xinclude: First attempt at a photoalbum
authorFredrik Unger <fred@tree.se>
Tue, 30 Jul 2013 08:27:59 +0000 (10:27 +0200)
committerFredrik Unger <fred@tree.se>
Tue, 30 Jul 2013 08:27:59 +0000 (10:27 +0200)
xinclude/photoalbum.py [new file with mode: 0755]

diff --git a/xinclude/photoalbum.py b/xinclude/photoalbum.py
new file mode 100755 (executable)
index 0000000..ec2f91f
--- /dev/null
@@ -0,0 +1,71 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+import os
+import sys
+import glob
+from shutil import rmtree,copytree
+from PIL import Image
+
+from lxml import etree
+from lxml.builder import ElementMaker
+
+from treecutter import constants as const
+
+class PhotoAlbum(object):
+    def __init__(self, uri):
+        self.uri = uri
+        self.filelist = []
+
+    def thumbnail(self,infile,size):
+        file, ext = os.path.splitext(infile)
+        im = Image.open(infile)
+        im.thumbnail(size, Image.ANTIALIAS)
+        im.save(file + "."+str(size(0))+"x"+str(size(1))+, "JPG")
+
+    def files(self):
+        rootdirs = [self.uri+'/'+d for d in dirs]
+        for rootdir,d in zip(rootdirs,dirs):
+            if os.path.exists(d):
+                rmtree(d)
+            copytree(rootdir, d)
+        for d in dirs:
+            for root, subdir, files in os.walk(d):
+                for file in files:
+                    self.filelist.append(os.path.join(root,file))
+
+    def db_xml(self):
+        db = ElementMaker(namespace=const.DB_NS, nsmap=const.NSMAP)
+        co = db.para()
+        for f in self.filelist:
+            co.append(db.filename(db.link(f,**{const.XLINK+"href": f})))
+        return db.para(co)
+
+def recursively_empty(e):
+    if e.text:
+        return False
+    return all((recursively_empty(c) for c in e.iterchildren()))
+
+def clean_db(xml):
+    context = etree.iterwalk(xml)
+    for action, elem in context:
+        parent = elem.getparent()
+        if recursively_empty(elem):
+            parent.remove(elem)
+
+if __name__ == "__main__":
+    for arg in sys.argv[1:]:
+        al = arg.split("=",1)
+        if al[0] == "lang":
+            lang = al[1]
+        if al[0] == "xptr":
+            argument = al[1]
+
+    album = PhotoAlbum(argument)
+    album.files()
+    axml = album.db_xml()
+    clean_db(axml)
+
+    #print(etree.tostring(cxml, pretty_print=True))
+    #sys.stdout.write(out.encode('utf-8'))
+    sys.stdout.write(etree.tostring(axml,encoding='UTF-8',pretty_print=True))