예제 #1
0
class PythonMaker(object):

    def __init__(self):
        self.out = Emitter()
        self.ref = RefEmitter("## ", self.out)

        self.did_module_head = False
        self.did_module_body_lead_in = False
        self.module_name = None
        self.module_py_name = None

    def dump(self, fd):
        return self.out.dump(fd)

    def module(self, m):
        self.ref.module_head(m)
        self.module_name = m.name
        self.module_py_name = m.py_name

        for definition in m.definitions:
            if definition is None:
                continue
            kind = definition.__class__.__name__
            if kind == "Description":
                self.ref.description(definition)
                self.out('"""')
                self.out(str(definition.content)[1:-1])
                self.out('"""')
            elif kind == "Defaults":
                self.ref("""defaults { %s };""" % "no content")
            elif kind == "Struct":
                self.struct(definition)
            elif kind == "Operation":
                self.operation(definition)
            else:
                raise ValueError("WTF? %s %s" % (kind, definition))

        self.ref.module_tail(m)

    def module_head(self):
        raise NotImplementedError

    def struct(self, st):
        self.module_head()
        assert not self.did_module_body_lead_in, "Must have all structs before any functions (to be fixed)"
        self.ref.struct(st)
        self.out("@AdaptiveValue")
        self.out("class %s(AdaptiveValueType):" % st.py_name)
        with self.out.indentation():
            self.out("__slots__ = " + ", ".join('"%s"' % field.name for field in st.fields))
            self.out("")
            self.out("def __init__(self, %s):" % ", ".join(process_declarations(st.fields)))
            with self.out.indentation():
                self.out("AdaptiveValueType.__init__(self)")
                for field in st.fields:
                    self.out("self.%s = %s" % (field.name, field.py_name))  # FIXME is this okay?
        self.out("")

    def operation(self, op):
        raise NotImplementedError
예제 #2
0
    def __init__(self):
        self.out = Emitter()
        self.ref = RefEmitter("## ", self.out)

        self.did_module_head = False
        self.did_module_body_lead_in = False
        self.module_name = None
        self.module_py_name = None
예제 #3
0
def service_server(node):
    out = Emitter()
    with out.block('package %s' % code(node.package.name)):
        node.traverse(ServerGenerator(out))
    return out.dumps()
예제 #4
0
def value(node):
    out = Emitter()
    with out.block('package %s' % code(node.package.name)):
        node.traverse(EncodeGenerator(out))
        node.traverse(DecodeGenerator(out))
    return out.dumps()