本文整理汇总了Python中xml.sax.make_parser函数的典型用法代码示例。如果您正苦于以下问题:Python make_parser函数的具体用法?Python make_parser怎么用?Python make_parser使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了make_parser函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: tree_from_stream
def tree_from_stream(stream,
norm_sp=1, ext_ges=0, ext_pes=0, include_comment=1,
encoding='UTF-8', html=0):
"""
create internal tree from xml stream (open file or IOString)
if norm_sp = 1, normalize space and new line
"""
from xml.sax import make_parser, SAXNotRecognizedException
from xml.sax.handler import feature_namespaces, feature_external_ges, \
feature_external_pes, property_lexical_handler
from parser import SaxHandler
handler = SaxHandler(norm_sp, include_comment, encoding)
if html:
parser = make_parser(["xml.sax.drivers2.drv_sgmlop_html"])
else:
parser = make_parser()
# do not perform Namespace processing
parser.setFeature(feature_namespaces, 0)
# do not include any external entities
try:
parser.setFeature(feature_external_ges, ext_ges)
#xml.sax._exceptions.
except SAXNotRecognizedException:
print 'Unable to set feature external ges'
try:
parser.setFeature(feature_external_pes, ext_pes)
#xml.sax._exceptions.
except SAXNotRecognizedException:
print 'Unable to set feature external pes'
# add lexical handler for comments, entities, dtd and cdata
parser.setProperty(property_lexical_handler, handler)
parser.setContentHandler(handler)
parser.parse(stream)
return handler.get_tree()
示例2: StartWork
def StartWork(self):
StatusManager.StatusManager.pushServer=self
req_GetClientId = ConnectStrings.GetString_ServerHeader()+'?'+ConnectStrings.GetString_GetClientId()
print req_GetClientId
getClientIdHandler=GetClientIdHandler.GetClientIdHandler()
parser=make_parser()
parser.setContentHandler(getClientIdHandler)
parser.parse(req_GetClientId)
print StatusManager.StatusManager.clientId
req_AddStream=ConnectStrings.GetString_ServerHeader()+'?'+ConnectStrings.GetString_AddToStream()
print req_AddStream
addToStreamHandler=AddToStreamHandler.AddToStreamHandler()
parser2=make_parser()
parser2.setContentHandler(addToStreamHandler)
url=req_AddStream
StatusManager.StatusManager.stream=urllib2.urlopen(url)
tagList=[]
str=''
while 1:
char=StatusManager.StatusManager.stream.read(1)
if char=='<':
nextChar=StatusManager.StatusManager.stream.read(1)
while nextChar!='>':
str+=nextChar
nextChar=StatusManager.StatusManager.stream.read(1)
XmlProcess.ProcessNode(str)
str=''
示例3: upload_annotations_file
def upload_annotations_file(request, project_id=None):
if not request.user.is_superuser:
return HttpResponseForbidden("Error: You must be an administrator to use this form")
if not request.FILES.has_key('fileToUpload') or not request.FILES['fileToUpload']:
return projectEdit(request,project_id=project_id,error="Please select a file to upload (supported formats are .zip and .xml).")
f = request.FILES['fileToUpload']
if (not f.name.endswith('.xml')) and (not f.name.endswith('.zip')):
return projectEdit(request,project_id=project_id,error="Please select a file to upload (supported formats are .zip and .xml).")
if fnmatch.fnmatch(f.name, '*.xml'):
parser = make_parser()
curHandler = XmlImportHandler(project_id)
parser.setContentHandler(curHandler)
data = ""
for chunk in f.chunks():
data += chunk
parser.feed(data)
if fnmatch.fnmatch(f.name, '*.zip'):
zipdata = ""
for chunk in f.chunks():
zipdata += chunk
zip = zipfile.ZipFile(StringIO.StringIO(zipdata))
for file in zip.namelist():
parser = make_parser()
curHandler = XmlImportHandler(project_id)
parser.setContentHandler(curHandler)
#pdb.set_trace()
if file.find("__MACOSX/") == -1:
parser.feed(zip.read(file))
zip.close()
return projectEdit(request, project_id=project_id)
示例4: get_objects_recursive
def get_objects_recursive(self, objtype, ids=[], recursive=False):
"""
Recursively get all osm objects that are listed in the ids.
If recursive=False, then you get only the objects that are directly
referenced in relations.
If recursive=True, then you get all hierarchically referenced
from the relations.
"""
relationids = set([])
wayids = set([])
nodeids = set([])
relationdata, waydata, nodedata = '','',''
if objtype == 'node':
nodeids = set(ids)
elif objtype == 'way':
wayids = set(ids)
elif objtype == 'relation':
relationids = set(ids)
else:
return ""
if recursive:
recursions = 100 # maximum recursion level
else:
recursions = 1 # only get all direct members
loaded_relationids = set([])
while relationids:
r_data = self.get_objects('relation', relationids)
relationdata += '\n' + r_data
if not recursions:
break
else:
recursions -= 1
parser = make_parser()
osm_handler = SubobjectHandler()
parser.setContentHandler(osm_handler)
parseString(OSMHEAD + r_data + OSMTAIL, osm_handler)
nodeids |= osm_handler.nodes
wayids |= osm_handler.ways
loaded_relationids |= relationids
relationids = osm_handler.relations - loaded_relationids
if wayids:
waydata = self.get_objects('way', wayids)
parser = make_parser()
osm_handler = SubobjectHandler()
parser.setContentHandler(osm_handler)
parseString(OSMHEAD + waydata + OSMTAIL, osm_handler)
nodeids |= osm_handler.nodes
if nodeids:
nodedata = self.get_objects('node', nodeids)
return nodedata + waydata + relationdata
示例5: __init__
def __init__(self):
"""
Create a new Definition Object
"""
self.map = {}
self.patches = {}
handler = DefinitionHandler()
sax.make_parser()
for path in [AAPATH, NAPATH]:
defpath = getDatFile(path)
if defpath == "":
raise PDBInternalError("%s not found!" % path)
acidFile = open(defpath)
sax.parseString(acidFile.read(), handler)
acidFile.close()
self.map.update(handler.map)
# Now handle patches
defpath = getDatFile(PATCHPATH)
if defpath == "":
raise PDBInternalError("%s not found!" % PATCHPATH)
handler.map = {}
patchFile = open(defpath)
sax.parseString(patchFile.read(), handler)
patchFile.close()
# Apply specific patches to the reference object, allowing users
# to specify protonation states in the PDB file
for patch in handler.patches:
if patch.newname != "":
# Find all residues matching applyto
resnames = self.map.keys()
for name in resnames:
regexp = re.compile(patch.applyto).match(name)
if not regexp:
continue
newname = patch.newname.replace("*", name)
self.addPatch(patch, name, newname)
# Either way, make sure the main patch name is available
self.addPatch(patch, patch.applyto, patch.name)
示例6: readDotGraphStyles
def readDotGraphStyles(self):
p = make_parser()
h = rocks.profile.RollHandler()
map = {}
for file in os.listdir('.'):
tokens = os.path.splitext(file)
if len(tokens) != 2:
continue
name = tokens[0]
ext = tokens[1]
tokens = string.split(name, '-')
if len(tokens) < 2:
continue
prefix = tokens[0]
if prefix == 'roll' and \
ext == '.xml' and \
os.path.isfile(file):
fin = open(file, 'r')
p.setContentHandler(h)
p.parse(fin)
fin.close()
r = h.getRollName()
map[r] = rocks.util.Struct()
map[r].edgeColor = h.getEdgeColor()
map[r].nodeColor = h.getNodeColor()
map[r].nodeShape = h.getNodeShape()
return map
示例7: load_costs
def load_costs(self, dumpfile, iteration, weight):
# load costs from dumpfile and update memory according to weight and iteration
if weight <= 0:
sys.stderr.write("Skipped loading of costs because the weight was %s but should have been > 0\n" % weight)
return
assert(weight > 0)
if self.iteration == None and iteration != 0:
print "Warning: continuing with empty memory"
# update memory weights. memory is a weighted average across all runs
self.memory_factor = self.memory_weight / (self.memory_weight + weight)
self.memory_weight += weight
self.iteration = iteration
self.errors = []
# mark all edges as unseen
for edges in self.intervals.itervalues():
for edgeMemory in edges.itervalues():
edgeMemory.seen = False
# parse costs
self.num_loaded = 0
parser = make_parser()
parser.setContentHandler(self)
parser.parse(dumpfile)
# decay costs of unseen edges
self.num_decayed = 0
for edges in self.intervals.itervalues():
for edgeMemory in edges.itervalues():
if edgeMemory.decay_unseen(self.memory_factor):
self.num_decayed += 1
示例8: import_data
def import_data(self, metadata, output):
"Imports places data from OpenStreetMap"
old_etag = metadata.get('etag', '')
request = AnyMethodRequest(self._url, method='HEAD')
response = urllib2.urlopen(request)
new_etag = response.headers['ETag'][1:-1]
self.output = output
if False and new_etag == old_etag:
output.write('OSM data not updated. Not updating.\n')
return
p = subprocess.Popen([self.SHELL_CMD % self._url], shell=True,
stdin=subprocess.PIPE, stdout=subprocess.PIPE, close_fds=True)
parser = make_parser()
parser.setContentHandler(OSMHandler(self._get_source(),
self._get_entity_types(),
lambda tags, type_list=None: self._find_types(tags, self._osm_tags if type_list is None else type_list),
output,
self._lat_north,
self._lat_south,
self._lon_west,
self._lon_east))
parser.parse(p.stdout)
for lang_code, lang_name in settings.LANGUAGES:
with override(lang_code):
self.disambiguate_titles(self._get_source())
return {
'etag': new_etag,
}
示例9: readficheroconfig
def readficheroconfig():
parser = make_parser()
ParserDTD = uaserver.ParserDTD()
parser.setContentHandler(ParserDTD)
parser.parse(open(FICHEROCONFIG))
TAGS = ParserDTD.get_tags()
return TAGS
示例10: test_5027_1
def test_5027_1(self):
# The xml prefix (as in xml:lang below) is reserved and bound by
# definition to http://www.w3.org/XML/1998/namespace. XMLGenerator had
# a bug whereby a KeyError is thrown because this namespace is missing
# from a dictionary.
#
# This test demonstrates the bug by parsing a document.
test_xml = StringIO(
'<?xml version="1.0"?>'
'<a:g1 xmlns:a="http://example.com/ns">'
'<a:g2 xml:lang="en">Hello</a:g2>'
'</a:g1>')
parser = make_parser()
parser.setFeature(feature_namespaces, True)
result = StringIO()
gen = XMLGenerator(result)
parser.setContentHandler(gen)
parser.parse(test_xml)
self.assertEqual(result.getvalue(),
start + (
'<a:g1 xmlns:a="http://example.com/ns">'
'<a:g2 xml:lang="en">Hello</a:g2>'
'</a:g1>'))
示例11: read
def read(self, filename):
self.publications = []
self.authors = []
self.author_idx = {}
self.min_year = None
self.max_year = None
handler = DocumentHandler(self)
parser = make_parser()
parser.setContentHandler(handler)
infile = open(filename, "r")
valid = True
try:
parser.parse(infile)
except SAXException as e:
valid = False
print "Error reading file (" + e.getMessage() + ")"
infile.close()
for p in self.publications:
if self.min_year == None or p.year < self.min_year:
self.min_year = p.year
if self.max_year == None or p.year > self.max_year:
self.max_year = p.year
self.authors_graph = self._build_authors_graph()
return valid
示例12: parse
def parse(opts):
"""
Entry point for XML Schema parsing into an OME Model.
"""
# The following two statements are required to "prime" the generateDS
# code and ensure we have reasonable namespace support.
filenames = opts.args
namespace = opts.namespace
logging.debug("Namespace: %s" % namespace)
set_type_constants(namespace)
generateDS.generateDS.XsdNameSpace = namespace
logging.debug("Type map: %s" % opts.lang.type_map)
parser = sax.make_parser()
ch = XschemaHandler()
parser.setContentHandler(ch)
for filename in filenames:
parser.parse(filename)
root = ch.getRoot()
if root is None:
raise ModelProcessingError(
"No model objects found, have you set the correct namespace?")
root.annotate()
return OMEModel.process(ch, opts)
示例13: __init__
def __init__(self, arquivo_mapa):
parser = sax.make_parser()
self.tmxhandler = TMXHandler(arquivo_mapa)
parser.setContentHandler(self.tmxhandler)
print 'Carregando o mapa %s ...' % (arquivo_mapa,)
parser.parse(arquivo_mapa)
print 'Pronto !'
示例14: __init__
def __init__(self, parent, config):
C3Object.__init__(self, parent, config)
self.parser = make_parser()
self.inputSource = SaxInput()
self.errorHandler = ErrorHandler()
self.parser.setErrorHandler(self.errorHandler)
self.parser.setContentHandler(self)
示例15: scan
def scan(self):
"""Parse the *.qrc* files"""
if not has_xml:
Logs.error('no xml support was found, the rcc dependencies will be incomplete!')
return ([], [])
parser = make_parser()
curHandler = XMLHandler()
parser.setContentHandler(curHandler)
fi = open(self.inputs[0].abspath(), 'r')
try:
parser.parse(fi)
finally:
fi.close()
nodes = []
names = []
root = self.inputs[0].parent
for x in curHandler.files:
nd = root.find_resource(x)
if nd:
nodes.append(nd)
else:
names.append(x)
return (nodes, names)