コード例 #1
0
ファイル: generate_stub.py プロジェクト: Blue42hand/freeorion
def handle_function(doc):
    name = doc['name']
    doc = Docs(doc['doc'], 1)
    return_string = 'return %s%s' % (doc.rtype, '()' if doc.rtype != 'None' else '')

    res = 'def %s(%s):\n%s\n    %s' % (name, doc.get_argument_string(), doc.get_doc_string(), return_string)
    return res
コード例 #2
0
def handle_class(info):
    name = info['name']
    docs = info['doc']
    attrs = info['attrs']
    assert not docs, "Got docs need to handle it"
    parents = info['parents']
    if not parents:
        parents = ['object']  # instance is boost wrapper
    result = ['class %s(%s):' % (name, ', '.join(parents))]

    properties = []
    instance_methods = []
    for attr_name, attr in sorted(attrs.items()):
        if attr['type'] == "<type 'property'>":
            properties.append((attr_name, attr.get('rtype', '')))
        elif attr['type'] == "<type 'instancemethod'>":
            instance_methods.append(attr['routine'])
        else:
            warn("Skipping '%s': %s" % (name, attr))

    for property_name, rtype in properties:
        if not rtype:
            return_text = 'pass'
        elif rtype.startswith("<type"):
            return_text = 'return %s()' % rtype[7:-2]
        else:
            return_text = 'return %s()' % rtype.split('.')[-1].strip("'>")

        if property_name == 'class':
            result.append('    # cant define it via python in that way')
            result.append('    # @property')
            result.append('    # def %s(self): ' % property_name)
            result.append('    #    %s' % return_text)
        else:
            result.append('    @property')
            result.append('    def %s(self):' % property_name)
            result.append('        %s' % return_text)
        result.append('')

    for routine_name, routine_docs in instance_methods:
        docs = Docs(routine_docs, 2, is_class=True)
        # TODO: Subclass map-like classes from dict (or custom class) rather than this hack
        if docs.rtype in ('VisibilityIntMap', 'IntIntMap'):
            docs.rtype = 'dict[int, int]'
            return_string = 'return dict()'
        elif docs.rtype == 'None':
            return_string = 'return None'
        else:
            return_string = 'return %s()' % docs.rtype

        doc_string = docs.get_doc_string()
        result.append('    def %s(%s):' % (routine_name, docs.get_argument_string()))
        result.append(doc_string)
        result.append('        %s' % return_string)
        result.append('')
    if not (properties or instance_methods):
        result.append('    pass')
    if not result[-1]:
        result.pop()
    return '\n'.join(result)
コード例 #3
0
def handle_function(doc):
    name = doc['name']
    doc = Docs(doc['doc'], 1)
    return_string = 'return %s%s' % (doc.rtype,
                                     '()' if doc.rtype != 'None' else '')

    res = 'def %s(%s):\n%s\n    %s' % (name, doc.get_argument_string(),
                                       doc.get_doc_string(), return_string)
    return res
コード例 #4
0
def handle_class(info):
    name = info['name']
    docs = info['doc']
    attrs = info['attrs']
    assert not docs, "Got docs need to handle it"
    parents = info['parents']
    if not parents:
        parents = ['object']  # instance is boost wrapper
    result = ['class %s(%s):' % (name, ', '.join(parents))]

    properties = []
    instance_methods = []
    for attr_name, attr in sorted(attrs.items()):
        if attr['type'] == "<type 'property'>":
            properties.append((attr_name, attr.get('rtype', '')))
        elif attr['type'] == "<type 'instancemethod'>":
            instance_methods.append(attr['routine'])
        else:
            warning("Skipping '%s': %s" % (name, attr))

    for property_name, rtype in properties:
        if not rtype:
            return_text = 'pass'
        elif rtype.startswith("<type"):
            return_text = 'return %s()' % rtype[7:-2]
        else:
            return_text = 'return %s()' % rtype.split('.')[-1].strip("'>")

        if property_name == 'class':
            result.append('    # cant define it via python in that way')
            result.append('    # @property')
            result.append('    # def %s(self): ' % property_name)
            result.append('    #    %s' % return_text)
        else:
            result.append('    @property')
            result.append('    def %s(self):' % property_name)
            result.append('        %s' % return_text)
        result.append('')

    for routine_name, routine_docs in instance_methods:
        docs = Docs(routine_docs, 2, is_class=True)
        # TODO: Subclass map-like classes from dict (or custom class) rather than this hack
        if docs.rtype in ('VisibilityIntMap', 'IntIntMap'):
            docs.rtype = 'dict[int, int]'
            return_string = 'return dict()'
        elif docs.rtype == 'None':
            return_string = 'return None'
        else:
            return_string = 'return %s()' % docs.rtype

        doc_string = docs.get_doc_string()
        result.append('    def %s(%s):' %
                      (routine_name, docs.get_argument_string()))
        result.append(doc_string)
        result.append('        %s' % return_string)
        result.append('')
    if not (properties or instance_methods):
        result.append('    pass')
    if not result[-1]:
        result.pop()
    return '\n'.join(result)
