本文整理匯總了Python中lxml.objectify.parse方法的典型用法代碼示例。如果您正苦於以下問題:Python objectify.parse方法的具體用法?Python objectify.parse怎麽用?Python objectify.parse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類lxml.objectify
的用法示例。
在下文中一共展示了objectify.parse方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _get_profiles_from_one_file
# 需要導入模塊: from lxml import objectify [as 別名]
# 或者: from lxml.objectify import parse [as 別名]
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
示例2: generate_rows
# 需要導入模塊: from lxml import objectify [as 別名]
# 或者: from lxml.objectify import parse [as 別名]
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}
示例3: get_test_context
# 需要導入模塊: from lxml import objectify [as 別名]
# 或者: from lxml.objectify import parse [as 別名]
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)
示例4: read_test_data
# 需要導入模塊: from lxml import objectify [as 別名]
# 或者: from lxml.objectify import parse [as 別名]
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()
示例5: sml_import
# 需要導入模塊: from lxml import objectify [as 別名]
# 或者: from lxml.objectify import parse [as 別名]
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
示例6: _read_config_to_json
# 需要導入模塊: from lxml import objectify [as 別名]
# 或者: from lxml.objectify import parse [as 別名]
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
示例7: make_model_from_file
# 需要導入模塊: from lxml import objectify [as 別名]
# 或者: from lxml.objectify import parse [as 別名]
def make_model_from_file(self, path):
obj = objectify.parse(path)
root = obj.getroot()
self._make_model(root)
示例8: parse
# 需要導入模塊: from lxml import objectify [as 別名]
# 或者: from lxml.objectify import parse [as 別名]
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)
示例9: gpx_import
# 需要導入模塊: from lxml import objectify [as 別名]
# 或者: from lxml.objectify import parse [as 別名]
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