xinclude: added pic and latests plugins
authorFredrik Unger <fred@tree.se>
Tue, 17 Dec 2013 14:30:56 +0000 (15:30 +0100)
committerFredrik Unger <fred@tree.se>
Tue, 17 Dec 2013 14:30:56 +0000 (15:30 +0100)
pic includes several versions of the image and scales it accordingly
latests creates a list of the latest X articles with images if availible

xinclude/latest.py [new file with mode: 0755]
xinclude/pic.py [new file with mode: 0755]

diff --git a/xinclude/latest.py b/xinclude/latest.py
new file mode 100755 (executable)
index 0000000..3565578
--- /dev/null
@@ -0,0 +1,79 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+from dateutil.tz import *
+from datetime import datetime
+from dateutil.parser import parse
+
+import os
+import fnmatch
+
+from lxml import etree
+from lxml.builder import ElementMaker
+import sys
+import re
+
+from treecutter import constants as const
+
+class Latest(object):
+    def __init__(self, count):
+        self.count = count
+        self.events = []
+        for dirname, dirnames, filenames in os.walk('.'):
+            for filename in filenames:
+                if fnmatch.fnmatch(filename, '*index.??.xml'):
+                    file_ = os.path.join(dirname,filename)
+                    doc = etree.parse(file_)
+                    if doc.xpath(
+            u'/db:article/db:info/db:mediaobject/db:imageobject/db:imagedata',
+                               namespaces=const.XPATH):
+                        self.events.append(Doc(doc,dirname))
+
+    def db_xml(self):
+        db = ElementMaker(namespace=const.DB_NS, nsmap=const.NSMAP)
+        doclist = db.itemizedlist(db.title(u'Latest Projects'))
+        for ev in self.events:
+            doclist.append(ev.db_xml())
+        return doclist
+
+
+class Doc(object):
+    def __init__(self,doc,link):
+        self.pubdate = doc.xpath(u'/db:article/db:info/db:pubdate',
+                               namespaces=const.XPATH)
+        self.fn = doc.xpath(u'/db:article/db:info/db:author/db:personname/db:firstname',
+                               namespaces=const.XPATH)
+        self.ln = doc.xpath(u'/db:article/db:info/db:author/db:personname/db:surname',
+                               namespaces=const.XPATH)
+        self.title = doc.xpath(u'/db:article/db:info/db:title',
+                               namespaces=const.XPATH)
+        self.photo = doc.xpath(
+            u'/db:article/db:info/db:mediaobject/db:imageobject/db:imagedata',
+                               namespaces=const.XPATH)
+        self.link = link
+
+    def db_xml(self):
+        db = ElementMaker(namespace=const.DB_NS, nsmap=const.NSMAP)
+        lst = db.listitem(
+            db.link(db.inlinemediaobject(
+                    db.imageobject(
+                        db.imagedata(fileref=self.link+'/'+self.photo[0].attrib['fileref'],
+                                     width="198", depth="111"))),
+                    **{const.XLINK+"href": self.link}),
+            db.para("Project: "+self.title[0].text,role="project"),
+            db.para("Author: "+self.fn[0].text+" "+self.ln[0].text,role="author"),
+            db.link("Read Details",**{const.XLINK+"href": self.link})
+            )
+        return lst
+
+if __name__ == "__main__":
+    for arg in sys.argv[1:]:
+        al = arg.split("=")
+        if al[0] == "lang":
+            lang = al[1]
+        if al[0] == "xptr":
+            argument = al[1]
+
+    latest = Latest(argument)
+    exml = latest.db_xml()
+    sys.stdout.write(etree.tostring(exml,encoding='UTF-8',pretty_print=False))
diff --git a/xinclude/pic.py b/xinclude/pic.py
new file mode 100755 (executable)
index 0000000..3710450
--- /dev/null
@@ -0,0 +1,70 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+from dateutil.tz import *
+from datetime import datetime
+from dateutil.parser import parse
+
+import os
+import sys
+import Image
+
+from lxml import etree
+from lxml.builder import ElementMaker
+
+from treecutter import constants as const
+
+class Pic(object):
+    def __init__(self, picture, caption):
+        self.picture = picture
+        self.caption = caption
+        self.sizes = [(500,500),(800,800),(1024,1024)]
+        self.outfiles = []
+
+    def thumbnails(self):
+        infile = os.path.splitext(self.picture)
+        for size in self.sizes:
+            outfile = infile[0]+"-"+str(size[0])+infile[1]
+            if infile != outfile:
+                try:
+                    im = Image.open(self.picture)
+                    im.thumbnail(size, Image.ANTIALIAS)
+                    im.save(outfile, im.format)
+                    self.outfiles.append(outfile)
+                except IOError:
+                    print "cannot create thumbnail for '%s'" % infile
+        self.outfiles.append(self.picture)
+
+    def db_xml(self):
+        webpic = self.outfiles[0]
+        imw = Image.open(webpic)
+        ww,dw = imw.size
+
+        db = ElementMaker(namespace=const.DB_NS, nsmap=const.NSMAP)
+
+        pics = db.para()
+        for f in self.outfiles:
+            im = Image.open(f)
+            w,d = im.size
+            pics.append(db.link(str(w)+"x"+str(d)+" ",**{const.XLINK+"href": f}))
+        #pics = list(', '.join(pics))
+        #pics.append(')')
+        #        pics.insert(0,'Sizes (')
+        lst = db.mediaobject(
+            db.imageobject(
+                db.imagedata(fileref=webpic, width=str(ww), depth=str(dw))),
+            db.caption(db.para(self.caption),pics))
+        return lst
+
+if __name__ == "__main__":
+    for arg in sys.argv[1:]:
+        al = arg.split("=")
+        if al[0] == "lang":
+            lang = al[1]
+        if al[0] == "xptr":
+            argument = al[1]
+    p,c = argument.split("|")
+    pic = Pic(p,c)
+    pic.thumbnails()
+    pxml = pic.db_xml()
+    sys.stdout.write(etree.tostring(pxml,encoding='UTF-8',pretty_print=False))