コード例 #5
0
ファイル: generate_stub.py プロジェクト: scyt4l3/freeorion
def handle_class(info):
    name = info['name']
    docs = info['doc']
    attrs = info['attrs']
    assert not docs, "Got docs need to handle it"
    parents = info['parents']
    if not parents:
        parents = ['object']  # instance is boost wrapper
    result = ['class %s(%s):' % (name, ', '.join(parents))]

    properties = []
    instance_methods = []
    for attr_name, attr in attrs.items():
        if attr['type'] == "<type 'property'>":
            properties.append((attr_name, attr.get('rtype', '')))
        elif attr['type'] == "<type 'instancemethod'>":
            instance_methods.append(attr['rutine'])
        else:
            print "!!!", name, attr

    for property_name, rtype in properties:
        if not rtype:
            return_text = 'pass'
        elif rtype.startswith("<type"):
            return_text = 'return %s()' % rtype[7:-2]
        else:
            return_text = 'return %s()' % rtype.split('.')[-1].strip("'>")

        if property_name == 'class':
            result.append('    # cant define it via python in that way')
            result.append('    # @property')
            result.append('    # def %s(self): ' % property_name)
            result.append('    #    %s' % return_text)
        else:
            result.append('    @property')
            result.append('    def %s(self):' % property_name)
            result.append('        %s' % return_text)
        result.append('')

    for rutine_name, rutine_docs in instance_methods:
        if rutine_name == 'error_stub':
            continue

        docs = Docs(rutine_docs, 2)

        if docs.rtype == 'VisibilityIntMap':
            return_string = 'return dict()'
        elif docs.rtype == 'None':
            return_string = 'return None'
        else:
            return_string = 'return %s()' % docs.rtype

        argument_string = docs.get_argument_string(is_class=True)
        if argument_string:
            argument_string = ', ' + argument_string

        doc_string = docs.get_doc_string(is_class=True)
        result.append('    def %s(self%s):' % (rutine_name, argument_string))
        result.append(doc_string)
        result.append('        %s' % return_string)
        result.append('')
    if not (properties or instance_methods):
        result.append('    pass')
    if not result[-1]:
        result.pop()
    return '\n'.join(result)
コード例 #6
0
ファイル: generate_stub.py プロジェクト: Blue42hand/freeorion
def handle_class(info):
    name = info['name']
    docs = info['doc']
    attrs = info['attrs']
    assert not docs, "Got docs need to handle it"
    parents = info['parents']
    if not parents:
        parents = ['object']  # instance is boost wrapper
    result = ['class %s(%s):' % (name, ', '.join(parents))]

    properties = []
    instance_methods = []
    for attr_name, attr in attrs.items():
        if attr['type'] == "<type 'property'>":
            properties.append((attr_name, attr.get('rtype', '')))
        elif attr['type'] == "<type 'instancemethod'>":
            instance_methods.append(attr['rutine'])
        else:
            print "!!!", name, attr

    for property_name, rtype in properties:
        if not rtype:
            return_text = 'pass'
        elif rtype.startswith("<type"):
            return_text = 'return %s()' % rtype[7:-2]
        else:
            return_text = 'return %s()' % rtype.split('.')[-1].strip("'>")

        if property_name == 'class':
            result.append('    # cant define it via python in that way')
            result.append('    # @property')
            result.append('    # def %s(self): ' % property_name)
            result.append('    #    %s' % return_text)
        else:
            result.append('    @property')
            result.append('    def %s(self):' % property_name)
            result.append('        %s' % return_text)
        result.append('')

    for rutine_name, rutine_docs in instance_methods:
        if rutine_name == 'error_stub':
            continue

        docs = Docs(rutine_docs, 2)

        if docs.rtype == 'VisibilityIntMap':
            return_string = 'return dict()'
        elif docs.rtype == 'None':
            return_string = 'return None'
        else:
            return_string = 'return %s()' % docs.rtype

        argument_string = docs.get_argument_string(is_class=True)
        if argument_string:
            argument_string = ', ' + argument_string

        doc_string = docs.get_doc_string(is_class=True)
        result.append('    def %s(self%s):' % (rutine_name, argument_string))
        result.append(doc_string)
        result.append('        %s' % return_string)
        result.append('')
    if not (properties or instance_methods):
        result.append('    pass')
    if not result[-1]:
        result.pop()
    return '\n'.join(result)