def do_build(self, source, fromFile, catalog=None, bagcls=Bag, empty=None): """TODO :param source: TODO :param fromFile: TODO :param catalog: TODO :param bagcls: TODO :param empty: TODO :param testmode: TODO """ bagImport = _SaxImporter() if not catalog: catalog = gnrclasses.GnrClassCatalog() bagImport.catalog = catalog bagImport.bagcls = bagcls bagImport.empty = empty if fromFile: infile = open(source, 'rt') source = infile.read() infile.close() if six.PY34 and isinstance(source, str): source = source.encode('utf8') sax.parseString(source, bagImport) result = bagImport.bags[0][0] if bagImport.format == 'GenRoBag': result = result['GenRoBag'] if result is None: result = [] return result
def do_build(self, source, fromFile, catalog=None, bagcls=Bag, empty=None, testmode=False): """TODO :param source: TODO :param fromFile: TODO :param catalog: TODO :param bagcls: TODO :param empty: TODO :param testmode: TODO""" if not testmode: bagImport = _SaxImporter() else: bagImport = sax.handler.ContentHandler() if not catalog: catalog = gnrclasses.GnrClassCatalog() bagImport.catalog = catalog bagImport.bagcls = bagcls bagImport.empty = empty bagImportError = _SaxImporterError() if fromFile: infile = open(source) source = infile.read() infile.close() if isinstance(source, six.text_type): if source.startswith('<?xml'): source = source[source.index('?>') + 2:] source = "<?xml version='1.0' encoding='UTF-8'?>%s" % source.encode( 'UTF-8') source = re.sub("&(?!([a-zA-Z][a-zA-Z0-9]*|#\d+);)", "&", source) sax.parseString(source, bagImport) if not testmode: result = bagImport.bags[0][0] if bagImport.format == 'GenRoBag': result = result['GenRoBag'] if result == None: result = [] return result
def do_build(self, source, fromFile, catalog=None, bagcls=Bag, empty=None, testmode=False): """add??? :param source: add??? :param fromFile: add??? :param catalog: add???. Default value is ``None`` :param bagcls: add???. Default value is ``Bag`` :param empty: add???. Default value is ``None`` :param testmode: add???. Default value is ``False`` :returns: add??? """ if not testmode: bagImport = _SaxImporter() else: bagImport = sax.handler.ContentHandler() if not catalog: catalog = gnrclasses.GnrClassCatalog() bagImport.catalog = catalog bagImport.bagcls = bagcls bagImport.empty = empty bagImportError = _SaxImporterError() if fromFile: sax.parse(source, bagImport) else: if isinstance(source, unicode): if source.startswith('<?xml'): source = source[source.index('?>'):] source = "<?xml version='1.0' encoding='UTF-8'?>%s" % source.encode( 'UTF-8') sax.parseString(source, bagImport) if not testmode: result = bagImport.bags[0][0] if bagImport.format == 'GenRoBag': result = result['GenRoBag'] if result == None: result = [] return result
def build(self, bag, filename=None, encoding='UTF-8', catalog=None, typeattrs=True, typevalue=True, addBagTypeAttr=True, unresolved=False, autocreate=False, docHeader=None, self_closed_tags=None, translate_cb=None, omitUnknownTypes=False, omitRoot=False, forcedTagAttr=None, mode4d=False, pretty=None): """Return a complete standard XML version of the Bag, including the encoding tag ``<?xml version=\'1.0\' encoding=\'UTF-8\'?>``; the Bag's content is hierarchically represented as an XML block sub-element of the ``<GenRoBag>`` node. Is also possible to write the result on a file, passing the path of the file as the ``filename`` parameter. :param bag: the Bag to transform in a XML block version :param filename: the path of the output file :param encoding: allow to set the XML encoding :param catalog: TODO :param typeattrs: TODO :param typevalue: TODO :param addBagTypeAttr: TODO :param unresolved: TODO :param autocreate: TODO :param docHeader: TODO :param self_closed_tags: TODO :param translate_cb: TODO :param omitUnknownTypes: TODO :param omitRoot: TODO :param forceTagAttr: TODO >>> mybag = Bag() >>> mybag['aa.bb'] = 4567 >>> mybag.toXml() '<?xml version=\'1.0\' encoding=\'iso-8859-15\'?><GenRoBag><aa><bb T="L">4567</bb></aa></GenRoBag>' """ result = '' if docHeader != False: result = docHeader or "<?xml version='1.0' encoding='" + encoding + "'?>\n" if not catalog: catalog = gnrclasses.GnrClassCatalog() self.translate_cb = translate_cb self.omitUnknownTypes = omitUnknownTypes self.catalog = catalog self.typeattrs = typeattrs self.typevalue = typevalue self.self_closed_tags = self_closed_tags or [] self.forcedTagAttr = forcedTagAttr self.addBagTypeAttr = addBagTypeAttr self.mode4d = mode4d if not typeattrs: self.catalog.addSerializer("asText", bool, lambda b: 'y' * int(b)) self.unresolved = unresolved if omitRoot: result = result + self.bagToXmlBlock(bag, namespaces=[]) else: result = result + \ self.buildTag('GenRoBag', self.bagToXmlBlock( bag, namespaces=[]), xmlMode=True, localize=False) if six.PY2: result = unicode(result).encode(encoding, 'replace') if pretty: from xml.dom.minidom import parseString result = parseString(result) result = result.toprettyxml() result = result.replace('\t\n', '').replace('\t\n', '') if filename: if autocreate: dirname = os.path.dirname(filename) if dirname and not os.path.exists(dirname): os.makedirs(dirname) output = open(filename, 'wt', encoding='utf-8') output.write(result) output.close() return result