From 74852f8246c459006223f883a4f6ff2e0ddecf3c Mon Sep 17 00:00:00 2001 From: Fredrik Unger Date: Tue, 30 Jul 2013 10:27:59 +0200 Subject: [PATCH] xinclude: First attempt at a photoalbum --- xinclude/photoalbum.py | 71 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100755 xinclude/photoalbum.py diff --git a/xinclude/photoalbum.py b/xinclude/photoalbum.py new file mode 100755 index 0000000..ec2f91f --- /dev/null +++ b/xinclude/photoalbum.py @@ -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)) -- 2.30.2