| 1 |
# |
| 2 |
# Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
# or more contributor license agreements. See the NOTICE file |
| 4 |
# distributed with this work for additional information |
| 5 |
# regarding copyright ownership. The ASF licenses this file |
| 6 |
# to you under the Apache License, Version 2.0 (the |
| 7 |
# "License"); you may not use this file except in compliance |
| 8 |
# with the License. You may obtain a copy of the License at |
| 9 |
# |
| 10 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
# |
| 12 |
# Unless required by applicable law or agreed to in writing, |
| 13 |
# software distributed under the License is distributed on an |
| 14 |
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
# KIND, either express or implied. See the License for the |
| 16 |
# specific language governing permissions and limitations |
| 17 |
# under the License. |
| 18 |
# |
| 19 |
import mllib |
| 20 |
import os |
| 21 |
import sys |
| 22 |
|
| 23 |
doc = mllib.xml_parse(os.path.join(os.path.dirname(__file__), "transport.xml")) |
| 24 |
mdoc = mllib.xml_parse(os.path.join(os.path.dirname(__file__), "messaging.xml")) |
| 25 |
tdoc = mllib.xml_parse(os.path.join(os.path.dirname(__file__), "transactions.xml")) |
| 26 |
sdoc = mllib.xml_parse(os.path.join(os.path.dirname(__file__), "security.xml")) |
| 27 |
|
| 28 |
|
| 29 |
def eq(attr, value): |
| 30 |
return lambda nd: nd[attr] == value |
| 31 |
|
| 32 |
|
| 33 |
TYPEStmp = doc.query["amqp/section/type", eq("@class", "composite")] + \ |
| 34 |
mdoc.query["amqp/section/type", eq("@class", "composite")] + \ |
| 35 |
tdoc.query["amqp/section/type", eq("@class", "composite")] + \ |
| 36 |
sdoc.query["amqp/section/type", eq("@class", "composite")] + \ |
| 37 |
mdoc.query["amqp/section/type", eq("@provides", "section")] |
| 38 |
TYPES = [] |
| 39 |
for ty in TYPEStmp: |
| 40 |
if ty not in TYPES: |
| 41 |
TYPES.append(ty) |
| 42 |
RESTRICTIONS = {} |
| 43 |
COMPOSITES = {} |
| 44 |
|
| 45 |
for type in doc.query["amqp/section/type"] + mdoc.query["amqp/section/type"] + \ |
| 46 |
sdoc.query["amqp/section/type"] + tdoc.query["amqp/section/type"]: |
| 47 |
|
| 48 |
source = type["@source"] |
| 49 |
if source: |
| 50 |
RESTRICTIONS[type["@name"]] = source |
| 51 |
if type["@class"] == "composite": |
| 52 |
COMPOSITES[type["@name"]] = type |
| 53 |
|
| 54 |
|
| 55 |
def resolve(name): |
| 56 |
if name in RESTRICTIONS: |
| 57 |
return resolve(RESTRICTIONS[name]) |
| 58 |
else: |
| 59 |
return name |
| 60 |
|
| 61 |
|
| 62 |
TYPEMAP = { |
| 63 |
"boolean": ("bool", "", ""), |
| 64 |
"binary": ("pn_binary_t", "*", ""), |
| 65 |
"string": ("wchar_t", "*", ""), |
| 66 |
"symbol": ("char", "*", ""), |
| 67 |
"ubyte": ("uint8_t", "", ""), |
| 68 |
"ushort": ("uint16_t", "", ""), |
| 69 |
"uint": ("uint32_t", "", ""), |
| 70 |
"ulong": ("uint64_t", "", ""), |
| 71 |
"timestamp": ("uint64_t", "", ""), |
| 72 |
"list": ("pn_list_t", "*", ""), |
| 73 |
"map": ("pn_map_t", "*", ""), |
| 74 |
"box": ("pn_box_t", "*", ""), |
| 75 |
"*": ("pn_object_t", "*", "") |
| 76 |
} |
| 77 |
|
| 78 |
CONSTRUCTORS = { |
| 79 |
"boolean": "boolean", |
| 80 |
"string": "string", |
| 81 |
"symbol": "symbol", |
| 82 |
"ubyte": "ubyte", |
| 83 |
"ushort": "ushort", |
| 84 |
"uint": "uint", |
| 85 |
"ulong": "ulong", |
| 86 |
"timestamp": "ulong" |
| 87 |
} |
| 88 |
|
| 89 |
NULLABLE = set(["string", "symbol"]) |
| 90 |
|
| 91 |
|
| 92 |
def fname(field): |
| 93 |
return field["@name"].replace("-", "_") |
| 94 |
|
| 95 |
|
| 96 |
def tname(t): |
| 97 |
return t["@name"].replace("-", "_") |
| 98 |
|
| 99 |
|
| 100 |
def multi(f): |
| 101 |
return f["@multiple"] == "true" |
| 102 |
|
| 103 |
|
| 104 |
def ftype(field): |
| 105 |
if multi(field): |
| 106 |
return "list" |
| 107 |
elif field["@type"] in COMPOSITES: |
| 108 |
return "box" |
| 109 |
else: |
| 110 |
return resolve(field["@type"]).replace("-", "_") |
| 111 |
|
| 112 |
|
| 113 |
def fconstruct(field, expr): |
| 114 |
type = ftype(field) |
| 115 |
if type in CONSTRUCTORS: |
| 116 |
result = "pn_%s(mem, %s)" % (CONSTRUCTORS[type], expr) |
| 117 |
if type in NULLABLE: |
| 118 |
result = "%s ? %s : NULL" % (expr, result) |
| 119 |
else: |
| 120 |
result = expr |
| 121 |
if multi(field): |
| 122 |
result = "pn_box(mem, pn_boolean(mem, true), %s)" % result |
| 123 |
return result |
| 124 |
|
| 125 |
|
| 126 |
def declaration(field): |
| 127 |
name = fname(field) |
| 128 |
type = ftype(field) |
| 129 |
t, pre, post = TYPEMAP.get(type, (type, "", "")) |
| 130 |
return t, "%s%s%s" % (pre, name, post) |
| 131 |
|
| 132 |
|
| 133 |
def field_kw(field): |
| 134 |
return fname(field).upper() |