Skip to content

Commit

Permalink
PROTON-2844: [Python tooling] Eliminate use of mllib
Browse files Browse the repository at this point in the history
Python has good enough suppport for xml in the standard library
so there is no longer any need to use a home built solution.
  • Loading branch information
astitcher committed Aug 2, 2024
1 parent bbe9a4b commit 7c08f80
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 726 deletions.
21 changes: 11 additions & 10 deletions c/src/encodings.h.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,28 @@
# under the License.
#

import mllib
import optparse
import os
import sys
import xml.etree.ElementTree as ET

ns = {'amqp': 'http://www.amqp.org/schema/amqp.xsd'}
xml = os.path.join(os.path.dirname(__file__), "types.xml")
doc = mllib.xml_parse(xml)
doc = ET.parse(xml).getroot()


print("/* generated from %s */" % xml)
print("#ifndef _PROTON_ENCODINGS_H")
print("#define _PROTON_ENCODINGS_H 1")
print()
print("#define PNE_DESCRIPTOR (0x00)")

for enc in doc.query["amqp/section/type/encoding"]:
name = enc["@name"] or enc.parent["@name"]
# XXX: a bit hacky
if name == "ieee-754":
name = enc.parent["@name"]
types = doc.findall('./amqp:section/amqp:type', ns)
encodings = [(t.attrib['name'], e) for t in types for e in t.findall('./amqp:encoding', ns)]
for parentname, enc in encodings:
name = enc.attrib.get("name")
if not name or name == "ieee-754":
name = parentname
cname = "PNE_" + name.replace("-", "_").upper()
print("#define %s%s(%s)" % (cname, " " * (20 - len(cname)), enc["@code"]))
print("#define %s%s(%s)" % (cname, " " * (20 - len(cname)), enc.attrib["code"]))

print()
print("#endif /* encodings.h */")
14 changes: 7 additions & 7 deletions c/src/protocol.h.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@

for type in TYPES:
fidx = 0
for f in type.query["field"]:
for f in type.findall("amqp:field", ns):
print("#define %s_%s (%s)" % (field_kw(type), field_kw(f), fidx))
fidx += 1
d = f["@default"]
d = f.attrib.get("default")
if d:
ft = ftype(f)
# Don't bother to emit a boolean default that is False
Expand All @@ -51,13 +51,13 @@
idx = 0

for type in TYPES:
desc = type["descriptor"]
name = type["@name"].upper().replace("-", "_")
print("#define %s_SYM (\"%s\")" % (name, desc["@name"]))
hi, lo = [int(x, 0) for x in desc["@code"].split(":")]
desc = type.find("./amqp:descriptor", ns)
name = type.attrib["name"].upper().replace("-", "_")
print("#define %s_SYM (\"%s\")" % (name, desc.attrib["name"]))
hi, lo = [int(x, 0) for x in desc.attrib["code"].split(":")]
code = (hi << 32) + lo
print("#define %s ((uint64_t) %s)" % (name, code))
fields[code] = (type["@name"], [f["@name"] for f in type.query["field"]])
fields[code] = (type.attrib["name"], [f.attrib["name"] for f in type.findall("amqp:field", ns)])
idx += 1

print("""
Expand Down
48 changes: 22 additions & 26 deletions c/src/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,36 @@
# specific language governing permissions and limitations
# under the License.
#
import mllib
import os
import sys
import xml.etree.ElementTree as ET

doc = mllib.xml_parse(os.path.join(os.path.dirname(__file__), "transport.xml"))
mdoc = mllib.xml_parse(os.path.join(os.path.dirname(__file__), "messaging.xml"))
tdoc = mllib.xml_parse(os.path.join(os.path.dirname(__file__), "transactions.xml"))
sdoc = mllib.xml_parse(os.path.join(os.path.dirname(__file__), "security.xml"))
ns = {'amqp': 'http://www.amqp.org/schema/amqp.xsd'}
doc = ET.parse(os.path.join(os.path.dirname(__file__), "transport.xml")).getroot()
mdoc = ET.parse(os.path.join(os.path.dirname(__file__), "messaging.xml")).getroot()
tdoc = ET.parse(os.path.join(os.path.dirname(__file__), "transactions.xml")).getroot()
sdoc = ET.parse(os.path.join(os.path.dirname(__file__), "security.xml")).getroot()


def eq(attr, value):
return lambda nd: nd[attr] == value


TYPEStmp = doc.query["amqp/section/type", eq("@class", "composite")] + \
mdoc.query["amqp/section/type", eq("@class", "composite")] + \
tdoc.query["amqp/section/type", eq("@class", "composite")] + \
sdoc.query["amqp/section/type", eq("@class", "composite")] + \
mdoc.query["amqp/section/type", eq("@provides", "section")]
TYPEStmp = doc.findall("./amqp:section/amqp:type/[@class='composite']", ns) + \
mdoc.findall("./amqp:section/amqp:type/[@class='composite']", ns) + \
tdoc.findall("./amqp:section/amqp:type/[@class='composite']", ns) + \
sdoc.findall("./amqp:section/amqp:type/[@class='composite']", ns) + \
mdoc.findall("./amqp:section/amqp:type/[@provides='section']", ns)
TYPES = []
for ty in TYPEStmp:
if ty not in TYPES:
TYPES.append(ty)
RESTRICTIONS = {}
COMPOSITES = {}

for type in doc.query["amqp/section/type"] + mdoc.query["amqp/section/type"] + \
sdoc.query["amqp/section/type"] + tdoc.query["amqp/section/type"]:
for type in doc.findall("./amqp:section/amqp:type", ns) + mdoc.findall("./amqp:section/amqp:type", ns) + \
sdoc.findall("./amqp:section/amqp:type", ns) + tdoc.findall("./amqp:section/amqp:type", ns):

source = type["@source"]
source = type.attrib["source"]
if source:
RESTRICTIONS[type["@name"]] = source
if type["@class"] == "composite":
COMPOSITES[type["@name"]] = type
RESTRICTIONS[type.attrib["name"]] = source
if type.attrib["class"] == "composite":
COMPOSITES[type.attrib["name"]] = type


def resolve(name):
Expand Down Expand Up @@ -90,24 +86,24 @@ def resolve(name):


def fname(field):
return field["@name"].replace("-", "_")
return field.attrib["name"].replace("-", "_")


def tname(t):
return t["@name"].replace("-", "_")
return t.attrib["name"].replace("-", "_")


def multi(f):
return f["@multiple"] == "true"
return f.attrib.get("multiple") == "true"


def ftype(field):
if multi(field):
return "list"
elif field["@type"] in COMPOSITES:
elif field.attrib["type"] in COMPOSITES:
return "box"
else:
return resolve(field["@type"]).replace("-", "_")
return resolve(field.attrib["type"]).replace("-", "_")


def fconstruct(field, expr):
Expand Down
76 changes: 0 additions & 76 deletions tools/python/mllib/__init__.py

This file was deleted.

Loading

0 comments on commit 7c08f80

Please sign in to comment.