photoalbum: rewote to use the image object
[treecutter.git] / xinclude / photoalbum.py
index ec2f91f1e0ada1ababbb2cce5359a0e7bacb2d88..18ae51530a38346261803be5c814e0b1383dbd14 100755 (executable)
@@ -5,41 +5,50 @@ 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.image import Image
 
 from treecutter import constants as const
 
 class PhotoAlbum(object):
     def __init__(self, uri):
-        self.uri = uri
+        self.uri = unicode(uri)
+        self.albumid = abs(hash(self.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))
+        d = self.uri
+        for root, subdir, files in os.walk(d):
+            for f in files:
+                img = Image(os.path.join(root,f))
+                if not img.generated():
+                    self.filelist.append(img)
 
     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)
+        sl = db.itemizedlist(**{const.XML+"id": "slider"})
+        cnt = 0
+        for img in self.filelist:
+            cnt = cnt + 1
+            caption = db.caption()
+            for p in img.caption().split('\n\n'):
+                caption.append(db.para(p))
+            link = db.para(db.link(img.infostr(),
+                                   **{const.XLINK+"href": img.filename()}))
+            caption.append(link)
+            sl.append(
+                db.listitem(db.mediaobject(
+                        db.imageobject(db.imagedata(fileref=img.slider())),caption),
+                            **{const.XML+"id": "p%x%d" % (self.albumid,cnt)}))
+
+        th = db.itemizedlist(**{const.XML+"id": "thumb"})
+        cnt = 0
+        for img in self.filelist:
+            cnt = cnt + 1
+            th.append(db.listitem(db.para(db.link(db.inlinemediaobject(
+                        db.imageobject(db.imagedata(fileref=img.thumbnail()))),**{const.XLINK+"href": "#p%x%d" % (self.albumid, cnt)}))))
+        return db.informalfigure(sl,th,**{const.XML+"id": "box"})
 
 def recursively_empty(e):
     if e.text:
@@ -64,8 +73,9 @@ if __name__ == "__main__":
     album = PhotoAlbum(argument)
     album.files()
     axml = album.db_xml()
-    clean_db(axml)
+#    clean_db(axml)
 
     #print(etree.tostring(cxml, pretty_print=True))
-    #sys.stdout.write(out.encode('utf-8'))
+    #sys.stderr.write(axml.encode('utf-8'))
+    #sys.stderr.write(etree.tostring(axml,encoding='UTF-8',pretty_print=True))
     sys.stdout.write(etree.tostring(axml,encoding='UTF-8',pretty_print=True))