Switching to ElementMaker from Element/SubElement
authorFredrik Unger <fred@tree.se>
Sun, 28 Oct 2012 07:22:04 +0000 (08:22 +0100)
committerFredrik Unger <fred@tree.se>
Sun, 28 Oct 2012 07:22:04 +0000 (08:22 +0100)
xinclude/address.py

index fd7e0002589cd843964b5d73fc0d8338bd9bea25..8ce6a6a7b77c581068cb8974afdd21c18b5a5357 100755 (executable)
@@ -7,10 +7,12 @@ from httplib2 import Http
 import urllib
 from math import *
 from lxml import etree
 import urllib
 from math import *
 from lxml import etree
+from lxml.builder import ElementMaker
 from PIL import Image, ImageDraw
 from StringIO import StringIO
 from geohash import encode
 from geographiclib.geodesic import Geodesic
 from PIL import Image, ImageDraw
 from StringIO import StringIO
 from geohash import encode
 from geographiclib.geodesic import Geodesic
+from treecutter import constants as const
 
 # EC Equator lenght
 EC = 40075016.686 # meter
 
 # EC Equator lenght
 EC = 40075016.686 # meter
@@ -21,18 +23,6 @@ ZOOMRANGE = range(1, 18)
 # tile size
 TS = 256
 
 # tile size
 TS = 256
 
-DB_NS="http://docbook.org/ns/docbook"
-DB = "{%s}" % DB_NS
-XI_NS="http://www.w3.org/2001/XInclude"
-XI = "{%s}" % XI_NS
-XLINK_NS="http://www.w3.org/1999/xlink"
-XLINK = "{%s}" % XLINK_NS
-HTML_NS="http://www.w3.org/1999/xhtml"
-HTML = "{%s}" % HTML_NS
-NSMAP = {None : DB_NS,
-         'xlink' : XLINK_NS}
-
-
 h = Http(".cache")
 
 class Coord(object):
 h = Http(".cache")
 
 class Coord(object):
@@ -137,26 +127,26 @@ class Coord(object):
         gridc.save(filename)
 
     def db_xml(self):
         gridc.save(filename)
 
     def db_xml(self):
-        uri = etree.Element(DB+'uri',nsmap=NSMAP)
-        ln  = etree.SubElement(uri,  DB+'link')
-        ln.set(XLINK+'href',self.osmlink())
-        imo  = etree.SubElement(ln,  DB+'inlinemediaobject')
-        io   = etree.SubElement(imo, DB+'imageobject', condition="web")
-        idat = etree.SubElement(io , DB+'imagedata',
-                                fileref=encode(self.latitude, self.longitude)+'.png',
-                                format='PNG')
-        to   = etree.SubElement(imo, DB+'textobject')
-        ph   = etree.SubElement(to,  DB+'phrase')
-        ph.text = "geo:"+str(self.latitude)+","+str(self.longitude)
-        para = etree.SubElement(ln,  DB+'para')
-        para.text = self.dms()
+        img = encode(self.latitude, self.longitude)+'.png'
+        phr = "geo:"+str(self.latitude)+","+str(self.longitude)
+
+        db = ElementMaker(namespace=const.DB_NS, nsmap=const.NSMAP)
+        uri = db.uri(db.link(
+                db.inlinemediaobject(
+                    db.imageobject(db.imagedata(
+                            fileref=img,
+                            format='PNG'))
+                    db.textobject(db.phrase(phr))
+                    ),
+                db.para(self.dms())
+                **{const.XLINK+"href": self.osmlink()}))
         return uri
 
         return uri
 
+
 class Address(object):
     """Address object to contain everything known about an address"""
     def __init__(self,address):
         self._address_string = address
 class Address(object):
     """Address object to contain everything known about an address"""
     def __init__(self,address):
         self._address_string = address
-        self._root = etree.Element(DB+'address',nsmap=NSMAP)
         self._coord = None
 
     def geocode(self,country=None):
         self._coord = None
 
     def geocode(self,country=None):