From 019eb0f96c5a011a38e43f0c49134cfa5fd53af7 Mon Sep 17 00:00:00 2001 From: Sam Ruby Date: Mon, 16 Oct 2006 19:59:37 -0400 Subject: [PATCH] Fall back to expat if libxml2 is not present --- planet/idindex.py | 24 ++++++++++++++++++++++-- tests/test_idindex.py | 5 +---- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/planet/idindex.py b/planet/idindex.py index 2a3685f..e0184bf 100644 --- a/planet/idindex.py +++ b/planet/idindex.py @@ -25,15 +25,22 @@ def destroy(): log.info(idindex + " deleted") def create(): - import libxml2 from planet import logger as log cache = config.cache_directory() index=os.path.join(cache,'index') if not os.path.exists(index): os.makedirs(index) index = dbhash.open(filename(index, 'id'),'c') + try: + import libxml2 + except: + libxml2 = False + from xml.dom import minidom + for file in glob(cache+"/*"): - if not os.path.isdir(file): + if os.path.isdir(file): + continue + elif libxml2: try: doc = libxml2.parseFile(file) ctxt = doc.xpathNewContext() @@ -45,6 +52,19 @@ def create(): doc.freeDoc() except: log.error(file) + else: + try: + doc = minidom.parse(file) + doc.normalize() + ids = doc.getElementsByTagName('id') + entry = [e for e in ids if e.parentNode.nodeName == 'entry'] + source = [e for e in ids if e.parentNode.nodeName == 'source'] + if entry and source: + index[filename('',entry[0].childNodes[0].nodeValue)] = \ + source[0].childNodes[0].nodeValue + doc.freeDoc() + except: + log.error(file) log.info(str(len(index.keys())) + " entries indexed") index.close() diff --git a/tests/test_idindex.py b/tests/test_idindex.py index b88d1ea..d21803f 100644 --- a/tests/test_idindex.py +++ b/tests/test_idindex.py @@ -52,10 +52,7 @@ class idIndexTest(unittest.TestCase): try: module = 'dbhash' - import dbhash - module = 'libxml2' - import libxml2 except ImportError: - logger.warn(module + " is not available => can't test id index") + logger.warn("dbhash is not available => can't test id index") for method in dir(idIndexTest): if method.startswith('test_'): delattr(idIndexTest,method)