Commits

victorlin committed 2d3c9b3

Make lxml as an optional dependency.

Comments (0)

Files changed (2)

     packages=find_packages('src'),
     include_package_data=True,
     package_dir={'': 'src'},
-    install_requires=['lxml'])
+    install_requires=[]
+)

src/pycountry/db.py

 """Generic database code."""
 
 import logging
-import lxml.etree
 
 logger = logging.getLogger('pycountry.db')
 
                                {})
 
         f = open(filename, 'rb')
-        etree = lxml.etree.parse(f)
-
-        for entry in etree.xpath('//%s' % self.xml_tag):
+        
+        try:
+            import lxml.etree
+            etree = lxml.etree.parse(f)
+            entries = etree.xpath('//%s' % self.xml_tag)
+        # fall back to standard etree
+        except (ImportError, AssertionError):
+            from xml.etree import ElementTree
+            etree = ElementTree.parse(f)
+            entries = []
+            
+            def walk(parent):
+                for child in parent:
+                    if child.tag == self.xml_tag:
+                        # standard ElementTree doesn't have getparent()
+                        # but it will be used later, therefore, we need to
+                        # do a little hack, add the getparent method here
+                        def getparent():
+                            return parent
+                        child.getparent = getparent
+                        
+                        entries.append(child)
+                    walk(child)
+                        
+            # Walk through all elements in tree to find match tags.
+            # The reason we don't use .iter() here is because we need to
+            # attach getparent tot element
+            walk(etree.getroot())
+            
+        for entry in entries:
             mapped_data = {}
             for key in entry.keys():
                 mapped_data[self.field_map[key]] = entry.get(key)