import re as RX
import optparse as OP
+import sys as SYS; stdout = SYS.stdout
import types as TY
-from sys import stdout
-
###--------------------------------------------------------------------------
### Random utilities.
"""Replace non-alphanumeric characters in NAME with underscores."""
return R_IDBAD.sub('_', name)
+if SYS.version_info >= (3,):
+ def func_name(func): return func.__name__
+ xrange = range
+ long = int
+else:
+ def func_name(func): return func.func_name
+
+def with_metaclass(meta, *supers):
+ return meta('#<anonymous base %s>' % meta.__name__,
+ supers or (object,), dict())
+
+def load(file, mod):
+ with open(file) as f: text = f.read()
+ code = compile(text, file, 'exec')
+ exec(code, mod)
+
###--------------------------------------------------------------------------
### Determining the appropriate types.
except AttributeError: pass
return c
-class BasicIntType (object):
+class BasicIntType (with_metaclass(IntClass)):
"""
A base class for integer-type classes, providing defaults and protocol.
## Load the captured type information.
me.ti = TY.ModuleType('typeinfo')
- execfile(opts.typeinfo, me.ti.__dict__)
+ load(opts.typeinfo, me.ti.__dict__)
## Build a map of the available types.
tymap = {}
## Make sure we've not ended up somewhere really silly.
if mpwbits < 16:
- raise Exception, "`mpw' type is too small: your C environment is weird"
+ raise Exception("`mpw' type is too small: your C environment is weird")
## Now figure out suitable types for `mpw' and `mpd'.
def find_type(bits, what):
for ty in byrank:
if ty.bits >= bits: return ty
- raise Exception, \
- "failed to find suitable %d-bit type, for %s" % (bits, what)
+ raise Exception \
+ ("failed to find suitable %d-bit type, for %s" % (bits, what))
## Store our decisions.
me.mpwbits = mpwbits
arguments from the command and are expected to write their output to
stdout.
"""
- name = func.func_name
+ name = func_name(func)
if name.startswith('m_'): name = name[2:]
MODEMAP[name] = func
return func
else: MODEMAP[c.mode] = c.run
return c
-class GroupTable (object):
+class GroupTable (with_metaclass(GroupTableClass)):
"""
Base class for group tables objects.
## _slotmap A dictionary mapping slot names to their
## descriptions.
- __metaclass__ = GroupTableClass
-
## Default values.
keyword = 'group'
slots = []
if ff[0] == 'alias':
## An alias. Just remember this.
- if len(ff) != 3: raise Exception, "wrong number of alias arguments"
+ if len(ff) != 3: raise Exception("wrong number of alias arguments")
me._flush()
me._names.append((ff[1], ff[2]))
## Check the headline syntax. Headline slots may be set here, or
## later by name.
if len(ff) < 2 or len(ff) > 2 + len(me._headslots):
- raise Exception, "bad number of headline arguments"
+ raise Exception("bad number of headline arguments")
## Flush out the previous stanza.
me._flush()
elif ff[0] in me._slotmap:
## A slot assignment. Get the slot to store a value.
if me.st.name is None:
- raise Exception, "no group currently being defined"
+ raise Exception("no group currently being defined")
if len(ff) != 2:
- raise Exception, "bad number of values for slot `%s'" % ff[0]
+ raise Exception("bad number of values for slot `%s'" % ff[0])
me._slotmap[ff[0]].set(me.st, ff[1])
else:
## Something incomprehensible.
- raise Exception, "unknown keyword `%s'" % ff[0]
+ raise Exception("unknown keyword `%s'" % ff[0])
## End of the input. Write out the final stanza.
me._flush()
stdout.write("\nconst %s %s[] = {\n" % (me.entry_t, me.tabname))
for a, n in me._names:
if n not in me._defs:
- raise Exception, "alias `%s' refers to unknown group `%s'" % (a, n)
+ raise Exception("alias `%s' refers to unknown group `%s'" % (a, n))
stdout.write(' { "%s", &c_%s },\n' % (a, fix_name(n)))
stdout.write(" { 0, 0 }\n};\n\n")
Store a VALUE for the slot.
"""
if me._allowp and not me._allowp(st, value):
- raise Exception, "slot `%s' not allowed here" % me.name
+ raise Exception("slot `%s' not allowed here" % me.name)
st.d[me] = value
def setup(me, st):
Prepare the slot for output, checking its value and so on.
"""
if me not in st.d and (not me._omitp or not me._omitp(st)):
- raise Exception, "missing slot `%s'" % me.name
+ raise Exception("missing slot `%s'" % me.name)
class EnumSlot (BaseSlot):
"""
Check that the VALUE is one of the ones we know.
"""
if value not in me._values:
- raise Exception, "invalid %s value `%s'" % (me.name, value)
+ raise Exception("invalid %s value `%s'" % (me.name, value))
super(EnumSlot, me).set(st, value)
def write(me, st):