def show(xml_src, parser): """Self test using simple or user-specified XML data >>> xml = ''' ... ... ... Some text about eggs. ... Ode to Spam ... ''' >>> squeeze = lambda s: s.replace(LF*2,LF).strip() >>> print squeeze(show(xml,DOM)[0]) -----* _XO_Spam *----- {Eggs} PCDATA=Some text about eggs. {MoreSpam} PCDATA=Ode to Spam >>> print squeeze(show(xml,EXPAT)[0]) -----* _XO_Spam *----- {Eggs} PCDATA=Some text about eggs. {MoreSpam} PCDATA=Ode to Spam PCDATA= """ try: xml_obj = XML_Objectify(xml_src, parser=parser) py_obj = xml_obj.make_instance() return (pyobj_printer(py_obj).encode('UTF-8'), "++ SUCCESS (using " + parser + ")\n") except: return ("", "++ FAILED (using " + parser + ")\n")
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" py_obj = XML_Objectify(fname, DOM).make_instance() if self.quiet < 5: print "Indexing", fname except IOError: return 0 self.fname_prefix = fname self.recurse_nodes(py_obj)
def frsConvert(): ospath = os.path.normpath(sys.argv[1]) frs = FRS() traverlpaths = [] for dirpath, dirnames, filenames in os.walk(ospath): traverlpaths.append(dirpath) if dirpath == ospath: traverlpaths.remove(dirpath) if ".frs" in dirpath: traverlpaths.remove(dirpath) continue for filename in filenames: traverlpaths.append(os.path.join(dirpath,filename)) for dirpath in traverlpaths: print dirpath,10*'-' manifestpath = frs.manifestpath(dirpath) print manifestpath,10*'*' mf = Manifest() mf.load(file(manifestpath).read()) print 'contenttype:', mf.contenttype print 'uid: ', mf.uid print 'manifest:', mf.manifest metadata = {} metadata['main']={} metadata['main']['contenttype'] = mf.contenttype metadata['dublin']={} for item in mf.manifest: if item['name']: metadata['main']['mimetype'] = item['mimetype'] else: filename = item['fullpath'] subfilepath = frs.subfilespath(dirpath,filename) xml_obj = XML_Objectify(subfilepath).make_instance() if hasattr(xml_obj,"cmf_type"): metadata['main']['contenttype'] = xml_obj.cmf_type.PCDATA if hasattr(xml_obj,"dc_creator"): metadata['dublin']['creators'] = ["users."+xml_obj.dc_creator.PCDATA] if hasattr(xml_obj,"dc_title"): metadata['dublin']['title'] = xml_obj.dc_title.PCDATA if hasattr(xml_obj,"dc_language"): metadata['dublin']['language'] =xml_obj.dc_language.PCDATA if hasattr(xml_obj,"dc_subject"): metadata['dublin']['subjects'] = [subject.PCDATA for subject in xml_obj.dc_subject] if hasattr(xml_obj,"dc_description"): metadata['dublin']['description'] = xml_obj.dc_description.PCDATA if hasattr(xml_obj,"xmp_CreateDate"): metadata['dublin']['created'] = xml_obj.xmp_CreateDate.PCDATA if hasattr(xml_obj,"xmp_ModifyDate"): metadata['dublin']['modified'] = xml_obj.xmp_ModifyDate.PCDATA for field in xml_obj.field: if field.id == "effectiveDate": metadata['dublin']['effective'] = field.PCDATA metadatapath = frs.metadatapath(dirpath) fd = file(metadatapath,"w") json.dump(metadata,fd,ensure_ascii=False,indent=4,cls=AwareJSONEncoder) fd.close()
def main(self): self.parseArgs() testClasses = {} previousTestSuite = None suiteXml = XML_Objectify(self.suiteXmlFilename, parser=DOM).make_instance() for test in suiteXml.test: currentTestSuite = test.name previousTestClass = None for aClass in test.classes.__dict__['class']: currentTestClass = aClass.name self.convert(previousTestSuite, currentTestSuite, self.getClassNameFromPath(previousTestClass), currentTestClass, self.getFullPathToTestFile(currentTestClass)) previousTestClass = currentTestClass previousTestSuite = currentTestSuite
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') + ';')
for instance_name in type_dict['instance']: instance = getattr(object, instance_name) descript = descript + (' '*level)+'<'+instance_name # look ahead to see if the next instance contains lists or instances nested_type_dict = _get_type_dict(instance) descript = descript + XML_printer(instance, level+INDENT, nested_type_dict) if nested_type_dict['instance'] or nested_type_dict['list']: descript = descript + (' '*level)+'</'+instance_name+'>\n' for list_name in type_dict['list']: inst_list = getattr(object, list_name) for instance in inst_list: descript = descript + (' '*level)+'<'+list_name nested_type_dict = _get_type_dict(instance) descript = descript + XML_printer(instance, level+INDENT, nested_type_dict) if nested_type_dict['instance'] or nested_type_dict['list']: descript = descript + (' '*level)+'</'+list_name+'>\n' return descript if __name__ == '__main__': import sys from gnosis.xml.objectify import XML_Objectify, EXPAT xml_obj = XML_Objectify(sys.argv[1], EXPAT) pyobj = xml_obj.make_instance() print XML_printer(pyobj)
def __init__(self, xml_src): XML_Objectify.__init__(self, xml_src=xml_src, parser=EXPAT)
import sys from gnosis.xml.objectify import XML_Objectify, pyobj_printer, EXPAT class MyExpatBased_XML_Objectify(XML_Objectify): expat_args = [] expat_kwargs = {"nspace_sep": None} # Ignore XML namespaces. def __init__(self, xml_src): XML_Objectify.__init__(self, xml_src=xml_src, parser=EXPAT) if len(sys.argv) > 1: for filename in sys.argv[1:]: try: xml_obj = XML_Objectify(xml_src=filename, parser=EXPAT) py_obj = xml_obj.make_instance() except SyntaxError: print """Caught SyntaxError exception! Possibly an XML file with namespaces that is causing this, so try again but ignore XML namespaces...""", xml_obj = MyExpatBased_XML_Objectify(filename) try: py_obj = xml_obj.make_instance() print "it worked!" except: print "it did NOT work!" raise print pyobj_printer(py_obj).encode("UTF-8") else: print "Please specify one or more XML files to Objectify."
"Read using Expat parser and print and objectified XML file" import sys from gnosis.xml.objectify import XML_Objectify, pyobj_printer, EXPAT if len(sys.argv) > 1: for filename in sys.argv[1:]: try: xml_obj = XML_Objectify(xml_src=filename, parser=EXPAT) py_obj = xml_obj.make_instance() except SyntaxError: print("Caught SyntaxError exception!") print("Possibly an XML file with namespaces that is causing this,") print("so try again but ignore XML namespaces...") XML_Objectify.expat_kwargs['nspace_sep'] = None xml_obj = XML_Objectify(xml_src=filename, parser=EXPAT) try: py_obj = xml_obj.make_instance() print("it worked!") except: print("it did NOT work!") raise print(pyobj_printer(py_obj).encode('UTF-8')) else: print("Please specify one or more XML files to Objectify.")
"Read and print and objectified XML file" import sys from gnosis.xml.objectify import XML_Objectify, pyobj_printer, EXPAT, DOM if len(sys.argv) > 1: for filename in sys.argv[1:]: for parser in (DOM, EXPAT): try: xml_obj = XML_Objectify(filename, parser=parser) py_obj = xml_obj.make_instance() print pyobj_printer(py_obj).encode('UTF-8') print "++ SUCCESS ( using", parser, ")" print "=" * 50 except: raise print "++ FAILED ( using", parser, ")" print "=" * 50 else: print "Please specify one or more XML files to Objectify."