Repository navigation
Expand file tree
/
Copy pathcreate_ttypes.py
More file actions
91 lines (60 loc) · 2.18 KB
/
Copy pathcreate_ttypes.py
File metadata and controls
91 lines (60 loc) · 2.18 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import inspect
import sys
from os.path import abspath, dirname, join
path = dirname(abspath(__file__))
module = join(path, "..", "..")
sys.path.append(join(module, "lib"))
sys.path.append(join(module, "remote"))
from thriftbackend.thriftgen.pyload import ttypes
from thriftbackend.thriftgen.pyload.Pyload import Iface
def main():
enums = []
classes = []
print "generating lightweight ttypes.py"
for name in dir(ttypes):
klass = getattr(ttypes, name)
if name in ("TBase", "TExceptionBase") or name.startswith("_") or not (issubclass(klass, ttypes.TBase) or issubclass(klass, ttypes.TExceptionBase)):
continue
if hasattr(klass, "thrift_spec"):
classes.append(klass)
else:
enums.append(klass)
f = open(join(path, "ttypes.py"), "wb")
f.write(
"""#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Autogenerated by pyload
# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
class BaseObject(object):
\t__slots__ = []
""")
## generate enums
for enum in enums:
name = enum.__name__
f.write("class %s:\n" % name)
for attr in dir(enum):
if attr.startswith("_") or attr in ("read", "write"): continue
f.write("\t%s = %s\n" % (attr, getattr(enum, attr)))
f.write("\n")
for klass in classes:
name = klass.__name__
base = "Exception" if issubclass(klass, ttypes.TExceptionBase) else "BaseObject"
f.write("class %s(%s):\n" % (name, base))
f.write("\t__slots__ = %s\n\n" % klass.__slots__)
#create init
args = ["self"] + ["%s=None" % x for x in klass.__slots__]
f.write("\tdef __init__(%s):\n" % ", ".join(args))
for attr in klass.__slots__:
f.write("\t\tself.%s = %s\n" % (attr, attr))
f.write("\n")
f.write("class Iface:\n")
for name in dir(Iface):
if name.startswith("_"): continue
func = inspect.getargspec(getattr(Iface, name))
f.write("\tdef %s(%s):\n\t\tpass\n" % (name, ", ".join(func.args)))
f.write("\n")
f.close()
if __name__ == "__main__":
main()