def create(cls, schema_location=None): if not schema_location is None: schema = cls.__get_xml_schema(schema_location) parser = objectify.makeparser(schema=schema) else: parser = objectify.makeparser() # Get the class lookup from the mapping registry. mp_reg = get_mapping_registry(XmlMime) parser.set_element_class_lookup(mp_reg.parsing_lookup) return parser
def config_validators(self, pos): xmlschema_doc = etree.parse(os.path.join(XSD_PATH, CIERRE_XSD[pos])) xmlschema = etree.XMLSchema(xmlschema_doc) self.cierre_parser = objectify.makeparser(schema=xmlschema) xmlschema_doc = etree.parse( os.path.join(XSD_PATH, PROVISIONAL_XSD[pos])) xmlschema = etree.XMLSchema(xmlschema_doc) self.provisional_parser = objectify.makeparser(schema=xmlschema)
def __init__(self, schema_filename=None): self.logger = logging.getLogger(__name__) if schema_filename is not None: module_dir = os.path.split(__file__)[0] schema_file = os.path.join(module_dir, schema_filename) with open(schema_file) as f: self.schema = etree.XMLSchema(file=f) self.schema_parser = objectify.makeparser( schema=self.schema.schema, strip_cdata=False) else: self.schema = None self.schema_parser = objectify.makeparser(strip_cdata=False)
def parse_xml(self, validate=True): """Importar contenido del xml""" self.check_fpos(self.f_xsd) schema = etree.XMLSchema(file=self.f_xsd) parser = objectify.makeparser(schema=schema) try: self.obj = objectify.fromstring(self.str_xml, parser) except Exception as e: self.error = e.message if validate: raise except_f1('Error', u'Documento inválido: {0}'.format(e)) else: parser = objectify.makeparser(schema=None) self.obj = objectify.fromstring(self.str_xml, parser)
def fromstring(text, schema=None, huge_tree=False): """Parses a KML text string This function parses a KML text string and optionally validates it against a provided schema object""" if schema: parser = objectify.makeparser(schema = schema.schema, huge_tree=huge_tree) else: parser = objectify.makeparser(huge_tree=huge_tree) try: return objectify.fromstring(text, parser=parser) except etree.XMLSyntaxError as e: print("You may want to set huge_tree=True") raise e
def parse(fileobject, schema=None): """Parses a file object This function parses a KML file object, and optionally validates it against a provided schema. """ if schema: # with validation parser = objectify.makeparser(schema = schema.schema, strip_cdata=False) return objectify.parse(fileobject, parser=parser) else: # without validation parser = objectify.makeparser(strip_cdata=False) return objectify.parse(fileobject, parser=parser)
def parse(fileobject, schema=None): """Parses a file object This function parses a KML file object, and optionally validates it against a provided schema. """ if schema: # with validation parser = objectify.makeparser(schema=schema.schema, strip_cdata=False) return objectify.parse(fileobject, parser=parser) else: # without validation parser = objectify.makeparser(strip_cdata=False) return objectify.parse(fileobject, parser=parser)
def parse_xml(self, validate=True): """Importar el contingut de l'xml""" self.check_fpos(self.f_xsd) schema = etree.XMLSchema(file=self.f_xsd) parser = objectify.makeparser(schema=schema) try: self.obj = objectify.fromstring(self.str_xml, parser) except Exception as e: self.error = e.message if validate: raise except_f1('Error', _(u'Document invàlid: {0}').format(e)) else: parser = objectify.makeparser(schema=None) self.obj = objectify.fromstring(self.str_xml, parser)
def __init__(self, url=None, username=None, password=None, verify=None, cache=False): # Basic URL and authentication settings self.url = url or self.url self.username = username or self.username self.password = password or self.password self._session = Session() self._session.verify = verify or get_default_verify_paths().capath self.auth = HTTPBasicAuth(self.username, self.password) self.parser = makeparser(huge_tree=True) # API endpoints self.build = Build(osc_obj=self) self.comments = Comment(osc_obj=self) self.groups = Group(osc_obj=self) self.packages = Package(osc_obj=self) self.projects = Project(osc_obj=self) self.requests = BsRequest(osc_obj=self) self.search = Search(osc_obj=self) self.users = Person(osc_obj=self) # Cache if cache: # pylint: disable=broad-except try: self.session = CacheControl(self._session) except Exception as error: self.session = self._session warnings.warn("Cannot use the cache: {}".format(error), RuntimeWarning) else: self.session = self._session
def parser_from_schema(schema_url, require_version=True): """ Returns an XSD-schema-enabled lxml parser from a WSDL or XSD `schema_url` can of course be local path via file:// url NOTE: currently we're not making use of the resulting parser(!) due to: https://bugs.launchpad.net/lxml/+bug/1416853 """ schema_tree = etree.parse(schema_url) def get_version(element, getter): try: return getter(element) except VersionNotFound: if require_version: raise else: return None root = schema_tree.getroot() if root.tag == '{%s}definitions' % namespaces.WSDL: # wsdl should contain an embedded schema schema_el = schema_tree.find('wsdl:types/xs:schema', namespaces=NS_MAP) # hack to avoid segfault: schema_el = _make_safe(schema_el) version = get_version(root, version_from_wsdl) else: schema_el = root version = get_version(schema_el, version_from_schema) schema = etree.XMLSchema(schema_el) return objectify.makeparser(schema=schema), version
def make_obj(files): books = [] ns = {'ns0': 'http://www.gribuser.ru/xml/fictionbook/2.0'} parser = objectify.makeparser(huge_tree=True) for f in files: author = {'first-name': '', 'last-name': ''} genre = [] try: book = et.XML(f) except et.XMLSyntaxError: book = objectify.fromstring(f, parser) title_info = book.find('ns0:description/ns0:title-info', ns) title = title_info.find('ns0:book-title', ns).text author['first-name'] = title_info.find('ns0:author/ns0:first-name', ns).text author['last-name'] = title_info.find('ns0:author/ns0:last-name', ns).text genres = title_info.findall('ns0:genre', ns) for g in genres: temp = g.text genre.append(temp) # body = book.find('ns0:body', ns) bits_of_text = book.xpath('//text()') text = ' '.join(bit.strip() for bit in bits_of_text if bit.strip() != '').lower() text = fws(text) # text = ET.tostring(body, method='text', encoding='utf-8').decode('utf-8') book_obj = Book(title, author, genre, text) books.append(book_obj) print('Books added: {}'.format(Book.amount)) print(book_obj.tostring()) print() print('Books are objectified') return books
def validate_gpx(xml_file): '''Function to validate GPX file against XSD schema of gpx file''' # Copy all GPX file's contents into a string variable. xml_string = xml_file.read() xml_file.close() # Obtain location of the XSD path (relatively to the current file location) xsd_file_location = os.path.join(os.path.dirname(__file__), 'gpx.xsd') # Try to parse the schema. try: # Declare schema to use. schema = etree.XMLSchema(file=xsd_file_location) # Create a parser which we will match against our xml string. parser = objectify.makeparser(schema=schema) # Match XSD with the XML file. Nothing will happen if successful. # XMLSyntaxError will be raised if unsuccessful. objectify.fromstring(str.encode(xml_string), parser) # Capture the exception. except XMLSyntaxError: # Print error message and exit the program. print('ERROR. Failed to validate the GPX file.') sys.exit(1)
def __init__(self, filename): """ Takes the xml-File, that describes the model and builds all necessary datastructures. When a JspModel is given this acts as a copy constructor. """ if isinstance(filename, JspModel): self.model = filename.model self.index_translation_list = filename.index_translation_list self.allowed_machines = filename.allowed_machines self.setuptimes = filename.setuptimes else: # read the xml schema and build a parser from it schemafile = "{}/xml/model.xsd".format(os.path.dirname(__file__)) schema = etree.XMLSchema(file=open(schemafile, "r")) parser = objectify.makeparser(schema=schema) # read the model and build objects from it if filename.endswith(".gz"): modelfile = gzip.open(filename, 'r') else: modelfile = open(filename, 'r') self.model = objectify.parse(modelfile, parser).getroot() # prebuild the index translation list self.index_translation_list = self._create_index_translation_list() # provide a translated list of the allowed machines for every # operation self.allowed_machines = self._create_allowed_machines_list() # create a setupmatrix between all operations (indexed globally) self.setuptimes = self._create_setuptimes()
def validate_xml(content: bytes, xsd_file_name: str): """ Validates XML using XSD """ schema = etree.XMLSchema(file=xsd_file_name) parser = objectify.makeparser(schema=schema) objectify.fromstring(content, parser)
def initParser(schemaFile): if not os.path.exists(schemaFile): log.error("Could not find schema for messages. There should be a " "file named '"+schemaFile+"' in the script directory") sys.exit(1) messageSchema = etree.XMLSchema(file=open(schemaFile, "r")) return objectify.makeparser(schema = messageSchema)
def __init__(self): env = Environment.getInstance() self.log = logging.getLogger(__name__) self.log.debug("initializing AMQP handler") self.env = env self.config = env.config # Initialize parser schema_root = etree.XML(PluginRegistry.getEventSchema()) schema = etree.XMLSchema(schema_root) self._parser = objectify.makeparser(schema=schema) # Evaluate username user = self.config.get("amqp.id", default=None) if not user: user = self.env.uuid password = self.config.get("amqp.key") # Load configuration self.url = parseURL(makeAuthURL(self.config.get('amqp.url'), user, password)) self.reconnect = self.config.get('amqp.reconnect', True) self.reconnect_interval = self.config.get('amqp.reconnect_interval', 3) self.reconnect_limit = self.config.get('amqp.reconnect_limit', 0) # Go for it self.start()
def _parse_plugin(self, plugin_name: str): """ Parses the configuration for the plugin named `plugin_name` :param plugin_name: The name of the plugin to parse """ logging.debug(f"Parsing configuration for {plugin_name} plugin") # we need to create a new parser that parses our 'broken' XML files # (they are regarded as 'broken' because they contain multiple root tags) parser = objectify.makeparser(recover=True) with open(os.path.join(self.path, plugin_name + '.xml')) as f: # wrap the 'broken' XML in a new <root> so that we can parse the # whole document instead of just the first root lines = f.readlines() fixed_xml = bytes( lines[0] + '<root>' + ''.join(lines[1:]) + '</root>', 'utf-8') plugin_tree: objectify.ObjectifiedElement = objectify.fromstring( fixed_xml, parser) plugin_root = plugin_tree.Plugin try: # a balance has no labbCAN device yet device_list = plugin_root.DeviceList if plugin_name == 'balance' \ else plugin_root.labbCAN.DeviceList for device in device_list.iterchildren(): self.devices += [Device(device.get('Name'))] except AttributeError: pass if 'rotaxys' in plugin_name: # no possibility to find the jib length elsewhere for device in plugin_root.DeviceList.iterchildren(): self.device_by_name( device.get('Name')).set_device_property( 'jib_length', abs(int(device.JibLength.text)))
def setUp(self): super(DataLoadToolExportTestCase, self).setUp() self.maxDiff = None # For XML document diffs. self.consumers_schema = dataloadtool_schema('Consumers.xsd') self.parser = objectify.makeparser(schema=self.consumers_schema) self.test_output_path = path.join(path.dirname(__file__), 'test.out') self.test_output_credentials_path = path.join(path.dirname(__file__), 'test_alias.out')
def parser_from_schema(schema_url, require_version=True): """ Returns an XSD-schema-enabled lxml parser from a WSDL or XSD `schema_url` can of course be local path via file:// url """ schema_tree = etree.parse(schema_url) def get_version(element, getter): try: return getter(element) except VersionNotFound: if require_version: raise else: return None root = schema_tree.getroot() if root.tag == '{%s}definitions' % namespaces.WSDL: # wsdl should contain an embedded schema schema_el = schema_tree.find('wsdl:types/xs:schema', namespaces=NS_MAP) version = get_version(root, version_from_wsdl) else: schema_el = root version = get_version(schema_el, version_from_schema) schema = etree.XMLSchema(schema_el) return objectify.makeparser(schema=schema), version
def ncbi_parse_taxonomy_xml(tax_xml): result_keys = [ 'taxid', 'scientific_name', 'rank', 'kingdom', 'phylum', 'class', 'order', 'family', 'genus', 'full_lineage' ] result_list = [] huge_parser = objectify.makeparser(huge_tree=True) xml_results = objectify.fromstring(tax_xml, huge_parser) for tx in xml_results.Taxon: result = {} try: result['taxid'] = tx['TaxId'].text result['scientific_name'] = tx['ScientificName'].text result['rank'] = tx['Rank'].text if hasattr(tx, 'Lineage'): result['full_lineage'] = tx['Lineage'].text if hasattr(tx, 'LineageEx'): for child in tx['LineageEx'].iterchildren(): if child.Rank.text in result_keys: result[child.Rank.text] = child.ScientificName.text except: problem_child = tx['TaxId'].text print('{} could not be parsed'.format(problem_child)) result_list.append(result) return result_list
def __init__(self, resource, inventories=None, cache=None, name=None, logger=None, auto_parse=True): """ Initiate the XMLResolver """ super(XMLFolderResolver, self).__init__(resource=resource) if not isinstance(cache, BaseCache): cache = BaseCache() self.__inventories__ = inventories self.__parser__ = makeparser() self.__cache = cache self.name = name self.logger = logger if not logger: self.logger = logging.getLogger(name) if not name: self.name = "repository" self.TEXT_CLASS = XMLFolderResolver.TEXT_CLASS self.works = [] self.inventory_cache_key = _cache_key("Nautilus", "Inventory", "Resources", self.name) self.texts_metadata_cache_key = _cache_key("Nautilus", "Inventory", "TextsMetadata", self.name) self.texts_parsed_cache_key = _cache_key("Nautilus", "Inventory", "TextsParsed", self.name) __inventory__ = self.__cache.get(self.inventory_cache_key) __texts__ = self.__cache.get(self.texts_metadata_cache_key) if __inventory__ and __texts__: self.inventory, self.__texts__ = __inventory__, __texts__ elif auto_parse: self.parse(resource)
def parseLog(file): """ parse xml """ file = sys.argv[1] handler = open(file).read() parser = objectify.makeparser(recover=True) root = objectify.fromstring(handler, parser) print(root.attrib['venue']) attrs = [ 'id', 'number', 'horse', 'barrier', 'weight', 'goodtrack', 'thisdistance', 'firstup', 'secondup', 'decimalmargin', 'trainernumber', 'rsbtrainername', 'trainertrack', 'jockeynumber', 'jockeysurname', 'jockeyfirstname' ] for noms in root.race: print("#######################################") print('id', 'number', 'horse', 'barrier', 'weight', 'goodtrack', 'thisdistance', 'firstup', 'secondup', 'decimalmargin', 'trainernumber', 'rsbtrainername', 'trainertrack', 'jockeynumber', 'jockeysurname', 'jockeyfirstname') print(noms.attrib.get('id')) for item in noms.nomination: try: listy = [item.attrib[x] for x in attrs] listy.insert(0, noms.attrib.get('id')) print(listy) except KeyError: continue
def main(): ms = mathml_segmentation() data = json.load(open('db/queryResults.json')) content = [] i = 0 for data_d in data['results']['bindings']: mathml_xml_string = (data_d['Formula']["value"]) if (len(mathml_xml_string) > 0 ): i+=1; parser = objectify.makeparser(remove_comments=True,ns_clean=True,recover=True,remove_blank_text=True,resolve_entities=True,encoding='utf-8') mathml_xml = etree.fromstring(mathml_xml_string,parser=parser) label_value = data_d['Label']['value'] if 'Description' in data_d: description_value = data_d['Description']['value'] else: description_value="" ms.reset_formula_id() content = content + (ms.parse_mathml(mathml_xml,label_value,description_value)) # a = u'\xa1' # print smart_str(a) # pprint(content) print(i) ms.make_RDF(content)
def __init__(self, xmlFile): ## @brief Magic number (#XCVR_CONFIG_MAGIC) self.magic = 0x52564358 ## @brief Transceiver configuration version self.version = 0x00000101 ## @brief Transceiver configuration XML file self.xmlFile = xmlFile schemaString = ETHXCVR_ConfigSchema.replace("ETHXCVR_MAX_PORTS", str(ETHXCVR_MAX_PORTS)) schemaString = schemaString.replace("ETHXCVR_MAX_DRIVER_PARAMS", str(ETHXCVR_MAX_DRIVER_PARAMS)) schema = etree.XMLSchema(objectify.fromstring(schemaString)) ## @brief Validating parser for the schema self.parser = objectify.makeparser(schema=schema) fp = open(self.xmlFile, 'r') xml = fp.read() fp.close() ## @brief Objectify tree for the transceiver configuration self.xcvrCfg = objectify.fromstring(xml, self.parser) ## @brief Configuration name self.name = str(self.xcvrCfg.name) ## @brief File name self.file = os.path.basename(self.xmlFile) ## @brief Port configuration self.port = [] for port in self.xcvrCfg.port: self.port.append(ETHXCVR_PortConfig(port))
def __init__(self, xml_str): # Due to Delphi(TM) xml implementation literaly shits into xml, # we need lxml parser to be more forgiving (recover=True): oparser = objectify.makeparser(recover=True) root = objectify.fromstring(xml_str, parser=oparser).ClassInstance try: self.name = str(root.attrib["Name"]) except KeyError: self.name = "Undefinded" _logger.info("hypermap have no name. Giving it 'Undefined' name") dt = datetime.strptime(" ".join([str(root.Header.Date), str(root.Header.Time)]), "%d.%m.%Y %H:%M:%S") self.date = dt.date().isoformat() self.time = dt.time().isoformat() self.version = int(root.Header.FileVersion) # create containers: self.sem = Container() self.stage = Container() self.image = Container() # fill the sem and stage attributes: self._set_sem(root) self._set_image(root) self.elements = {} self._set_elements(root) self.line_counter = np.fromstring(str(root.LineCounter), dtype=np.uint16, sep=",") self.channel_count = int(root.ChCount) self.mapping_count = int(root.DetectorCount) self.channel_factors = {} self.spectra_data = {} self._set_sum_edx(root)
def __init__(self, xml_str): # Due to Delphi(TM) xml implementation literaly shits into xml, # we need lxml parser to be more forgiving (recover=True): oparser = objectify.makeparser(recover=True) root = objectify.fromstring(xml_str, parser=oparser).ClassInstance try: self.name = str(root.attrib['Name']) except KeyError: self.name = 'Undefinded' _logger.info("hypermap have no name. Giving it 'Undefined' name") dt = datetime.strptime( ' '.join([str(root.Header.Date), str(root.Header.Time)]), "%d.%m.%Y %H:%M:%S") self.date = dt.date().isoformat() self.time = dt.time().isoformat() self.version = int(root.Header.FileVersion) # create containers: self.sem = Container() self.stage = Container() self.image = Container() # fill the sem and stage attributes: self._set_sem(root) self._set_image(root) self.elements = {} self._set_elements(root) self.line_counter = np.fromstring(str(root.LineCounter), dtype=np.uint16, sep=',') self.channel_count = int(root.ChCount) self.mapping_count = int(root.DetectorCount) self.channel_factors = {} self.spectra_data = {} self._set_sum_edx(root)
def parse_xml(self, filename, use_objectify=False, elements=None, tags=None): """ Parse and clean the supplied file by removing any elements or tags we don't use. :param filename: The filename of the xml file to parse. Str :param use_objectify: Use the objectify parser rather than the etree parser. (Bool) :param elements: A tuple of element names (Str) to remove along with their content. :param tags: A tuple of element names (Str) to remove, preserving their content. :return: The root element of the xml document """ try: with open(filename, 'rb') as import_file: # NOTE: We don't need to do any of the normal encoding detection here, because lxml does it's own # encoding detection, and the two mechanisms together interfere with each other. if not use_objectify: tree = etree.parse(import_file, parser=etree.XMLParser(recover=True)) else: tree = objectify.parse(import_file, parser=objectify.makeparser(recover=True)) if elements or tags: self.wizard.increment_progress_bar( translate('BiblesPlugin.OsisImport', 'Removing unused tags (this may take a few minutes)...')) if elements: # Strip tags we don't use - remove content etree.strip_elements(tree, elements, with_tail=False) if tags: # Strip tags we don't use - keep content etree.strip_tags(tree, tags) return tree.getroot() except OSError as e: self.log_exception('Opening {file_name} failed.'.format(file_name=e.filename)) critical_error_message_box( title='An Error Occured When Opening A File', message='The following error occurred when trying to open\n{file_name}:\n\n{error}' .format(file_name=e.filename, error=e.strerror)) return None
def __init__(self, schemafile): self.repo = None self.dirs = [] self.outdir = None self.schema = etree.XMLSchema(file=schemafile) self.parser = objectify.makeparser(schema=self.schema) self.loadedfiles = []
def get_recent_tracks(user, **kwargs): return retry(lambda: objectify.fromstring( urllib2.urlopen("http://ws.audioscrobbler.com/2.0/?" + urllib.urlencode(dict(method="user.getrecenttracks", api_key=app.config["LAST_FM_API_KEY"], user=user.username, **kwargs))).read(), objectify.makeparser(encoding="utf-8", recover=True) ), logger=logger)
def load(self): try: # This is necessary to parse POM files with HTML entities parser = objectify.makeparser(resolve_entities=False, recover=True) self.xml = objectify.parse(self.pomFile, parser=parser) except: print 'Error parsing %s' % self.pomFile raise
def convert_to_model_instance(xml, model, unwrap=False): parser = objectify.makeparser() parser.set_element_class_lookup(CustomLookup(model)) obj = objectify.fromstring(xml, parser) if unwrap: return obj.getchildren()[0] return obj
def xml_validator(some_xml_string, xsd_file): try: schema = etree.XMLSchema(file=xsd_file) parser = objectify.makeparser(schema=schema) objectify.fromstring(some_xml_string, parser) return None except XMLSyntaxError as xml_error: return six.text_type(xml_error)
def xml_validator(some_xml_string, xsd_file): try: schema = etree.XMLSchema(file=xsd_file) parser = objectify.makeparser(schema=schema) objectify.fromstring(some_xml_string, parser) except XMLSyntaxError: return False return True
def __init__(self): # Patch XSD provided by QuakeML website to fit our needs xsd_path = os.path.join(os.path.dirname(__file__), 'QuakeML-BED-1.2.xsd') xsd_root = etree.parse(xsd_path).getroot() self.xsd_root = self.__patch_xsd(xsd_root) xsd_stringio = StringIO(etree.tostring(self.xsd_root)) self.parser = objectify.makeparser(schema=etree.XMLSchema(file=xsd_stringio))
def getEventParser(): if PluginRegistry._event_parser is None: # Initialize parser schema_root = etree.XML(PluginRegistry.getEventSchema()) schema = etree.XMLSchema(schema_root) PluginRegistry._event_parser = objectify.makeparser(schema=schema) return PluginRegistry._event_parser
def read_schema(self, file_name): '''Read xsd file and create from it schema used to verify responses. Return parser object.''' xsd_file_path = os.path.join(self.xsd_dir, file_name) f = open(xsd_file_path, 'rb') schema = etree.XMLSchema(file=f) parser = objectify.makeparser(schema=schema) return parser
def download_artist_similarities(): from lxml import objectify import operator from sqlalchemy.sql import func import urllib import urllib2 from themyutils.misc import retry from bs4 import BeautifulSoup import requests artists = set(map(operator.itemgetter(0), db.session.query(Artist.name).\ filter(Artist.id.in_(db.session.query(UserArtist.artist_id).\ filter(UserArtist.user_id.in_([6, 11]), UserArtist.scrobbles > 250))))) for artist in artists: """ artist_1 = db.session.query(Artist).filter(Artist.name == artist).first() url = b"http://www.last.fm/ru/music/%s" % urllib.quote_plus(artist.encode("utf-8"), b"").replace(b"%2B", b"%252B") for i, li in enumerate(BeautifulSoup(requests.get(url + b"/+similar").text).select("li.large-grid-wrapper")): name = li.select("a.link-block-target")[0].text.strip() artist_2 = db.session.query(Artist).filter(Artist.name == name).first() if artist_2: a1, a2 = tuple(sorted([artist_1, artist_2], key=lambda artist: artist.name.lower())) if db.session.query(func.count(ArtistSimilarity.id)).\ filter(ArtistSimilarity.artist_1 == a1, ArtistSimilarity.artist_2 == a2).\ scalar() == 0: similarity = ArtistSimilarity() similarity.artist_1 = a1 similarity.artist_2 = a2 similarity.match = 1 - i / 100 print(artist_1.name, artist_2.name, similarity.match) db.session.add(similarity) db.session.commit() """ #""" xml = retry(lambda: objectify.fromstring( urllib2.urlopen("http://ws.audioscrobbler.com/2.0/?" + urllib.urlencode(dict(method="artist.getSimilar", api_key=app.config["LAST_FM_API_KEY"], artist=artist.encode("utf-8")))).read(), objectify.makeparser(encoding="utf-8", recover=True) ), max_tries=5, exceptions=((urllib2.HTTPError, lambda e: e.code not in [400]),)) for match in xml.similarartists.iter("artist"): artist_2 = db.session.query(Artist).filter(Artist.name == unicode(match.name)).first() if artist_2: a1, a2 = tuple(sorted([artist_1, artist_2], key=lambda artist: artist.name.lower())) if db.session.query(func.count(ArtistSimilarity.id)).\ filter(ArtistSimilarity.artist_1 == a1, ArtistSimilarity.artist_2 == a2).\ scalar() == 0: similarity = ArtistSimilarity() similarity.artist_1 = a1 similarity.artist_2 = a2 similarity.match = float(match.match) db.session.add(similarity) db.session.commit()
def parse_xml(self): """Importar el contingut de l'xml""" self.check_fpos(self.f_xsd) schema = etree.XMLSchema(file=self.f_xsd) parser = objectify.makeparser(schema=schema) try: self.obj = objectify.fromstring(self.str_xml, parser) except: raise except_f1('Error', _('Document invàlid'))
def __init__(self): local = '<?xml version="1.0"?>\ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">' local += MPEGTS_TSPidSchemaGlobal + MPEGTS_TSParamsSchemaGlobal + MPEGTS_ElementSchemaGlobal local += '</xs:schema>' schema = etree.XMLSchema(objectify.fromstring(local)) ## @brief Brief description of parser self.parser = objectify.makeparser(schema=schema)
def validate_xml_string_against_xsd(xml_string, xsd_file): try: schema = etree.XMLSchema(file=xsd_file) parser = objectify.makeparser(schema=schema) objectify.fromstring(xml_string, parser) logger.info("XSD Pass!") except Exception as e: logger.info("XSD Fail!") BuiltIn().fail(e)
def xml_validator(some_xml_string, xsd_file=xsdFile): try: schema = etree.XMLSchema(file=xsd_file) parser = objectify.makeparser(schema=schema) objectify.fromstring(some_xml_string, parser) print "XML Validado" except XMLSyntaxError: print "XML invalido de acordo com XSD" pass
def read_from_string(input_string, dt=0.1): # Parse XML using CommonRoad schema schema = etree.XMLSchema(file=open( os.path.dirname(os.path.abspath(__file__)) + "/XML_commonRoad_XSD.xsd", "rb")) parser = objectify.makeparser(schema=schema, encoding='utf-8') root = objectify.fromstring(input_string, parser=parser) # Create scenario scenario = Scenario(dt=dt, benchmark_id=root.attrib['benchmarkID']) for lanelet in root.iterchildren('lanelet'): left_vertices = [] right_vertices = [] for pt in lanelet.leftBound.getchildren(): left_vertices.append(np.array([float(pt.x), float(pt.y)])) for pt in lanelet.rightBound.getchildren(): right_vertices.append(np.array([float(pt.x), float(pt.y)])) center_vertices = [ (l + r) / 2 for (l, r) in zip(left_vertices, right_vertices) ] scenario.lanelet_network.add_lanelet( Lanelet(left_vertices=np.array( [np.array([x, y]) for x, y in left_vertices]), center_vertices=np.array( [np.array([x, y]) for x, y in center_vertices]), right_vertices=np.array( [np.array([x, y]) for x, y in right_vertices]), lanelet_id=int(lanelet.attrib['id']), predecessor=[ int(el.attrib['ref']) for el in lanelet.iterchildren(tag='predecessor') ], successor=[ int(el.attrib['ref']) for el in lanelet.iterchildren(tag='successor') ], adjacent_left=int(lanelet.adjacentLeft.attrib['ref']) if lanelet.find('adjacentLeft') is not None else None, adjacent_left_same_direction=lanelet.adjacentLeft. attrib['drivingDir'] == "same" if lanelet.find('adjacentLeft') is not None else None, adjacent_right=int(lanelet.adjacentRight.attrib['ref']) if lanelet.find('adjacentRight') is not None else None, adjacent_right_same_direction=lanelet.adjacentRight. attrib['drivingDir'] == "same" if lanelet.find('adjacentRight') is not None else None)) return scenario
def verify(self): schema_file = self._get_schema() # pylint: disable=maybe-no-member try: schema = etree.XMLSchema(file=schema_file) parser = objectify.makeparser(schema=schema) objectify.fromstring(self.file_data, parser) except XMLSyntaxError as ex: excep_str = "XML file % s does not validate against schema %s\n" % (self.file_name, schema_file) excep_str += "ERROR %s \n" % str(ex) raise Exception(excep_str)
def initParser(schemaFile): if not os.path.exists(schemaFile): log.error("Could not find schema. There should be a file named '"+schemaFile+"' in the script directory") sys.exit(1) try: messageSchema = etree.XMLSchema(file=open(schemaFile, "r")) return objectify.makeparser(schema = messageSchema) except etree.XMLSyntaxError as error: log.error("Could not parse schema. Message: " + error.msg + ", File: " + schemaFile) sys.exit(1)
def get_objectified_xml(self): xsd_filename = XML_TYPE_XSD_MAPPINGS[self.cap_xml_type] with open(os.path.join(CAPLIBRARY_PATH, xsd_filename)) as f: doc = etree.parse(f) schema = etree.XMLSchema(doc) try: parser = objectify.makeparser(schema=schema, recover=self.recover, remove_blank_text=True) a = objectify.fromstring(self.xml, parser) except etree.XMLSyntaxError, e: raise Exception("Error objectifying XML")
def fromstring(text, schema=None): """Parses a KML text string This function parses a KML text string and optionally validates it against a provided schema object""" if schema: parser = objectify.makeparser(schema=schema.schema) return objectify.fromstring(text, parser=parser) else: return objectify.fromstring(text)
def xml_validator(some_xml_string, xsd_file='/path/to/my_schema_file.xsd'): try: schema = etree.XMLSchema(file=xsd_file) parser = objectify.makeparser(schema=schema) objectify.fromstring(some_xml_string, parser) print "YEAH!, my xml file has validated" except XMLSyntaxError: #handle exception here print "Oh NO!, my xml file does not validate" pass
def fromstring(text, schema=None): """Parses a KML text string This function parses a KML text string and optionally validates it against a provided schema object""" if schema: parser = objectify.makeparser(schema = schema.schema) return objectify.fromstring(text, parser=parser) else: return objectify.fromstring(text)
def update_events(): artists = set([user_artist.artist.name for user_artist in db.session.query(UserArtist).\ filter(UserArtist.scrobbles >= 100, UserArtist.user_id.in_([6, 11]))]) try: for artist in artists: db_artist = db.session.query(Artist).filter(Artist.name == artist).one() page = 1 pages = -1 while pages == -1 or page <= pages: logger.debug("Opening %s's page %d of %d", artist, page, pages) xml = retry(lambda: objectify.fromstring( urllib2.urlopen("http://ws.audioscrobbler.com/2.0/?" + urllib.urlencode(dict(method="artist.getPastEvents", api_key=app.config["LAST_FM_API_KEY"], artist=artist.encode("utf-8"), page=page))).read(), objectify.makeparser(encoding="utf-8", recover=True) ), max_tries=5, exceptions=((urllib2.HTTPError, lambda e: e.code not in [400]),), logger=logger) if pages == -1: pages = int(xml.events.get("totalPages")) found = False for event in xml.events.iter("event"): if not hasattr(event, "venue"): continue db_event = db.session.query(Event).get(int(event.id)) if db_event: if db_artist in db_event.artists: found = True break else: db_event.artists.append(db_artist) else: db_event = Event() db_event.id = int(event.id) db_event.title = unicode(event.title) db_event.datetime = dateutil.parser.parse(unicode(event.startDate)) db_event.url = unicode(event.url) db_event.city = unicode(event.venue.location.city) db_event.country = unicode(event.venue.location.country) db_event.artists.append(db_artist) db.session.add(db_event) if found: break page = page + 1 db.session.commit() except urllib2.HTTPError: pass
def load_xml(self,file_name): if(self.schema == None): xml_parser = etree.XMLParser(schema=self.schema) self.xml_tree = etree.parse(file_name,parser=xml_parser) else: parser = objectify.makeparser(schema = self.schema) objectify.set_default_parser(parser) pprint.pprint(objectify.getRegisteredTypes()) #return self.xml_tree = objectify.parse(file_name,parser=parser)
def __init__(self, _id, what=None, user=None): schema = etree.XMLSchema(file=resource_filename("gosa.backend", "data/workflow.xsd")) parser = objectify.makeparser(schema=schema) self.env = Environment.getInstance() self.uuid = _id self.dn = self.env.base self._path = self.env.config.get("core.workflow_path", "/var/lib/gosa/workflows") self._xml_root = objectify.parse(os.path.join(self._path, _id, "workflow.xml"), parser).getroot() self.__attribute = {key: None for key in self.get_attributes()}
def __init__(self, element_class, namespace=None, schema=None): self.element_class = element_class lookup = etree.ElementNamespaceClassLookup() namespace = lookup.get_namespace(namespace) namespace[None] = self.element_class namespace.update({cls.__name__.lower(): cls for cls in all_subclasses(self.element_class)}) self.parser = objectify.makeparser(remove_comments=True, no_network=True) self.parser.set_element_class_lookup(lookup) self.schema = etree.RelaxNG(etree.parse(schema))
def parse_xml(self, xml): self._xml = xml # prepare schema from django.conf import settings import os xsd_path = os.path.join(settings.CURRENT_APP_DIR, 'tests/schema/TestScripts.xsd') with file(xsd_path) as f: xsd = f.read() schema_root = etree.fromstring(xsd) schema = etree.XMLSchema(schema_root) parser = objectify.makeparser(schema = schema) dom = objectify.fromstring(xml, parser) self._dom = dom self.script_index = ScriptIndex(dom.get('Scheme'), dom.get('ScriptID')) test = dom.Test self.description = get_mixed_content(test.Description) self.name = test.get('Name') self.mode = test.get('Mode', 'Learning') from datetime import timedelta self._time_scale = test.get('TimeScale', 'Minute') arg = self.TIME_SCALE_TYPES[self._time_scale] self._time_limit = test.get('LimitOnScript') self.time_limit = timedelta(**{arg: int(self._time_limit)}) # TODO: randomize groups and frames support, etc groups = SortedDict() for group in test.TestGroup: gid = group.get('GroupID') frames = SortedDict() for item in group.FrameIndex: frame_index = FrameIndex(item.get('Scheme'), item.get('FrameID')) gi = item.TestGroupItem giid = gi.get('GroupItemID') weight = int(gi.get('Weight', 1)) frames[giid] = {'index': frame_index, 'weight': weight} groups[gid] = frames self.groups = groups # TODO: multiple rules support try: import decimal decimal.getcontext().prec = 2 threshold = decimal.Decimal(test.TestRules.TestRule.get('Percent')) except AttributeError: threshold = 0 self.rating_threshold = threshold
def _parse_internal(source, parse_func, schema=None, parser_options=None): _parser_options = { 'strip_cdata' : False, } if parser_options: _parser_options.update(parser_options) if schema: _parser_options['schema'] = schema.schema parser = objectify.makeparser(**_parser_options) return parse_func(source, parser=parser)
def __init__(self): """ Constructor """ schema = etree.XMLSchema(file=SCHEMA) parser = objectify.makeparser(schema = schema) jcml_file = codecs.open(FILENAME, mode='r', encoding='utf-8') jcml = jcml_file.read() a = objectify.fromstring(jcml, parser)