summaryrefslogtreecommitdiff
path: root/python/qpid/ops.py
blob: 390552be6de909755901731929e177609ec3ad25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#
# 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.
#
import os, mllib, cPickle as pickle, sys
from util import fill

class Primitive(object):
  pass

class Enum(object):

  # XXX: for backwards compatibility
  def values(cls):
    print >> sys.stderr, "warning, please use .VALUES instead of .values()"
    return cls.VALUES
  # we can't use the backport preprocessor here because this code gets
  # called by setup.py
  values = classmethod(values)

class Field:

  def __init__(self, name, type, default=None):
    self.name = name
    self.type = type
    self.default = default

  def __repr__(self):
    return "%s: %s" % (self.name, self.type)

class Compound(object):

  UNENCODED=[]

  def __init__(self, *args, **kwargs):
    args = list(args)
    for f in self.ARGS:
      if args:
        a = args.pop(0)
      else:
        a = kwargs.pop(f.name, f.default)
      setattr(self, f.name, a)
    if args:
      raise TypeError("%s takes at most %s arguments (%s given))" %
                      (self.__class__.__name__, len(self.ARGS),
                       len(self.ARGS) + len(args)))
    if kwargs:
      raise TypeError("got unexpected keyword argument '%s'" % kwargs.keys()[0])

  def fields(self):
    result = {}
    for f in self.FIELDS:
      result[f.name] = getattr(self, f.name)
    return result

  def args(self):
    result = {}
    for f in self.ARGS:
      result[f.name] = getattr(self, f.name)
    return result

  def __getitem__(self, attr):
    return getattr(self, attr)

  def __setitem__(self, attr, value):
    setattr(self, attr, value)

  def dispatch(self, target, *args):
    handler = "do_%s" % self.NAME
    getattr(target, handler)(self, *args)

  def __repr__(self, extras=()):
    return "%s(%s)" % (self.__class__.__name__,
                       ", ".join(["%s=%r" % (f.name, getattr(self, f.name))
                                  for f in self.ARGS
                                  if getattr(self, f.name) != f.default]))

class Command(Compound):
  UNENCODED=[Field("channel", "uint16", 0),
             Field("id", "sequence-no", None),
             Field("sync", "bit", False),
             Field("headers", None, None),
             Field("payload", None, None)]

class Control(Compound):
  UNENCODED=[Field("channel", "uint16", 0)]

def pythonize(st):
  if st is None:
    return None
  else:
    return str(st.replace("-", "_"))

def pydoc(op, children=()):
  doc = "\n\n".join([fill(p.text(), 0) for p in op.query["doc"]])
  for ch in children:
    doc += "\n\n  " + pythonize(ch["@name"]) + " -- " + str(ch["@label"])
    ch_descs ="\n\n".join([fill(p.text(), 4) for p in ch.query["doc"]])
    if ch_descs:
      doc += "\n\n" + ch_descs
  return doc

def studly(st):
  return "".join([p.capitalize() for p in st.split("-")])

def klass(nd):
  while nd.parent is not None:
    if hasattr(nd.parent, "name") and nd.parent.name == "class":
      return nd.parent
    else:
      nd = nd.parent

def included(nd):
  cls = klass(nd)
  if cls is None:
    return True
  else:
    return cls["@name"] not in ("file", "stream")

def num(s):
  if s: return int(s, 0)

def code(nd):
  c = num(nd["@code"])
  if c is None:
    return None
  else:
    cls = klass(nd)
    if cls is None:
      return c
    else:
      return c | (num(cls["@code"]) << 8)

def default(f):
  if f["@type"] == "bit":
    return False
  else:
    return None

def make_compound(decl, base, domains):
  dict = {}
  fields = decl.query["field"]
  dict["__doc__"] = pydoc(decl, fields)
  dict["NAME"] = pythonize(decl["@name"])
  dict["SIZE"] = num(decl["@size"])
  dict["CODE"] = code(decl)
  dict["PACK"] = num(decl["@pack"])
  dict["FIELDS"] = [Field(pythonize(f["@name"]), resolve(f, domains),
                          default(f))
                    for f in fields]
  dict["ARGS"] = dict["FIELDS"] + base.UNENCODED
  return str(studly(decl["@name"])), (base,), dict

