if parent_info[0]: # non-root node stmt = "INSERT INTO %(table_name)s " \ "(primary_key,seq,%(parent_name)s" \ "%(other_cols)s) " \ "VALUES (%(primary_key)s,%(seq)s,%(parent_key)s" \ "%(other_vals)s)" % insDct insertLines.append(stmt) else: # the root node stmt = "INSERT INTO %(table_name)s " \ "(primary_key) VALUES (%(primary_key)s)" % insDct insertLines.append(stmt) #-- Command line version of tool if __name__ == '__main__': if len(sys.argv) > 1: if sys.argv[1] in ('-h', '/h', '-?', '/?', '?', '--help'): print(__shell_usage__) else: keep_containers(MAYBE) # Keep XML iff character markup py_obj = XML_Objectify(sys.argv[1], DOM).make_instance() walkNodes(py_obj) for insertLine in insertLines: print(insertLine.encode('UTF-8') + ';') else: keep_containers(MAYBE) # Keep XML iff character markup py_obj = XML_Objectify(sys.stdin, DOM).make_instance() walkNodes(py_obj) for insertLine in insertLines: print(insertLine.encode('UTF-8') + ';')
0.10 Initial version. 0.20 Module refactored into gnosis.xml package. This is a first pass, and various documentation and test cases should be added later. 02/03 Fixed some errors in the transition to Gnosis Utilities packaging. """ from types import * # Names from xml_objectify (and configure xml_objectify) from gnosis.xml.objectify import XML_Objectify, keep_containers from gnosis.xml.objectify import ALWAYS, MAYBE, NEVER, DOM, EXPAT keep_containers(MAYBE) # Which concrete indexer should serve as the base for XML_Indexer? import gnosis.indexer as indexer # Create a class to implement XPATH-like indexing class XML_Indexer(indexer.PreferredIndexer, indexer.TextSplitter): """Concrete Indexer for XML-as-hierarchical-filesystem """ def add_file(self, fname): "Index the nodes of an XML file" # Read in the file (if possible) try: py_obj = XML_Objectify(fname, EXPAT).make_instance() if not py_obj: # Fallback to DOM where Expat has problems raise "BadPaserError"
0.10 Initial version. 0.20 Module refactored into gnosis.xml package. This is a first pass, and various documentation and test cases should be added later. 02/03 Fixed some errors in the transition to Gnosis Utilities packaging. """ from types import * # Names from xml_objectify (and configure xml_objectify) from gnosis.xml.objectify import XML_Objectify, keep_containers from gnosis.xml.objectify import ALWAYS, MAYBE, NEVER, DOM, EXPAT keep_containers(MAYBE) # Which concrete indexer should serve as the base for XML_Indexer? import gnosis.indexer as indexer # Create a class to implement XPATH-like indexing class XML_Indexer(indexer.PreferredIndexer, indexer.TextSplitter): """Concrete Indexer for XML-as-hierarchical-filesystem """ def add_file(self, fname): "Index the nodes of an XML file" # Read in the file (if possible) try: py_obj = XML_Objectify(fname, EXPAT).make_instance() if not py_obj: # Fallback to DOM where Expat has problems
"(primary_key,seq,%(parent_name)s" \ "%(other_cols)s) " \ "VALUES (%(primary_key)s,%(seq)s,%(parent_key)s" \ "%(other_vals)s)" % insDct insertLines.append(stmt) else: # the root node stmt = "INSERT INTO %(table_name)s " \ "(primary_key) VALUES (%(primary_key)s)" % insDct insertLines.append(stmt) #-- Command line version of tool if __name__ == '__main__': if len(sys.argv) > 1: if sys.argv[1] in ('-h','/h','-?','/?','?','--help'): print __shell_usage__ else: keep_containers(MAYBE) # Keep XML iff character markup py_obj = XML_Objectify(sys.argv[1],DOM).make_instance() walkNodes(py_obj) for insertLine in insertLines: print insertLine.encode('UTF-8')+';' else: keep_containers(MAYBE) # Keep XML iff character markup py_obj = XML_Objectify(sys.stdin,DOM).make_instance() walkNodes(py_obj) for insertLine in insertLines: print insertLine.encode('UTF-8')+';'