def filter_changes(changes, keys_in_grammar):
keys_for_filtering = []
for key in keys_in_grammar:
# print("testing: {}".format(key))
if "_" in key:
prefix, postfix = key.rsplit("_",1)
if postfix in ("on","off"):
keys_for_filtering.append(prefix)
continue
keys_for_filtering.append(key)
keys_for_filtering = set(keys_for_filtering)
# print("KEYS FOR FILTERING: {}".format(keys_for_filtering))
for x in [x for x in changes.keys() if x not in keys_for_filtering]:
changes.pop(x)
# returns a forest where each option is a different parse. the parses have the same format as trees (but or-nodes now only have one option selected).
python类parse()的实例源码
def test_DomainManagerApplicationLifecycle(self):
self.assertNotEqual(self._domMgr, None)
self.assertEqual(len(self._domMgr._get_applicationFactories()), 0)
self.assertEqual(len(self._domMgr._get_applications()), 0)
# This filename isn't in compliance with SCA, but it is necessary for OSSIE
self._domMgr.installApplication("/waveforms/CommandWrapper/CommandWrapper.sad.xml")
self.assertEqual(len(self._domMgr._get_applicationFactories()), 1)
self.assertEqual(len(self._domMgr._get_applications()), 0)
appFact = self._domMgr._get_applicationFactories()[0]
dom = minidom.parse(os.path.join(scatest.getSdrPath(), "dom/waveforms/CommandWrapper/CommandWrapper.sad.xml"))
expectedId = dom.getElementsByTagName("softwareassembly")[0].getAttribute("id")
providedId = appFact._get_identifier()
self.assertEqual(providedId, expectedId, msg="Violation of SR:155 and/or SR:156")
expectedName = dom.getElementsByTagName("softwareassembly")[0].getAttribute("name")
providedName = appFact._get_name()
self.assertEqual(providedName, expectedName, msg="Violation of SR:153")
self._domMgr.uninstallApplication(providedId)
self.assertEqual(len(self._domMgr._get_applicationFactories()), 0)
self.assertEqual(len(self._domMgr._get_applications()), 0)
def _dopost(method, auth=False, **params):
#uncomment to check you aren't killing the flickr server
#print "***** do post %s" % method
params = _prepare_params(params)
url = '%s%s/?api_key=%s%s'% \
(HOST, API, API_KEY, _get_auth_url_suffix(method, auth, params))
# There's no reason this can't be str(urlencode(params)). I just wanted to
# have it the same as the rest.
payload = '%s' % (urlencode(params))
#another useful debug print statement
if debug:
print "_dopost url", url
print "_dopost payload", payload
return _get_data(minidom.parse(urlopen(url, payload)))
def _parseSVNEntries(self, entriesFile):
"""
Given a readable file object which represents a .svn/entries
file, return the revision as a string. If the file cannot be
parsed, return the string "Unknown".
"""
try:
from xml.dom.minidom import parse
doc = parse(entriesFile).documentElement
for node in doc.childNodes:
if hasattr(node, 'getAttribute'):
rev = node.getAttribute('revision')
if rev is not None:
return rev.encode('ascii')
except:
return "Unknown"
def read(self):
basicsatxml = minidom.parse(self.filename)
for sat in basicsatxml.firstChild.childNodes:
if sat.nodeType == sat.ELEMENT_NODE and sat.localName == "sat":
print sat.localName
satname = str(sat.getAttribute("name"))
satpos = str(sat.getAttribute("position"))
self.addSat(satname, satpos)
for transponder in sat.childNodes:
if transponder.nodeType == transponder.ELEMENT_NODE and transponder.localName == "transponder":
parameters = {}
paramlist = ["frequency", "symbol_rate", "polarization", "fec", "system", "modulation", "tsid", "onid"]
for param in paramlist:
entry = str(transponder.getAttribute(param))
if entry != "":
parameters[param] = entry
if len(parameters.keys()) > 1:
self.addTransponder(satpos, parameters)
print self.transponderlist
backuprestore.py 文件源码
项目:script.skin.helper.skinbackup
作者: marcelveldt
项目源码
文件源码
阅读 18
收藏 0
点赞 0
评论 0
def backup_skinshortcuts_properties(propertiesfile, dest_path):
'''parse skinshortcuts properties file and translate images'''
# look for any backgrounds and translate them
propfile = xbmcvfs.File(propertiesfile)
data = propfile.read()
propfile.close()
allprops = eval(data) if data else []
for count, prop in enumerate(allprops):
if prop[2] == "background":
background = prop[3] if prop[3] else ""
defaultid = prop[1]
if background.endswith(".jpg") or background.endswith(".png") or background.endswith(".gif"):
background = get_clean_image(background)
extension = background.split(".")[-1]
newthumb = os.path.join(dest_path, "%s-background-%s.%s" %
(xbmc.getSkinDir(), normalize_string(defaultid), extension))
newthumb_vfs = "special://profile/addon_data/script.skinshortcuts/%s-background-%s.%s" % (
xbmc.getSkinDir(), normalize_string(defaultid), extension)
if xbmcvfs.exists(background):
copy_file(background, newthumb)
allprops[count] = [prop[0], prop[1], prop[2], newthumb_vfs]
# write updated properties file
propfile = xbmcvfs.File(propertiesfile, "w")
propfile.write(repr(allprops))
propfile.close()
def getTargets(self, path):
targets = list()
# Recursively proceesses all nmap XML files from all specified folders
if len(path) > 0:
# Obtains all nmap XML files from each folder
p = path[0] + '*.xml'
if (len(glob(p)) == 0):
self.printMsg(5, "[!] [Error] There's no xml files in " + p[:-5])
else:
for f in glob(p):
self.printMsg(3, "Processing " + f.split(self.separator)[-1] + " ...")
dom = parse(f)
nmaprun = dom.documentElement
# For each host in nmap XML file
for node in nmaprun.getElementsByTagName('host'):
# Extracts IP addresses from all hosts with status = "up"
if node.getElementsByTagName('status')[0].getAttribute('state') == "up":
targets.append(node.getElementsByTagName('address')[0].getAttribute('addr'))
dom.unlink()
del path[0]
targets.extend(self.getTargets(path))
return targets
def getPorts(self, path):
ports = list()
# Recursively proceesses all nmap XML files from all specified folders
if len(path) > 0:
# Obtains all nmap XML files from each folder
p = path[0] + '*.xml'
if (len(glob(p)) == 0):
self.printMsg(5, "[!] [Error] There's no xml files in " + p[:-5])
else:
for f in glob(p):
self.printMsg(3, "Processing " + f.split(self.separator)[-1] + " ...")
dom = parse(f)
nmaprun = dom.documentElement
# For each host in nmap XML file
for node in nmaprun.getElementsByTagName('host'):
# Validate sif host is up & has ports node
if node.getElementsByTagName('status')[0].getAttribute('state') == "up" and node.getElementsByTagName('ports'):
# For each port in port node extracts port id if state is "open"
for port in node.getElementsByTagName('ports')[0].getElementsByTagName('port'):
if port.getElementsByTagName('state')[0].getAttribute('state') == "open":
ports.append(port.getAttribute('portid'))
dom.unlink()
del path[0]
ports.extend(self.getPorts(path))
return sorted(set(ports))
def convert_and_parse_xml(src_model_fname):
dst_model_fname = os.path.basename(src_model_fname).split('.')[0] + '.xml.mdl'
with open(dst_model_fname, 'wb') as wfile:
wfile.write('<MODEL>\n')
with open(src_model_fname, 'rb') as rfile:
for line in rfile.readlines():
newline = line
if '<CNT>' in line:
newline = line.strip() + '</CNT>'
elif '<MEAN>' in line:
newline = line.strip() + '</MEAN>'
elif pn_re.findall(line):
newline = pn_re.sub(r'@ \2 \3 \4 \5 @',line)
wfile.write(newline.strip() + os.linesep)
wfile.write('</MODEL>\n')
xmldoc = minidom.parse(dst_model_fname)
os.remove(dst_model_fname)
return xmldoc
def prepare_loader_manifest(target_dir_smali, target_package, loader_class):
manifest = minidom.parse(os.path.join(target_dir_smali, ANDROID_MANIFEST))
#Comprobamos los permisos destino. De no existir los damos de alta
permissions = minidom.parse(LOADER_PERMISSIONS)
for child_permission in permissions.getElementsByTagName("uses-permission"):
permission = child_permission.attributes['android:name'].value
if permission not in manifest.toxml():
manifest.getElementsByTagName("manifest")[0].appendChild(child_permission)
#Agregamos receiver
receiver = minidom.parse(LOADER_RECEIVER)
receiver.getElementsByTagName("receiver")[0].attributes['android:name'].value = target_package + "." + loader_class
manifest.getElementsByTagName("application")[0].appendChild(receiver.getElementsByTagName("receiver")[0])
#Guardamos
fo = open(os.path.join(target_dir_smali, ANDROID_MANIFEST), "wt")
manifest.writexml(fo)
fo.close()
def _PatchedManifest(manifest_path):
"""Patches an Android manifest to always include the 'tools' namespace
declaration, as it is not propagated by the manifest merger from the SDK.
See https://issuetracker.google.com/issues/63411481
"""
doc = minidom.parse(manifest_path)
manifests = doc.getElementsByTagName('manifest')
assert len(manifests) == 1
manifest = manifests[0]
manifest.setAttribute('xmlns:%s' % TOOLS_NAMESPACE_PREFIX, TOOLS_NAMESPACE)
tmp_prefix = os.path.basename(manifest_path)
with tempfile.NamedTemporaryFile(prefix=tmp_prefix) as patched_manifest:
doc.writexml(patched_manifest)
patched_manifest.flush()
yield patched_manifest.name
def _ParseConfigFile(config_path):
print 'Parsing %s' % config_path
issues_dict = {}
dom = minidom.parse(config_path)
for issue in dom.getElementsByTagName('issue'):
issue_id = issue.attributes['id'].value
severity = issue.getAttribute('severity')
path_elements = (
p.attributes.get('path')
for p in issue.getElementsByTagName('ignore'))
paths = set(p.value for p in path_elements if p)
regexp_elements = (
p.attributes.get('regexp')
for p in issue.getElementsByTagName('ignore'))
regexps = set(r.value for r in regexp_elements if r)
issues_dict[issue_id] = _Issue(severity, paths, regexps)
return issues_dict
def _ParseAndMergeResultFile(result_path, issues_dict):
print 'Parsing and merging %s' % result_path
dom = minidom.parse(result_path)
for issue in dom.getElementsByTagName('issue'):
issue_id = issue.attributes['id'].value
severity = issue.attributes['severity'].value
path = issue.getElementsByTagName('location')[0].attributes['file'].value
# Strip temporary file path.
path = re.sub(_TMP_DIR_RE, '', path)
# Escape Java inner class name separator and suppress with regex instead
# of path. Doesn't use re.escape() as it is a bit too aggressive and
# escapes '_', causing trouble with PRODUCT_DIR.
regexp = path.replace('$', r'\$')
if issue_id not in issues_dict:
issues_dict[issue_id] = _Issue(severity, set(), set())
issues_dict[issue_id].regexps.add(regexp)
def getChildTextbyParentAttribute (datafile, pnode, patt, pattval, cnode):
"""
Seraches XML file for the parent node with a specific value. Finds the child node and returns
its text
datafile = xml file searched
pnode = parent node
patt = parent node attribute
patval = parent node attribute value
cnode = child node
"""
tree = ElementTree.parse(datafile)
root = tree.getroot()
value = False
for node in root.findall(pnode):
attribute = node.get(patt)
if attribute == pattval:
cnode = node.find(cnode)
if cnode is not None:
value = cnode.text
else:
return None
break
return value
def getChildTextbyParentTag (datafile, pnode, cnode):
"""
Seraches XML file for the first parent. Finds the child node and returns its text
datafile = xml file searched
pnode = parent node
cnode = child node
"""
value = False
tree = ElementTree.parse(datafile)
root = tree.getroot()
node = root.find(pnode)
if node is not None:
child = node.find(cnode)
if child is not None:
value = child.text
return value
else:
# print_info("could not find cnode under the given pnode")
return value
else:
# print_info("could not find pnode in the provided file")
return value
def getChildAttributebyParentTag (datafile, pnode, cnode, cattrib):
"""Find the attribute in child node by traversing through the parent node
in the given file
datafile = xml file searched
pnode = parent node
cnode = child node
cattrob = child node attrib
"""
tree = ElementTree.parse(datafile)
root = tree.getroot()
node = root.find(pnode)
if node is not None:
child = node.find(cnode)
if child is not None:
value = child.get(cattrib)
return value
else:
# print_info("could not find cnode under the given pnode")
return False
else:
# print_info("could not find pnode in the provided file")
return False
def verifyParentandChildrenMatch (datafile, pnode, cnode, cvalue, rnode, rvalue):
"""
Searches XML file for the parent node. Finds the 1st child node and checks its value
if value is a match, then search for second child and check if its value matches
datafile = xml file searched
pnode = parent node
cnode = child node
cvalue = child node value
rnode = reference node
rvalue = refernce node value
"""
tree = ElementTree.parse(datafile)
root = tree.getroot()
status = False
for node in root.findall(pnode):
value = node.find(cnode).text
if value == cvalue:
if node.find(rnode) is not None:
cnodev = node.find(rnode).text
# print_debug("-D- cnodev: '%s', rvalue : '%s'" % (cnodev, rvalue))
if cnodev == rvalue:
# print_debug("-D- BREAK END METHOD verifyParentandChildrenMatch_Status '%s'" % status)
return True
return status
def getElementsListWithTagAttribValueMatch(datafile, tag, attrib, value):
"""
This method takes an xml document as input and finds all the sub elements (parent/children)
containing specified tag and an attribute with the specified value.
Returns a list of matching elements.
Arguments:
datafile = input xml file to be parsed.
tag = tag value of the sub-element(parent/child) to be searched for.
attrib = attribute name for the sub-element with above given tag should have.
value = attribute value that the sub-element with above given tag, attribute should have.
"""
element_list = []
root = ElementTree.parse(datafile).getroot()
for element in root.iterfind(".//%s[@%s='%s']" % (tag, attrib, value)):
element_list.append(element)
return element_list
def getElementWithTagAttribValueMatch(start, tag, attrib, value):
"""
When start is an xml datafile, it finds the root and first element with:
tag, attrib, value.
Or when it's an xml element, it finds the first child element with:
tag, attrib, value.
If there is not a match, it returns False.
"""
node = False
if isinstance(start, (file, str)):
# check if file exist here
if file_Utils.fileExists(start):
node = ElementTree.parse(start).getroot()
else:
print_warning('The file={0} is not found.'.format(start))
elif isinstance(start, ElementTree.Element):
node = start
if node is not False and node is not None:
elementName = ".//%s[@%s='%s']" % (tag, attrib, value)
element = node.find(elementName)
else:
element = node
return element
def getConfigElementTextWithSpecificXpath(datafile, xpath):
"""
This method takes an xml document as input and finds the first sub element (parent/children)
containing specified xpath which should be a filepath to a netconf config file
Returns the element text attribute
Arguments:
parent = parent element
xpath = a valid xml path value as supported by python, refer https://docs.python.org/2/library/xml.etree.elementtree.html
"""
root = ElementTree.parse(datafile).getroot()
elem1 = root.find(xpath).text
elem2_root = ElementTree.parse(elem1)
elem2 = elem2_root.find('config')
elem2_string = ElementTree.tostring(elem2)
return elem2_string
def getChildElementWithSpecificXpath(start, xpath):
"""
This method takes a xml file or parent element as input and finds the first child
containing specified xpath
Returns the child element.
Arguments:
start = xml file or parent element
xpath = a valid xml path value as supported by python, refer https://docs.python.org/2/library/xml.etree.elementtree.html
"""
node = False
if isinstance(start, (file, str)):
# check if file exist here
if file_Utils.fileExists(start):
node = ElementTree.parse(start).getroot()
else:
print_warning('The file={0} is not found.'.format(start))
elif isinstance(start, ElementTree.Element):
node = start
if node is not False or node is not None:
element = node.find(xpath)
else:
element = False
return element
def del_tags_from_xml(xml, tag_list=[]):
"""
It deletes the tags either by their names or xpath
Arguments:
1.xml: It takes xml file path or xml string as input
2.tag_list: It contains list of tags which needs to be removed
Returns:
It returns xml string
"""
if os.path.exists(xml):
tree = ElementTree.parse(xml)
root = tree.getroot()
else:
root = ElementTree.fromstring(xml)
for tag in tag_list:
if 'xpath=' in tag:
tag = tag.strip('xpath=')
req_tags = getChildElementsListWithSpecificXpath(root, tag)
else:
req_tags = getChildElementsListWithSpecificXpath(root, ".//{0}".format(tag))
recursive_delete_among_children(root, req_tags)
xml_string = ElementTree.tostring(root, encoding='utf-8', method='xml')
return xml_string
def convert_xml_to_list_of_dict(file_name):
"""
Takes xml file path as input and
converts to list of dictionaries
Arguments:
file_name : It takes xml file path as input
Returns:
list_of_dict: list of dictionaries where keys
are tag names and values are respective text of the tag.
"""
tree = ElementTree.parse(file_name)
root = tree.getroot()
list_of_dict = []
for child in root:
subchild_dict = OrderedDict()
for subchild in child:
subchild_dict[subchild.tag] = subchild.text
list_of_dict.append(subchild_dict)
return list_of_dict
#2016/06/22 ymizugaki add begin
def parse():
global xmlDefault
global xmlPresets
global default
global presets
# Parse default values
default = parseNode(xmlDefault)
# Parse preset values
for setting in xmlPresets:
presets.append(parseNode(setting))
return '{FINISHED}'
# Takes a node and parses it for data. Relies on that setting.xml has
# a valid format as specified by the DTD.
# For some reason minidom places an empty child node for every other node.
def _dopost(method, auth=False, **params):
#uncomment to check you aren't killing the flickr server
#print "***** do post %s" % method
params = _prepare_params(params)
url = '%s%s/?api_key=%s%s'% \
(HOST, API, API_KEY, _get_auth_url_suffix(method, auth, params))
# There's no reason this can't be str(urlencode(params)). I just wanted to
# have it the same as the rest.
payload = '%s' % (urlencode(params))
#another useful debug print statement
if debug:
print "_dopost url", url
print "_dopost payload", payload
return _get_data(minidom.parse(urlopen(url, payload)))
def combineparse2sent(sent, parse):
""" Combine constitent parse into sent
"""
parse = parse.split()
tokenlist = [token.word for token in sent.tokenlist]
parselist, tidx = [""] * len(tokenlist), 0
while parse:
item = parse.pop(0)
parselist[tidx] += (" " + item)
partialparse = parselist[tidx].replace(' ', '')
word = tokenlist[tidx].replace(' ', '')
# print word, partialparse
if (word + ')') in partialparse:
tidx += 1
# Attach to sent
for (tidx, token) in enumerate(sent.tokenlist):
item = parselist[tidx]
sent.tokenlist[tidx].partialparse = item
return sent
def _PatchedManifest(manifest_path):
"""Patches an Android manifest to always include the 'tools' namespace
declaration, as it is not propagated by the manifest merger from the SDK.
See https://issuetracker.google.com/issues/63411481
"""
doc = minidom.parse(manifest_path)
manifests = doc.getElementsByTagName('manifest')
assert len(manifests) == 1
manifest = manifests[0]
manifest.setAttribute('xmlns:%s' % TOOLS_NAMESPACE_PREFIX, TOOLS_NAMESPACE)
tmp_prefix = os.path.basename(manifest_path)
with tempfile.NamedTemporaryFile(prefix=tmp_prefix) as patched_manifest:
doc.writexml(patched_manifest)
patched_manifest.flush()
yield patched_manifest.name
def _ParseConfigFile(config_path):
print 'Parsing %s' % config_path
issues_dict = {}
dom = minidom.parse(config_path)
for issue in dom.getElementsByTagName('issue'):
issue_id = issue.attributes['id'].value
severity = issue.getAttribute('severity')
path_elements = (
p.attributes.get('path')
for p in issue.getElementsByTagName('ignore'))
paths = set(p.value for p in path_elements if p)
regexp_elements = (
p.attributes.get('regexp')
for p in issue.getElementsByTagName('ignore'))
regexps = set(r.value for r in regexp_elements if r)
issues_dict[issue_id] = _Issue(severity, paths, regexps)
return issues_dict
def _ParseAndMergeResultFile(result_path, issues_dict):
print 'Parsing and merging %s' % result_path
dom = minidom.parse(result_path)
for issue in dom.getElementsByTagName('issue'):
issue_id = issue.attributes['id'].value
severity = issue.attributes['severity'].value
path = issue.getElementsByTagName('location')[0].attributes['file'].value
# Strip temporary file path.
path = re.sub(_TMP_DIR_RE, '', path)
# Escape Java inner class name separator and suppress with regex instead
# of path. Doesn't use re.escape() as it is a bit too aggressive and
# escapes '_', causing trouble with PRODUCT_DIR.
regexp = path.replace('$', r'\$')
if issue_id not in issues_dict:
issues_dict[issue_id] = _Issue(severity, set(), set())
issues_dict[issue_id].regexps.add(regexp)
def read(self):
basicsatxml = minidom.parse(self.filename)
for sat in basicsatxml.firstChild.childNodes:
if sat.nodeType == sat.ELEMENT_NODE and sat.localName == "sat":
print sat.localName
satname = str(sat.getAttribute("name"))
satpos = str(sat.getAttribute("position"))
self.addSat(satname, satpos)
for transponder in sat.childNodes:
if transponder.nodeType == transponder.ELEMENT_NODE and transponder.localName == "transponder":
parameters = {}
paramlist = ["frequency", "symbol_rate", "polarization", "fec", "system", "modulation", "tsid", "onid"]
for param in paramlist:
entry = str(transponder.getAttribute(param))
if entry != "":
parameters[param] = entry
if len(parameters.keys()) > 1:
self.addTransponder(satpos, parameters)
print self.transponderlist