def make_restricted(decl, domains):
  name = pythonize(decl["@name"])
  dict = {}
  choices = decl.query["choice"]
  dict["__doc__"] = pydoc(decl, choices)
  dict["NAME"] = name
  dict["TYPE"] = str(decl.parent["@type"])
  values = []
  for ch in choices:
    val = int(ch["@value"], 0)
    dict[pythonize(ch["@name"])] = val
    values.append(val)
  dict["VALUES"] = values
  return name, (Enum,), dict

def make_type(decl, domains):
  name = pythonize(decl["@name"])
  dict = {}
  dict["__doc__"] = pydoc(decl)
  dict["NAME"] = name
  dict["CODE"] = code(decl)
  return str(studly(decl["@name"])), (Primitive,), dict

def make_command(decl, domains):
  decl.set_attr("name", "%s-%s" % (decl.parent["@name"], decl["@name"]))
  decl.set_attr("size", "0")
  decl.set_attr("pack", "2")
  name, bases, dict = make_compound(decl, Command, domains)
  dict["RESULT"] = pythonize(decl["result/@type"]) or pythonize(decl["result/struct/@name"])
  return name, bases, dict

def make_control(decl, domains):
  decl.set_attr("name", "%s-%s" % (decl.parent["@name"], decl["@name"]))
  decl.set_attr("size", "0")
  decl.set_attr("pack", "2")
  return make_compound(decl, Control, domains)

def make_struct(decl, domains):
  return make_compound(decl, Compound, domains)

def make_enum(decl, domains):
  decl.set_attr("name", decl.parent["@name"])
  return make_restricted(decl, domains)


vars = globals()

def make(nd, domains):
  return vars["make_%s" % nd.name](nd, domains)

def qualify(nd, field="@name"):
  cls = klass(nd)
  if cls is None:
    return pythonize(nd[field])
  else:
    return pythonize("%s.%s" % (cls["@name"], nd[field]))

def resolve(nd, domains):
  candidates = qualify(nd, "@type"), pythonize(nd["@type"])
  for c in candidates:
    if domains.has_key(c):
      while domains.has_key(c):
        c = domains[c]
      return c
  else:
    return c

def load_types_from_xml(file):
  spec = mllib.xml_parse(file)
  domains = dict([(qualify(d), pythonize(d["@type"]))
                  for d in spec.query["amqp/domain", included] + \
                    spec.query["amqp/class/domain", included]])
  type_decls = \
      spec.query["amqp/class/command", included] + \
      spec.query["amqp/class/control", included] + \
      spec.query["amqp/class/command/result/struct", included] + \
      spec.query["amqp/class/struct", included] + \
      spec.query["amqp/class/domain/enum", included] + \
      spec.query["amqp/domain/enum", included] + \
      spec.query["amqp/type"]
  types = [make(nd, domains) for nd in type_decls]
  return types

def load_types(file):
  base, ext = os.path.splitext(file)
  pclfile = "%s.pcl" % base
  if os.path.exists(pclfile) and \
        os.path.getmtime(pclfile) > os.path.getmtime(file):
    f = open(pclfile, "rb")
    types = pickle.load(f)
    f.close()
  else:
    types = load_types_from_xml(file)
    if os.access(os.path.dirname(os.path.abspath(pclfile)), os.W_OK):
      f = open(pclfile, "wb")
      pickle.dump(types, f)
      f.close()
  return types

from specs_config import amqp_spec as file
types = load_types(file)

ENUMS = {}
PRIMITIVE = {}
COMPOUND = {}
COMMANDS = {}
CONTROLS = {}

for name, bases, _dict in types:
  t = type(name, bases, _dict)
  vars[name] = t

  if issubclass(t, Command):
    COMMANDS[t.NAME] = t
    COMMANDS[t.CODE] = t
  elif issubclass(t, Control):
    CONTROLS[t.NAME] = t
    CONTROLS[t.CODE] = t
  elif issubclass(t, Compound):
    COMPOUND[t.NAME] = t
    if t.CODE is not None:
      COMPOUND[t.CODE] = t
  elif issubclass(t, Primitive):
    PRIMITIVE[t.NAME] = t
    PRIMITIVE[t.CODE] = t
  elif issubclass(t, Enum):
    ENUMS[t.NAME] = t