diff options
| author | Rafael H. Schloming <rhs@apache.org> | 2007-07-23 14:36:05 +0000 |
|---|---|---|
| committer | Rafael H. Schloming <rhs@apache.org> | 2007-07-23 14:36:05 +0000 |
| commit | c2c6f451dfecb3671082be6d5c37e8bb03468427 (patch) | |
| tree | b780601566a2ee8df19c6e991f98cd2abe81813d /python/qpid | |
| parent | 09e0292f2be2c7bf4efe69df7254ba17d342eb32 (diff) | |
| download | qpid-python-c2c6f451dfecb3671082be6d5c37e8bb03468427.tar.gz | |
Added a better XML library.
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@558742 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'python/qpid')
| -rw-r--r-- | python/qpid/spec.py | 39 | ||||
| -rw-r--r-- | python/qpid/xmlutil.py | 119 |
2 files changed, 19 insertions, 139 deletions
diff --git a/python/qpid/spec.py b/python/qpid/spec.py index f8e37737e2..88faedd825 100644 --- a/python/qpid/spec.py +++ b/python/qpid/spec.py @@ -29,7 +29,7 @@ class so that the generated code can be reused in a variety of situations. """ -import re, textwrap, new, xmlutil +import re, textwrap, new, mllib class SpecContainer: @@ -275,21 +275,20 @@ class Field(Metadata): self.docs = docs def get_desc(nd): - label = nd.get("@label") + label = nd["@label"] if not label: - label = nd.text + label = nd.text() if label: label = label.strip() return label def get_docs(nd): - return [n.text for n in nd["doc"]] + return [n.text() for n in nd.query["doc"]] def load_fields(nd, l, domains): - for f_nd in nd["field"]: - try: - type = f_nd["@domain"] - except KeyError: + for f_nd in nd.query["field"]: + type = f_nd["@domain"] + if type == None: type = f_nd["@type"] type = pythonize(type) domain = None @@ -300,27 +299,27 @@ def load_fields(nd, l, domains): get_desc(f_nd), get_docs(f_nd))) def load(specfile, *errata): - doc = xmlutil.parse(specfile) - spec_root = doc["amqp"][0] + doc = mllib.xml_parse(specfile) + spec_root = doc["amqp"] spec = Spec(int(spec_root["@major"]), int(spec_root["@minor"]), specfile) - for root in [spec_root] + map(lambda x: xmlutil.parse(x)["amqp"][0], errata): + for root in [spec_root] + map(lambda x: mllib.xml_parse(x)["amqp"], errata): # constants - for nd in root["constant"]: + for nd in root.query["constant"]: const = Constant(spec, pythonize(nd["@name"]), int(nd["@value"]), - nd.get("@class"), get_docs(nd)) + nd["@class"], get_docs(nd)) try: spec.constants.add(const) - except ValueError, e: + except ValueError, e: print "Warning:", e # domains are typedefs - for nd in root["domain"]: + for nd in root.query["domain"]: spec.domains.add(Domain(spec, nd.index(), pythonize(nd["@name"]), pythonize(nd["@type"]), get_desc(nd), get_docs(nd))) # classes - for c_nd in root["class"]: + for c_nd in root.query["class"]: cname = pythonize(c_nd["@name"]) if spec.classes.byname.has_key(cname): klass = spec.classes.byname[cname] @@ -331,16 +330,16 @@ def load(specfile, *errata): added_methods = [] load_fields(c_nd, klass.fields, spec.domains.byname) - for m_nd in c_nd["method"]: + for m_nd in c_nd.query["method"]: mname = pythonize(m_nd["@name"]) if klass.methods.byname.has_key(mname): meth = klass.methods.byname[mname] else: meth = Method(klass, mname, int(m_nd["@index"]), - m_nd.get_bool("@content", False), - [pythonize(nd["@name"]) for nd in m_nd["response"]], - m_nd.get_bool("@synchronous", False), + m_nd["@content"] == "1", + [pythonize(nd["@name"]) for nd in m_nd.query["response"]], + m_nd["@synchronous"] == "1", get_desc(m_nd), get_docs(m_nd)) klass.methods.add(meth) diff --git a/python/qpid/xmlutil.py b/python/qpid/xmlutil.py deleted file mode 100644 index 585516b44f..0000000000 --- a/python/qpid/xmlutil.py +++ /dev/null @@ -1,119 +0,0 @@ -# -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. -# - -""" -XML utilities used by spec.py -""" - -import xml.sax -from xml.sax.handler import ContentHandler - -def parse(file): - doc = Node("root") - xml.sax.parse(file, Builder(doc)) - return doc - -class Node: - - def __init__(self, name, attrs = None, text = None, parent = None): - self.name = name - self.attrs = attrs - self.text = text - self.parent = parent - self.children = [] - if parent != None: - parent.children.append(self) - - def get_bool(self, key, default = False): - v = self.get(key) - if v == None: - return default - else: - return bool(int(v)) - - def index(self): - if self.parent: - return self.parent.children.index(self) - else: - return 0 - - def has(self, key): - try: - result = self[key] - return True - except KeyError: - return False - except IndexError: - return False - - def get(self, key, default = None): - if self.has(key): - return self[key] - else: - return default - - def __getitem__(self, key): - if callable(key): - return filter(key, self.children) - else: - t = key.__class__ - meth = "__get%s__" % t.__name__ - if hasattr(self, meth): - return getattr(self, meth)(key) - else: - raise KeyError(key) - - def __getstr__(self, name): - if name[:1] == "@": - return self.attrs[name[1:]] - else: - return self[lambda nd: nd.name == name] - - def __getint__(self, index): - return self.children[index] - - def __iter__(self): - return iter(self.children) - - def path(self): - if self.parent == None: - return "/%s" % self.name - else: - return "%s/%s" % (self.parent.path(), self.name) - -class Builder(ContentHandler): - - def __init__(self, start = None): - self.node = start - - def __setitem__(self, element, type): - self.types[element] = type - - def startElement(self, name, attrs): - self.node = Node(name, attrs, None, self.node) - - def endElement(self, name): - self.node = self.node.parent - - def characters(self, content): - if self.node.text == None: - self.node.text = content - else: - self.node.text += content - |
