本文整理汇总了Python中defusedxml.ElementTree.ParseError方法的典型用法代码示例。如果您正苦于以下问题:Python ElementTree.ParseError方法的具体用法?Python ElementTree.ParseError怎么用?Python ElementTree.ParseError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类defusedxml.ElementTree
的用法示例。
在下文中一共展示了ElementTree.ParseError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: from_file
# 需要导入模块: from defusedxml import ElementTree [as 别名]
# 或者: from defusedxml.ElementTree import ParseError [as 别名]
def from_file(self, filename="vasprun.xml"):
"""
Parsing vasprun.xml from the working directory
Args:
filename (str): Path to the vasprun file
"""
if not (os.path.isfile(filename)):
raise AssertionError()
try:
self.root = ETree.parse(filename).getroot()
except ParseError:
raise VasprunError(
"The vasprun.xml file is either corrupted or the simulation has failed"
)
self.parse_root_to_dict()
示例2: _decode_data
# 需要导入模块: from defusedxml import ElementTree [as 别名]
# 或者: from defusedxml.ElementTree import ParseError [as 别名]
def _decode_data(self, entry):
"""Decode data field (password or comments)."""
key = entry.get('type', 'password')
key = 'comments' if key == 'note' else key
data = entry.pop('data', '')
if isinstance(data, int):
return key, ''
data = self._decode(data)
if key == 'comments':
if data:
try:
tree = ElementTree.XML(data)
except ElementTree.ParseError:
return key, ''
found = tree.find('.//string')
if found is None:
return key, ''
return key, found.text
return key, ''
return key, data
示例3: backup_progress
# 需要导入模块: from defusedxml import ElementTree [as 别名]
# 或者: from defusedxml.ElementTree import ParseError [as 别名]
def backup_progress(self):
"""Return status of cloud backup as a dict.
Is there a way to get progress for Server version?
"""
epoch_time = int(time.time() * 1000)
if self.deploymentType == "Cloud":
url = (
self._options["server"] + "/rest/obm/1.0/getprogress?_=%i" % epoch_time
)
else:
logging.warning("This functionality is not available in Server version")
return None
r = self._session.get(url, headers=self._options["headers"])
# This is weird. I used to get xml, but now I'm getting json
try:
return json.loads(r.text)
except Exception:
import defusedxml.ElementTree as etree
progress = {}
try:
root = etree.fromstring(r.text)
except etree.ParseError as pe:
logging.warning(
"Unable to find backup info. You probably need to initiate a new backup. %s"
% pe
)
return None
for k in root.keys():
progress[k] = root.get(k)
return progress
示例4: _ParseRecords
# 需要导入模块: from defusedxml import ElementTree [as 别名]
# 或者: from defusedxml.ElementTree import ParseError [as 别名]
def _ParseRecords(self, parser_mediator, evtx_file):
"""Parses Windows XML EventLog (EVTX) records.
Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
evtx_file (pyevt.file): Windows XML EventLog (EVTX) file.
"""
# To handle errors when parsing a Windows XML EventLog (EVTX) file in the
# most granular way the following code iterates over every event record.
# The call to evt_file.get_record() and access to members of evt_record
# should be called within a try-except.
for record_index in range(evtx_file.number_of_records):
if parser_mediator.abort:
break
try:
evtx_record = evtx_file.get_record(record_index)
self._ParseRecord(parser_mediator, record_index, evtx_record)
except (IOError, ElementTree.ParseError) as exception:
parser_mediator.ProduceExtractionWarning(
'unable to parse event record: {0:d} with error: {1!s}'.format(
record_index, exception))
for record_index in range(evtx_file.number_of_recovered_records):
if parser_mediator.abort:
break
try:
evtx_record = evtx_file.get_recovered_record(record_index)
self._ParseRecord(
parser_mediator, record_index, evtx_record, recovered=True)
except IOError as exception:
parser_mediator.ProduceExtractionWarning((
'unable to parse recovered event record: {0:d} with error: '
'{1!s}').format(record_index, exception))
示例5: is_format
# 需要导入模块: from defusedxml import ElementTree [as 别名]
# 或者: from defusedxml.ElementTree import ParseError [as 别名]
def is_format(self):
"""Return True if the file is an XML file."""
try:
self.dom = parse(self.file)
except (ParseError, ExpatError, UnicodeDecodeError):
return False
return True
示例6: parse_xml
# 需要导入模块: from defusedxml import ElementTree [as 别名]
# 或者: from defusedxml.ElementTree import ParseError [as 别名]
def parse_xml(xml_content):
try:
return defused_xml_parse(xml_content)
except ParseError:
raise FeedXMLError('Not a valid XML document')
示例7: run_diff
# 需要导入模块: from defusedxml import ElementTree [as 别名]
# 或者: from defusedxml.ElementTree import ParseError [as 别名]
def run_diff(test_config, diff):
root_directory = get_root_dir()
allowed_test_path = os.path.join(root_directory, diff)
test_path_base = os.path.split(allowed_test_path)[0]
test_name = os.path.split(allowed_test_path)[1]
actual, actual_diff, setup, expected_files, next_path = get_test_file_paths(test_path_base, test_name,
test_config.output_dir)
logging.debug('actual_path: ' + actual)
diff_count_map = {}
for f in expected_files:
logging.debug('expected_path: ' + f)
if os.path.isfile(f) and os.path.isfile(actual):
logging.debug("Diffing " + actual + " and " + f)
actual_xml = None
expected_xml = None
try:
actual_xml = parse(actual).getroot()
except ParseError as e:
logging.debug("Exception parsing actual file: " + actual + " exception: " + str(e))
continue
try:
expected_xml = parse(f).getroot()
except ParseError as e:
logging.debug("Exception parsing expected file: " + f + " exception: " + str(e))
continue
result = TestResult(test_config=test_config)
result.add_test_results(actual_xml, actual)
expected_output = TestResult(test_config=test_config)
expected_output.add_test_results(expected_xml, '')
num_diffs, diff_string = result.diff_test_results(expected_output)
logging.debug(diff_string)
diff_count_map[f] = sum(num_diffs)
for t in diff_count_map:
logging.debug(t + ' Number of differences: ' + str(diff_count_map[t]))
return 0
示例8: main
# 需要导入模块: from defusedxml import ElementTree [as 别名]
# 或者: from defusedxml.ElementTree import ParseError [as 别名]
def main():
all_permissions = [] # list of strings naming each permission used in the dataset
app_permission_map = {} # mapping from android app names to lists of permissions
app_malicious_map = {} # mapping from android app names to 1 or 0 for malware or goodware
root_dir = os.getcwd()
for i, directory in enumerate(['benign_apk', 'malicious_apk']):
os.chdir(directory)
category_root_dir = os.getcwd()
for filename in glob.glob('*.apk'):
print('Processing ' + filename)
try:
os.chdir(filename[:-4])
with open('AndroidManifest.xml') as xml_file:
et = ElementTree.parse(xml_file)
except (ElementTree.ParseError, UnicodeDecodeError, FileNotFoundError):
print('Parsing error encountered for ' + filename)
os.chdir(category_root_dir)
continue
app_name = filename
# make a one-hot bit vector of length 2. 1st bit set if malicious, otherwise 2nd bit
app_malicious_map[app_name] = [1,0] if i else [0,1]
permissions = et.getroot().findall('./uses-permission')
app_permission_map[app_name] = []
for permission in permissions:
try:
permission_name = permission.attrib['{http://schemas.android.com/apk/res/android}name'].upper()
if not permission_name.startswith('ANDROID.PERMISSION'): continue # ignore custom permissions
if permission_name not in all_permissions: all_permissions.append(permission_name)
app_permission_map[app_name].append(permission_name)
except KeyError:
pass
os.chdir(os.pardir)
os.chdir(root_dir)
all_apps = {} # mapping combining app_permission_map and app_malicious_map using bits
for app_name in app_permission_map:
bit_vector = [1 if p in app_permission_map[app_name] else 0 for p in all_permissions]
all_apps[app_name] = {'vector': bit_vector, 'malicious': app_malicious_map[app_name]}
with open('app_permission_vectors.json', 'w') as outfile:
json.dump({'features': all_permissions, 'apps': all_apps}, outfile)
print('Wrote data on ' + str(len(all_permissions)) + ' permissions and ' + str(len(all_apps)) + ' apps to a file.')
示例9: _request_xml
# 需要导入模块: from defusedxml import ElementTree [as 别名]
# 或者: from defusedxml.ElementTree import ParseError [as 别名]
def _request_xml(sport):
"""
Request XML data from scorespro.com
:param sport: sport being played
:type sport: string
:return: XML data
:rtype: string
"""
url = 'http://www.scorespro.com/rss2/live-{}.xml'.format(sport)
try:
r = requests.get(url)
return _load_xml(r.content)
except ET.ParseError:
raise errors.SportError(sport)
示例10: checkPostData
# 需要导入模块: from defusedxml import ElementTree [as 别名]
# 或者: from defusedxml.ElementTree import ParseError [as 别名]
def checkPostData(postrequest):
"""check if postdata is XML"""
postdata = postrequest.decode('utf-8')
try:
return ETdefused.fromstring(postdata)
except ETdefused.ParseError:
app.logger.error('Invalid XML in post request')
return False
示例11: __init__
# 需要导入模块: from defusedxml import ElementTree [as 别名]
# 或者: from defusedxml.ElementTree import ParseError [as 别名]
def __init__(self, path):
self.path = path
self.success = True
self.errors = []
try:
self.tree = parse_xml(path)
except ElementTree.ParseError as ex:
self.tree = None
self.errors.append({
'file': self.path,
'line': ex.position[0],
'error': ex.msg,
})
self.rule_name = None
示例12: async_get_downstream
# 需要导入模块: from defusedxml import ElementTree [as 别名]
# 或者: from defusedxml.ElementTree import ParseError [as 别名]
def async_get_downstream(self):
"""Get the current downstream cable modem state."""
if self.token is None:
await self.async_initialize_token()
self.ds_channels.clear()
raw = await self._async_ws_get_function(CMD_DOWNSTREAM)
try:
xml_root = element_tree.fromstring(raw)
for downstream in xml_root.iter("downstream"):
self.ds_channels.append(
DownstreamChannel(
int(downstream.find("freq").text),
int(downstream.find("pow").text),
downstream.find("mod").text,
downstream.find("chid").text,
float(downstream.find("RxMER").text),
int(downstream.find("PreRs").text),
int(downstream.find("PostRs").text),
downstream.find("IsQamLocked").text == "1",
downstream.find("IsFECLocked").text == "1",
downstream.find("IsMpegLocked").text == "1",
)
)
except (element_tree.ParseError, TypeError):
_LOGGER.warning("Can't read downstream channels from %s", self.host)
self.token = None
raise exceptions.ConnectBoxNoDataAvailable() from None
示例13: async_get_upstream
# 需要导入模块: from defusedxml import ElementTree [as 别名]
# 或者: from defusedxml.ElementTree import ParseError [as 别名]
def async_get_upstream(self):
"""Get the current upstream cable modem state."""
if self.token is None:
await self.async_initialize_token()
self.us_channels.clear()
raw = await self._async_ws_get_function(CMD_UPSTREAM)
try:
xml_root = element_tree.fromstring(raw)
for upstream in xml_root.iter("upstream"):
self.us_channels.append(
UpstreamChannel(
int(upstream.find("freq").text),
int(upstream.find("power").text),
upstream.find("srate").text,
upstream.find("usid").text,
upstream.find("mod").text,
upstream.find("ustype").text,
int(upstream.find("t1Timeouts").text),
int(upstream.find("t2Timeouts").text),
int(upstream.find("t3Timeouts").text),
int(upstream.find("t4Timeouts").text),
upstream.find("channeltype").text,
int(upstream.find("messageType").text),
)
)
except (element_tree.ParseError, TypeError):
_LOGGER.warning("Can't read upstream channels from %s", self.host)
self.token = None
raise exceptions.ConnectBoxNoDataAvailable() from None
示例14: async_get_ipv6_filtering
# 需要导入模块: from defusedxml import ElementTree [as 别名]
# 或者: from defusedxml.ElementTree import ParseError [as 别名]
def async_get_ipv6_filtering(self) -> None:
"""Get the current ipv6 filter (and filters time) rules."""
if self.token is None:
await self.async_initialize_token()
self.ipv6_filters.clear()
self._ipv6_filters_time = None
raw = await self._async_ws_get_function(CMD_GET_IPV6_FILTER_RULE)
try:
xml_root = element_tree.fromstring(raw)
for instance in xml_root.iter("instance"):
self.ipv6_filters.append(
Ipv6FilterInstance(
int(instance.find("idd").text),
instance.find("src_addr").text,
int(instance.find("src_prefix").text),
instance.find("dst_addr").text,
int(instance.find("dst_prefix").text),
int(instance.find("src_sport").text),
int(instance.find("src_eport").text),
int(instance.find("dst_sport").text),
int(instance.find("dst_eport").text),
int(instance.find("protocol").text),
int(instance.find("allow").text),
int(instance.find("enabled").text),
)
)
self._ipv6_filters_time = FiltersTimeMode(
int(xml_root.find("time_mode").text),
_parse_general_time(xml_root),
_parse_daily_time(xml_root)
)
except (element_tree.ParseError, TypeError):
_LOGGER.warning("Can't read IPv6 filter rules from %s", self.host)
self.token = None
raise exceptions.ConnectBoxNoDataAvailable() from None
示例15: parseSafeNetXML
# 需要导入模块: from defusedxml import ElementTree [as 别名]
# 或者: from defusedxml.ElementTree import ParseError [as 别名]
def parseSafeNetXML(xml):
"""
This function parses XML data of a Aladdin/SafeNet XML
file for eToken PASS
It returns a dictionary of
serial : { otpkey , counter, type }
"""
TOKENS = {}
try:
elem_tokencontainer = etree.fromstring(xml)
except etree.ParseError as e:
log.debug(traceback.format_exc())
raise ImportException('Could not parse XML data: {0!s}'.format(e))
if getTagName(elem_tokencontainer) != "Tokens":
raise ImportException("No toplevel element Tokens")
for elem_token in list(elem_tokencontainer):
SERIAL = None
COUNTER = None
HMAC = None
DESCRIPTION = None
if getTagName(elem_token) == "Token":
SERIAL = elem_token.get("serial")
log.debug("Found token with serial {0!s}".format(SERIAL))
for elem_tdata in list(elem_token):
tag = getTagName(elem_tdata)
if "ProductName" == tag:
DESCRIPTION = elem_tdata.text
log.debug("The Token with the serial %s has the "
"productname %s" % (SERIAL, DESCRIPTION))
if "Applications" == tag:
for elem_apps in elem_tdata:
if getTagName(elem_apps) == "Application":
for elem_app in elem_apps:
tag = getTagName(elem_app)
if "Seed" == tag:
HMAC = elem_app.text
if "MovingFactor" == tag:
COUNTER = elem_app.text
if not SERIAL:
log.error("Found token without a serial")
else:
if HMAC:
hashlib = "sha1"
if len(HMAC) == 64:
hashlib = "sha256"
TOKENS[SERIAL] = {'otpkey': HMAC,
'counter': COUNTER,
'type': 'hotp',
'hashlib': hashlib
}
else:
log.error("Found token {0!s} without a element 'Seed'".format(
SERIAL))
return TOKENS