diff options
Diffstat (limited to 'module/remote/create_ttypes.py')
-rw-r--r-- | module/remote/create_ttypes.py | 72 |
1 files changed, 67 insertions, 5 deletions
diff --git a/module/remote/create_ttypes.py b/module/remote/create_ttypes.py index a9a93bde7..023db60bb 100644 --- a/module/remote/create_ttypes.py +++ b/module/remote/create_ttypes.py @@ -11,9 +11,44 @@ module = join(path, "..", "..") sys.path.append(join(module, "lib")) sys.path.append(join(module, "remote")) +from thrift.Thrift import TType from thriftgen.pyload import ttypes -from thriftgen.pyload.Pyload import Iface +from thriftgen.pyload import Pyload + +type_map = { + TType.BOOL: 'bool', + TType.DOUBLE: 'float', + TType.I16: 'int', + TType.I32: 'int', + TType.I64: 'int', + TType.STRING: 'basestring', + TType.MAP: 'dict', + TType.LIST: 'list', + TType.SET: 'set', + TType.VOID: 'None', + TType.STRUCT: 'BaseObject', + TType.UTF8: 'unicode', +} + +def write_spec(attr, spec, f): + """ analyze the generated spec file and writes information into file """ + if spec[1] == TType.STRUCT: + f.write("\t'%s': %s,\n" % (attr, spec[3][0].__name__)) + elif spec[1] == TType.LIST: + if spec[3][0] == TType.STRUCT: + ttype = spec[3][1][0].__name__ + else: + ttype = type_map[spec[3][0]] + f.write("\t'%s': (list, %s),\n" % (attr, ttype)) + elif spec[1] == TType.MAP: + if spec[3][2] == TType.STRUCT: + ttype = spec[3][3][0].__name__ + else: + ttype = type_map[spec[3][2]] + f.write("\t'%s': (dict, %s, %s),\n" % (attr, type_map[spec[3][0]], ttype)) + else: + f.write("\t'%s': %s,\n" % (attr, type_map[spec[1]])) def main(): @@ -35,7 +70,6 @@ def main(): f = open(join(path, "ttypes.py"), "wb") - f.write( """#!/usr/bin/env python # -*- coding: utf-8 -*- @@ -47,6 +81,14 @@ class BaseObject(object): """) + dev = open(join(path, "ttypes_debug.py"), "wb") + dev.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\n +from ttypes import *\n +""") + ## generate enums for enum in enums: name = enum.__name__ @@ -59,33 +101,53 @@ class BaseObject(object): f.write("\n") + dev.write("classes = {\n\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__) + dev.write("'%s' : {\n" % name) #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__: + for i, attr in enumerate(klass.__slots__): f.write("\t\tself.%s = %s\n" % (attr, attr)) + spec = klass.thrift_spec[i+1] + assert spec[2] == attr + write_spec(attr, spec, dev) + f.write("\n") + dev.write("},\n") + + dev.write("}\n\n") f.write("class Iface(object):\n") + dev.write("methods = {\n") - for name in dir(Iface): + for name in dir(Pyload.Iface): if name.startswith("_"): continue - func = inspect.getargspec(getattr(Iface, name)) + func = inspect.getargspec(getattr(Pyload.Iface, name)) f.write("\tdef %s(%s):\n\t\tpass\n" % (name, ", ".join(func.args))) + spec = getattr(Pyload, "%s_result" % name).thrift_spec + if not spec or not spec[0]: + dev.write("\t'%s': None,\n" % name) + else: + spec = spec[0] + write_spec(name, spec, dev) + f.write("\n") + dev.write("}\n") f.close() + dev.close() if __name__ == "__main__": main()
\ No newline at end of file |