PageRenderTime 343ms CodeModel.GetById 201ms app.highlight 7ms RepoModel.GetById 134ms app.codeStats 0ms

/Tools/framer/framer/member.py

http://unladen-swallow.googlecode.com/
Python | 73 lines | 63 code | 9 blank | 1 comment | 8 complexity | dc66feb3e5e9085c68cd2249fe6d93f9 MD5 | raw file
 1from framer import template
 2from framer.util import cstring, unindent
 3
 4T_SHORT = "T_SHORT"
 5T_INT = "T_INT"
 6T_LONG = "T_LONG"
 7T_FLOAT = "T_FLOAT"
 8T_DOUBLE = "T_DOUBLE"
 9T_STRING = "T_STRING"
10T_OBJECT = "T_OBJECT"
11T_CHAR = "T_CHAR"
12T_BYTE = "T_BYTE"
13T_UBYTE = "T_UBYTE"
14T_UINT = "T_UINT"
15T_ULONG = "T_ULONG"
16T_STRING_INPLACE = "T_STRING_INPLACE"
17T_OBJECT_EX = "T_OBJECT_EX"
18
19RO = READONLY = "READONLY"
20READ_RESTRICTED = "READ_RESTRICTED"
21WRITE_RESTRICTED = "WRITE_RESTRICTED"
22RESTRICT = "RESTRICTED"
23
24c2t = {"int" : T_INT,
25       "unsigned int" : T_UINT,
26       "long" : T_LONG,
27       "unsigned long" : T_LONG,
28       "float" : T_FLOAT,
29       "double" : T_DOUBLE,
30       "char *" : T_CHAR,
31       "PyObject *" : T_OBJECT,
32       }
33
34class member(object):
35
36    def __init__(self, cname=None, type=None, flags=None, doc=None):
37        self.type = type
38        self.flags = flags
39        self.cname = cname
40        self.doc = doc
41        self.name = None
42        self.struct = None
43
44    def register(self, name, struct):
45        self.name = name
46        self.struct = struct
47        self.initvars()
48
49    def initvars(self):
50        v = self.vars = {}
51        v["PythonName"] = self.name
52        if self.cname is not None:
53            v["CName"] = self.cname
54        else:
55            v["CName"] = self.name
56        v["Flags"] = self.flags or "0"
57        v["Type"] = self.get_type()
58        if self.doc is not None:
59            v["Docstring"] = cstring(unindent(self.doc))
60        v["StructName"] = self.struct.name
61
62    def get_type(self):
63        """Deduce type code from struct specification if not defined"""
64        if self.type is not None:
65            return self.type
66        ctype = self.struct.get_type(self.name)
67        return c2t[ctype]
68
69    def dump(self, f):
70        if self.doc is None:
71            print >> f, template.memberdef_def % self.vars
72        else:
73            print >> f, template.memberdef_def_doc % self.vars