Python lxml.objectify.parse() Examples

The following are 9 code examples of lxml.objectify.parse(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module lxml.objectify , or try the search function .
Example #1
Source File: profile.py    From openconnect-sso with GNU General Public License v3.0 6 votes vote down vote up
def _get_profiles_from_one_file(path):
    logger.info("Loading profiles from file", path=path.name)

    with path.open() as f:
        xml = objectify.parse(f)

    hostentries = xml.xpath(
        "//enc:AnyConnectProfile/enc:ServerList/enc:HostEntry", namespaces=ns
    )

    profiles = []
    for entry in hostentries:
        profiles.append(
            HostProfile(
                name=entry.HostName,
                address=entry.HostAddress,
                user_group=entry.UserGroup,
            )
        )

    logger.debug("AnyConnect profiles parsed", path=path.name, profiles=profiles)
    return profiles 
Example #2
Source File: connector.py    From dataiku-contrib with Apache License 2.0 5 votes vote down vote up
def generate_rows(self, dataset_schema=None, dataset_partitioning=None,
                            partition_id=None, records_limit = -1):
        """
        The main reading method.

        Returns a generator over the rows of the dataset (or partition)
        Each yielded row must be a dictionary, indexed by column name.

        The dataset schema and partitioning are given for information purpose.
        """
        limit_mode = False
        if records_limit != -1 or self.test_mode:
            limit_mode = True
        ### We hard force limit to 100 because of the time required for parsing ...

        if not self.all_years and partition_id not in self.list_partitions(None):
            raise ValueError("Unexpected partition id: '%s' - expected one of %s" % (partition_id, ",".join(self.list_partitions(None))))

        count = 0
        for (url, filename, year) in self.files(partition_id):
            print filename
            fullname = self.get_filename(url, filename)
            if not fullname:
                continue
            if limit_mode and count > 100:
                break
            for doc in extract_xml_strings(fullname):
                count = count + 1
                if count % 1000 == 0:
                    print "Patents : parsed", count, " lines"
                if limit_mode and count > 100:
                    break
                emptyDict = {}
                from lxml import objectify
                from StringIO import StringIO
                o = objectify.parse(StringIO(doc))
                iterNodes(o.getroot(), emptyDict)
                s = json.dumps(emptyDict)
                yield { "patent" : s, "year" : year[0], "filename" : filename} 
Example #3
Source File: base.py    From testplan with Apache License 2.0 5 votes vote down vote up
def get_test_context(self):
        """
        Run the shell command generated by `list_command` in a subprocess,
        parse and return the stdout generated via `parse_test_context`.

        :return: Result returned by `parse_test_context`.
        :rtype: ``list`` of ``list``
        """
        cmd = self.list_command()  # pylint: disable=assignment-from-none
        if cmd is None:
            return [(self._DEFAULT_SUITE_NAME, ())]

        proc = subprocess_popen(
            cmd,
            cwd=self.cfg.proc_cwd,
            env=self.cfg.proc_env,
            stdout=subprocess.PIPE,
        )

        test_list_output = proc.communicate()[0]

        # with python3, stdout is bytes so need to decode.
        if not isinstance(test_list_output, six.string_types):
            test_list_output = test_list_output.decode(sys.stdout.encoding)

        return self.parse_test_context(test_list_output) 
Example #4
Source File: base.py    From testplan with Apache License 2.0 5 votes vote down vote up
def read_test_data(self):
        """
        Parse `report.xml` generated by the 3rd party testing tool and return
        the root node.

        You can override this if the test is generating a JSON file and you
        need custom logic to parse its contents for example.

        :return: Root node of parsed raw test data
        :rtype: ``xml.etree.Element``
        """
        with self.result.report.logged_exceptions(), open(
            self.report_path
        ) as report_file:
            return objectify.parse(report_file).getroot() 
Example #5
Source File: sml_import.py    From openmoves with MIT License 5 votes vote down vote up
def sml_import(xmlfile, user, request_form):
    filename = xmlfile.name
    tree = objectify.parse(xmlfile).getroot()
    move = parse_move(tree)
    move.source = os.path.abspath(filename)
    move.import_module = __name__
    device = parse_device(tree)
    persistent_device = Device.query.filter_by(serial_number=device.serial_number).scalar()
    if persistent_device:
        if not persistent_device.name:
            flash("update device name to '%s'" % device.name)
            persistent_device.name = device.name
        else:
            assert device.name == persistent_device.name
        device = persistent_device
    else:
        db.session.add(device)

    if Move.query.filter_by(user=user, date_time=move.date_time, device=device).scalar():
        flash("%s at %s already exists" % (move.activity, move.date_time), 'warning')
    else:
        move.user = user
        move.device = device
        db.session.add(move)

        samples = tree.DeviceLog.Samples.iterchildren()
        for sample in parse_samples(samples, move):
            db.session.add(sample)

        postprocess_move(move)

        db.session.commit()
        return move 
Example #6
Source File: main.py    From sysmon-config-bypass-finder with GNU General Public License v3.0 5 votes vote down vote up
def _read_config_to_json(sysmon_config):
    parser = etree.XMLParser(remove_comments=True)
    tree = objectify.parse(sysmon_config, parser=parser)
    root = tree.getroot()
    event_filtering = root.find('EventFiltering')

    configuration = []
    for rule in event_filtering.getchildren():
        rule_type = rule.tag
        on_match = rule.get('onmatch')
        single_rule = {
            'rule_type': rule_type,
            'on_match': on_match,
            'conditions': []
        }
        for condition in rule.iterchildren():
            cond_operator = condition.get('condition')
            cond_content = condition.text
            cond_type = condition.tag
            single_rule['conditions'].append({
                'operator': cond_operator,
                'content': cond_content,
                'condition_type': cond_type
            })
        configuration.append(single_rule)
    return configuration 
Example #7
Source File: structures_generator.py    From opcua-modeling-tool with MIT License 5 votes vote down vote up
def make_model_from_file(self, path):
        obj = objectify.parse(path)
        root = obj.getroot()
        self._make_model(root) 
Example #8
Source File: parser.py    From geos with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def parse(fileobject, schema=None):
    """Parses a file object
    
    This functon 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
        return objectify.parse(fileobject) 
Example #9
Source File: gpx_import.py    From openmoves with MIT License 4 votes vote down vote up
def gpx_import(xmlfile, user, request_form):
    # Get users options
    import_options = get_gpx_import_options(request_form)
    if import_options == None:
        return

    filename = xmlfile.filename
    try:
        tree = objectify.parse(xmlfile).getroot()
    except Exception as e:
        flash("Failed to parse the GPX file! %s" % e.msg)
        return

    for namespace in GPX_NAMESPACES.values():
        if tree.tag.startswith(namespace):
            gpx_namespace = namespace
            break
    else:
        flash("Unsupported GPX format version: %s" % tree.tag)
        return

    device = create_device()
    persistent_device = Device.query.filter_by(serial_number=device.serial_number).scalar()
    if persistent_device:
        if not persistent_device.name:
            flash("update device name to '%s'" % device.name)
            persistent_device.name = device.name
        else:
            assert device.name == persistent_device.name
        device = persistent_device
    else:
        db.session.add(device)

    move = create_move()
    move.source = os.path.abspath(filename)
    move.import_module = __name__

    # Parse samples
    all_samples = parse_samples(tree, move, gpx_namespace, import_options)

    derive_move_infos_from_samples(move, all_samples)

    if Move.query.filter_by(user=user, date_time=move.date_time, device=device).scalar():
        flash("%s at %s already exists" % (move.activity, move.date_time), 'warning')
    else:
        move.user = user
        move.device = device
        db.session.add(move)

        for sample in all_samples:
            db.session.add(sample)

        postprocess_move(move)

        db.session.commit()
        return move