本文整理汇总了Python中lxml.etree.ElementTree类的典型用法代码示例。如果您正苦于以下问题:Python ElementTree类的具体用法?Python ElementTree怎么用?Python ElementTree使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ElementTree类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: generate_tool_conf
def generate_tool_conf(parsed_ctds, tool_conf_destination, galaxy_tool_path, default_category):
# for each category, we keep a list of models corresponding to it
categories_to_tools = dict()
for parsed_ctd in parsed_ctds:
category = strip(parsed_ctd.ctd_model.opt_attribs.get("category", ""))
if not category.strip():
category = default_category
if category not in categories_to_tools:
categories_to_tools[category] = []
categories_to_tools[category].append(utils.get_filename(parsed_ctd.suggested_output_file))
# at this point, we should have a map for all categories->tools
toolbox_node = Element("toolbox")
if galaxy_tool_path is not None and not galaxy_tool_path.strip().endswith("/"):
galaxy_tool_path = galaxy_tool_path.strip() + "/"
if galaxy_tool_path is None:
galaxy_tool_path = ""
for category, file_names in categories_to_tools.iteritems():
section_node = add_child_node(toolbox_node, "section")
section_node.attrib["id"] = "section-id-" + "".join(category.split())
section_node.attrib["name"] = category
for filename in file_names:
tool_node = add_child_node(section_node, "tool")
tool_node.attrib["file"] = galaxy_tool_path + filename
toolconf_tree = ElementTree(toolbox_node)
toolconf_tree.write(open(tool_conf_destination,'w'), encoding="UTF-8", xml_declaration=True, pretty_print=True)
logger.info("Generated Galaxy tool_conf.xml in %s" % tool_conf_destination, 0)
示例2: retrieveMessagesFromFiles
def retrieveMessagesFromFiles(self):
# We read each file and create one message for each file
self.messages = []
self.lineView.get_model().clear()
for file in self.filesToBeImported:
from netzob.Common.ResourcesConfiguration import ResourcesConfiguration
xmlSchemaPath = os.path.join(ResourcesConfiguration.getStaticResources(), "xsds/0.1/common.xsd")
# If we find a version which validates the XML, we parse with the associated function
if not Workspace.isSchemaValidateXML(xmlSchemaPath, file):
logging.error(_("The specified XML file {0} is not valid according to the XSD ({1}).").format(str(file), str(xmlSchemaPath)))
else:
logging.debug(_("XML file valid according to the XSD schema"))
# Parse the XML Document as 0.1 version
tree = ElementTree()
tree.parse(file)
xmlFile = tree.getroot()
for xmlMessage in xmlFile.findall("{" + Project.COMMON_NAMESPACE + "}message"):
message = AbstractMessageFactory.loadFromXML(xmlMessage, Project.COMMON_NAMESPACE, "0.1")
logging.debug(_("XML String data: {0}").format(message.getStringData()))
self.messages.append(message)
self.lineView.get_model().append(None, [str(message.getID()), message.getType(), message.getStringData()])
示例3: _convert_internal
def _convert_internal(parsed_ctds, **kwargs):
# parse all input files into models using CTDopts (via utils)
# the output is a tuple containing the model, output destination, origin file
for parsed_ctd in parsed_ctds:
model = parsed_ctd.ctd_model
origin_file = parsed_ctd.input_file
output_file = parsed_ctd.suggested_output_file
if kwargs["skip_tools"] is not None and model.name in kwargs["skip_tools"]:
logger.info("Skipping tool %s" % model.name, 0)
continue
elif kwargs["required_tools"] is not None and model.name not in kwargs["required_tools"]:
logger.info("Tool %s is not required, skipping it" % model.name, 0)
continue
else:
logger.info("Converting %s (source %s)" % (model.name, utils.get_filename(origin_file)), 0)
tool = create_tool(model)
write_header(tool, model)
create_description(tool, model)
expand_macros(tool, model, **kwargs)
create_command(tool, model, **kwargs)
create_inputs(tool, model, **kwargs)
create_outputs(tool, model, **kwargs)
create_help(tool, model)
# wrap our tool element into a tree to be able to serialize it
tree = ElementTree(tool)
logger.info("Writing to %s" % utils.get_filename(output_file), 1)
tree.write(open(output_file, 'w'), encoding="UTF-8", xml_declaration=True, pretty_print=True)
示例4: scrape
def scrape(self, chamber, session):
for term in self.metadata["terms"]:
if session in term["sessions"]:
year = term["start_year"]
break
self.versions_dict = self._versions_dict(year)
base_bill_url = "http://data.opi.mt.gov/bills/%d/BillHtml/" % year
index_page = ElementTree(lxml.html.fromstring(self.urlopen(base_bill_url)))
bill_urls = []
for bill_anchor in index_page.findall("//a"):
# See 2009 HB 645
if bill_anchor.text.find("govlineveto") == -1:
# House bills start with H, Senate bills start with S
if chamber == "lower" and bill_anchor.text.startswith("H"):
bill_urls.append("%s%s" % (base_bill_url, bill_anchor.text))
elif chamber == "upper" and bill_anchor.text.startswith("S"):
bill_urls.append("%s%s" % (base_bill_url, bill_anchor.text))
for bill_url in bill_urls:
bill = self.parse_bill(bill_url, session, chamber)
if bill:
self.save_bill(bill)
示例5: exportProjectAction
def exportProjectAction(self, widget, data):
chooser = gtk.FileChooserDialog(title=_("Export as (XML)"), action=gtk.FILE_CHOOSER_ACTION_SAVE,
buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK))
res = chooser.run()
if res == gtk.RESPONSE_OK:
fileName = chooser.get_filename()
chooser.destroy()
doCreateFile = False
isFile = os.path.isfile(fileName)
if not isFile:
doCreateFile = True
else:
md = gtk.MessageDialog(None,
gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_QUESTION,
gtk.BUTTONS_OK_CANCEL, _("Are you sure to override the file '{0}'?").format(fileName))
resp = md.run()
md.destroy()
if resp == gtk.RESPONSE_OK:
doCreateFile = True
if doCreateFile:
root = self.netzob.getCurrentProject().generateXMLConfigFile()
tree = ElementTree(root)
tree.write(fileName)
NetzobInfoMessage(_("Project correctly exported to '{0}'").format(fileName))
示例6: getdescendants
def getdescendants(request, code):
params = {}
results = {}
language = request.LANGUAGE_CODE.lower()
if language == 'pt-br':
language = 'pt'
for lang in DECS_LANGS:
params[lang] = urllib.urlencode({
'tree_id': code or '',
'lang': lang,
})
resource = urllib.urlopen(settings.DECS_SERVICE, params[lang])
tree = ElementTree()
tree.parse(resource)
descendants = tree.findall('decsws_response/tree/descendants/term_list[@lang="%s"]/term' % lang)
for d in descendants:
if d.attrib['tree_id'] in results:
results[ d.attrib['tree_id'] ] += ',"%s":"%s"' % (lang,d.text.capitalize())
else:
results[ d.attrib['tree_id'] ] = '"%s":"%s"' % (lang,d.text.capitalize())
json = '[%s]' % ','.join((JSON_MULTILINGUAL_TERM % (id,desc) for desc,id in results.items()))
json_response = json_loads(json)
json_response.sort(key=lambda x: x['fields']['description'][language])
return HttpResponse(json_dumps(json_response), mimetype='application/json')
示例7: getNameOfProject
def getNameOfProject(workspace, projectDirectory):
projectFile = os.path.join(os.path.join(workspace.getPath(), projectDirectory), Project.CONFIGURATION_FILENAME)
# verify we can open and read the file
if projectFile == None:
return None
# is the projectFile is a file
if not os.path.isfile(projectFile):
logging.warn("The specified project's configuration file (" + str(projectFile) + ") is not valid : its not a file.")
return None
# is it readable
if not os.access(projectFile, os.R_OK):
logging.warn("The specified project's configuration file (" + str(projectFile) + ") is not readable.")
return None
# We validate the file given the schemas
for xmlSchemaFile in Project.PROJECT_SCHEMAS.keys():
xmlSchemaPath = os.path.join(ResourcesConfiguration.getStaticResources(), xmlSchemaFile)
# If we find a version which validates the XML, we parse with the associated function
if Project.isSchemaValidateXML(xmlSchemaPath, projectFile):
logging.debug("The file " + str(projectFile) + " validates the project configuration file.")
tree = ElementTree()
tree.parse(projectFile)
xmlProject = tree.getroot()
# Register the namespace
etree.register_namespace('netzob', PROJECT_NAMESPACE)
etree.register_namespace('netzob-common', COMMON_NAMESPACE)
projectName = xmlProject.get('name', 'none')
if projectName != None and projectName != 'none':
return projectName
else:
logging.warn("The project declared in file (" + projectFile + ") is not valid")
return None
示例8: saveConfigFile
def saveConfigFile(self, overrideTraces=[]):
"""This functions allows to save the current (and only)
instance of the Workspace. You can supply a list of traces
that should be written on-disk through the `overrideTraces`
variable. This allows to override specific traces that where
modified.
:param overrideTraces: a list of trace identifiers that should
be written on-disk, even if they already exists.
"""
workspaceFile = os.path.join(self.path, Workspace.CONFIGURATION_FILENAME)
logging.info("Save the config file of the workspace {0} in {1}".format(self.getName(), workspaceFile))
# Register the namespace
etree.register_namespace('netzob', WORKSPACE_NAMESPACE)
etree.register_namespace('netzob-common', COMMON_NAMESPACE)
# Dump the file
root = etree.Element("{" + WORKSPACE_NAMESPACE + "}workspace")
root.set("creation_date", TypeConvertor.pythonDatetime2XSDDatetime(self.getCreationDate()))
root.set("name", str(self.getName()))
xmlWorkspaceConfig = etree.SubElement(root, "{" + WORKSPACE_NAMESPACE + "}configuration")
relTracePath = os.path.relpath(self.getPathOfTraces(), self.path)
xmlTraces = etree.SubElement(xmlWorkspaceConfig, "{" + WORKSPACE_NAMESPACE + "}traces")
xmlTraces.text = str(self.getPathOfTraces())
xmlLogging = etree.SubElement(xmlWorkspaceConfig, "{" + WORKSPACE_NAMESPACE + "}logging")
xmlLogging.text = str(self.getPathOfLogging())
xmlPrototypes = etree.SubElement(xmlWorkspaceConfig, "{" + WORKSPACE_NAMESPACE + "}prototypes")
xmlPrototypes.text = str(self.getPathOfPrototypes())
xmlPrototypes = etree.SubElement(xmlWorkspaceConfig, "{" + WORKSPACE_NAMESPACE + "}enable_bug_reporting")
xmlPrototypes.text = str(self.enableBugReporting).lower()
xmlWorkspaceProjects = etree.SubElement(root, "{" + WORKSPACE_NAMESPACE + "}projects")
for projectPath in self.getProjectsPath():
xmlProject = etree.SubElement(xmlWorkspaceProjects, "{" + WORKSPACE_NAMESPACE + "}project")
xmlProject.set("path", projectPath)
xmlWorkspaceImported = etree.SubElement(root, "{" + WORKSPACE_NAMESPACE + "}traces")
for importedTrace in self.getImportedTraces():
# overrideTraces variable contains the list of
# ImportedTraces that should be overriden. This is useful
# in case of message removal for example.
forceOverride = (importedTrace.id in overrideTraces)
importedTrace.save(xmlWorkspaceImported, WORKSPACE_NAMESPACE, COMMON_NAMESPACE,
os.path.join(self.path, self.getPathOfTraces()), forceOverride)
xmlWorkspaceFunctions = etree.SubElement(root, "{" + WORKSPACE_NAMESPACE + "}functions")
for function in self.getCustomFunctions():
function.save(xmlWorkspaceFunctions, WORKSPACE_NAMESPACE)
tree = ElementTree(root)
tree.write(workspaceFile, pretty_print=True)
示例9: get_chapters
def get_chapters(request):
params = {}
results = {}
language = "pt"
# language = request.LANGUAGE_CODE.lower()
# if language == 'pt-br':
# language = 'pt'
params = urllib.urlencode({"LI": "CAPITULO"})
resource = urllib.urlopen(settings.ICD10_SERVICE, params)
tree = ElementTree()
tree.parse(resource)
terms = tree.findall("cid10ws_response")
data = []
for term in terms:
description = {}
chapter = term.findall("tree/self/term_list/term")[0]
for lang in ICD10_LANGS:
term_trans = term.findall('record_list/record/descriptor_list/descriptor[@lang="%s"]' % lang)[0]
if term_trans.text:
description[lang] = "%s - %s" % (chapter.attrib["chapter"], term_trans.text.strip().capitalize())
data.append({"fields": {"description": description, "label": chapter.attrib["tree_id"]}})
return HttpResponse(json.dumps(data), mimetype="application/json")
示例10: print_predictions
def print_predictions(agency, stops, label=""):
title_index = build_title_index(stops)
url = build_url(agency, stops)
debug("NextBus predictions for %s: %s" % (agency, url))
f = urllib.urlopen(url)
e = ElementTree(file=f)
predictions = e.findall("//predictions")
predictions = filter(lambda el: el.find(".//prediction") is not None, predictions)
predictions.sort(key=lambda el: el.find(".//prediction").get("epochTime"))
for n, p in enumerate(predictions):
routeTag = p.get("routeTag")
stopTag = p.get("stopTag")
title = title_index.get((routeTag, stopTag), False)
if title:
title = "<em>%s</em>" % (title.replace("\n", "<br>"), )
else:
title = p.get("routeTitle")
title = re.sub(r'^Saferide ', '', title)
title = label + title
print "<h2>"+title+"</h2>"
times = p.findall(".//prediction")
print "<ol class='predictions'>"
print '<li>%s</li>' % minutes(times.pop(0).get("minutes"))
for t in times[0:2]:
print '<li>%s</li>' % minutes(t.get("minutes"))
print "</ol>"
示例11: scrape
def scrape(self, chamber, year):
year = int(year)
session = self.getSession(year)
# 2 year terms starting on odd year, so if even number, use the previous odd year
if year < 1999:
raise NoDataForPeriod(year)
if year % 2 == 0:
year -= 1
if year == 1999:
base_bill_url = "http://data.opi.mt.gov/bills/BillHtml/"
else:
base_bill_url = "http://data.opi.mt.gov/bills/%d/BillHtml/" % year
index_page = ElementTree(lxml.html.fromstring(self.urlopen(base_bill_url)))
bill_urls = []
for bill_anchor in index_page.findall("//a"):
# See 2009 HB 645
if bill_anchor.text.find("govlineveto") == -1:
# House bills start with H, Senate bills start with S
if chamber == "lower" and bill_anchor.text.startswith("H"):
bill_urls.append("%s%s" % (base_bill_url, bill_anchor.text))
elif chamber == "upper" and bill_anchor.text.startswith("S"):
bill_urls.append("%s%s" % (base_bill_url, bill_anchor.text))
for bill_url in bill_urls:
bill = self.parse_bill(bill_url, session, chamber)
self.save_bill(bill)
示例12: parse_bill
def parse_bill(self, bill_url, session, chamber):
bill = None
bill_page = ElementTree(lxml.html.fromstring(self.urlopen(bill_url)))
for anchor in bill_page.findall("//a"):
if anchor.text_content().startswith("status of") or anchor.text_content().startswith(
"Detailed Information (status)"
):
status_url = anchor.attrib["href"].replace("\r", "").replace("\n", "")
bill = self.parse_bill_status_page(status_url, bill_url, session, chamber)
elif anchor.text_content().startswith("This bill in WP"):
index_url = anchor.attrib["href"]
index_url = index_url[0 : index_url.rindex("/")]
# this looks weird. See http://data.opi.mt.gov/bills/BillHtml/SB0002.htm for why
index_url = index_url[index_url.rindex("http://") :]
self.add_bill_versions(bill, index_url)
if bill is None:
# No bill was found. Maybe something like HB0790 in the 2005 session?
# We can search for the bill metadata.
page_name = bill_url.split("/")[-1].split(".")[0]
bill_type = page_name[0:2]
bill_number = page_name[2:]
laws_year = metadata["session_details"][session]["years"][0] % 100
status_url = self.search_url_template % (laws_year, bill_type, bill_number)
bill = self.parse_bill_status_page(status_url, bill_url, session, chamber)
return bill
示例13: loadProject_0_1
def loadProject_0_1(projectFile):
# Parse the XML Document as 0.1 version
tree = ElementTree()
tree.parse(projectFile)
xmlProject = tree.getroot()
# Register the namespace
etree.register_namespace('netzob', PROJECT_NAMESPACE)
etree.register_namespace('netzob-common', COMMON_NAMESPACE)
projectID = xmlProject.get('id')
projectName = xmlProject.get('name', 'none')
projectCreationDate = TypeConvertor.xsdDatetime2PythonDatetime(xmlProject.get('creation_date'))
projectPath = xmlProject.get('path')
project = Project(projectID, projectName, projectCreationDate, projectPath)
# Parse the configuration
if xmlProject.find("{" + PROJECT_NAMESPACE + "}configuration") != None:
projectConfiguration = ProjectConfiguration.loadProjectConfiguration(xmlProject.find("{" + PROJECT_NAMESPACE + "}configuration"), PROJECT_NAMESPACE, "0.1")
project.setConfiguration(projectConfiguration)
# Parse the vocabulary
if xmlProject.find("{" + PROJECT_NAMESPACE + "}vocabulary") != None:
projectVocabulary = Vocabulary.loadVocabulary(xmlProject.find("{" + PROJECT_NAMESPACE + "}vocabulary"), PROJECT_NAMESPACE, COMMON_NAMESPACE, "0.1", project)
project.setVocabulary(projectVocabulary)
# Parse the grammar
if xmlProject.find("{" + PROJECT_NAMESPACE + "}grammar") != None:
projectGrammar = Grammar.loadGrammar(xmlProject.find("{" + PROJECT_NAMESPACE + "}grammar"), projectVocabulary, PROJECT_NAMESPACE, "0.1")
if projectGrammar != None:
project.setGrammar(projectGrammar)
return project
示例14: scrape
def scrape(self, chamber, session):
for term in self.metadata['terms']:
if session in term['sessions']:
year = term['start_year']
break
self.versions_dict = self._versions_dict(year)
base_bill_url = 'http://leg.mt.gov/bills/%d/BillHtml/' % year
index_page = ElementTree(lxml.html.fromstring(self.get(base_bill_url).text))
bill_urls = []
for bill_anchor in index_page.findall('//a'):
# See 2009 HB 645
if bill_anchor.text.find("govlineveto") == -1:
# House bills start with H, Senate bills start with S
if chamber == 'lower' and bill_anchor.text.startswith('H'):
bill_urls.append("%s%s" % (base_bill_url, bill_anchor.text))
elif chamber == 'upper' and bill_anchor.text.startswith('S'):
bill_urls.append("%s%s" % (base_bill_url, bill_anchor.text))
for bill_url in bill_urls:
bill = self.parse_bill(bill_url, session, chamber)
if bill:
self.save_bill(bill)
示例15: loadProject_0_1
def loadProject_0_1(projectFile):
# Parse the XML Document as 0.1 version
tree = ElementTree()
tree.parse(projectFile)
xmlProject = tree.getroot()
# Register the namespace
etree.register_namespace("netzob", PROJECT_NAMESPACE)
etree.register_namespace("netzob-common", COMMON_NAMESPACE)
projectID = str(xmlProject.get("id"))
projectName = xmlProject.get("name", "none")
projectCreationDate = TypeConvertor.xsdDatetime2PythonDatetime(xmlProject.get("creation_date"))
projectPath = xmlProject.get("path")
project = Project(projectID, projectName, projectCreationDate, projectPath)
description = xmlProject.get("description")
project.setDescription(description)
# Parse the configuration
if xmlProject.find("{" + PROJECT_NAMESPACE + "}configuration") is not None:
projectConfiguration = ProjectConfiguration.loadProjectConfiguration(
xmlProject.find("{" + PROJECT_NAMESPACE + "}configuration"), PROJECT_NAMESPACE, "0.1"
)
project.setConfiguration(projectConfiguration)
# Parse the vocabulary
if xmlProject.find("{" + PROJECT_NAMESPACE + "}vocabulary") is not None:
projectVocabulary = Vocabulary.loadVocabulary(
xmlProject.find("{" + PROJECT_NAMESPACE + "}vocabulary"),
PROJECT_NAMESPACE,
COMMON_NAMESPACE,
"0.1",
project,
)
project.setVocabulary(projectVocabulary)
# Parse the grammar
if xmlProject.find("{" + PROJECT_NAMESPACE + "}grammar") is not None:
projectGrammar = Grammar.loadGrammar(
xmlProject.find("{" + PROJECT_NAMESPACE + "}grammar"), projectVocabulary, PROJECT_NAMESPACE, "0.1"
)
if projectGrammar is not None:
project.setGrammar(projectGrammar)
# Parse the simulator
if xmlProject.find("{" + PROJECT_NAMESPACE + "}simulator") is not None:
projectSimulator = Simulator.loadSimulator(
xmlProject.find("{" + PROJECT_NAMESPACE + "}simulator"),
PROJECT_NAMESPACE,
"0.1",
project.getGrammar().getAutomata(),
project.getVocabulary(),
)
if projectSimulator is not None:
project.setSimulator(projectSimulator)
return project