Exemplo n.º 1
0
def pyclass(self, source, target):
    """Create python classes.
    """
    if source.stereotype('pyegg:stub'):
        return
    # skip class generation if previous custom handler mark this class as
    # already handled
    custom_handled = token('custom_handled_classes', True, classes=list())
    handled_classes = [str(uuid) for uuid in custom_handled.classes]
    if str(source.uuid) in handled_classes:
        return

    name = source.name
    module = target.anchor
    try:
        set_copyright(source, module)
    except AttributeError:
        msg = 'Package "%s" must have either <<pymodule>> or ' + \
              '<<pypackage>> stereotype'
        msg = msg % dotted_path(module)
        raise ValueError(msg)
    if module.classes(name):
        class_ = module.classes(name)[0]
        target.finalize(source, class_)
        return
    class_ = python.Class(name)
    module[str(class_.uuid)] = class_
    if not is_class_a_function(source) \
      and not source.parent.stereotype('pyegg:pymodule'):
        imp = Imports(module.parent['__init__.py'])
        imp.set(class_base_name(class_), [[class_.classname, None]])

    target.finalize(source, class_)
def pyfunctionfromclass(self, source, target):
    """Convert Class to function if class has stereotype function set.
    """
    if not is_class_a_function(source):
        return

    class_ = read_target_node(source, target.target)
    dec_keys = [dec.name for dec in class_.decorators()]
    decorators = [class_.detach(key) for key in dec_keys]
    module = class_.parent
    container = module
    if not source.parent.stereotype('pyegg:pymodule'):
        container = module.parent['__init__.py']
    functions = container.functions(class_.classname)
    if functions:
        if len(functions) > 1:
            raise "expected exactly one function by name '%s'" \
                % class_.classname
        function = functions[0]
    else:
        function = python.Function(class_.classname)
        function.__name__ = function.uuid
        container.insertlast(function)
    del module[str(class_.uuid)]

    # resync target to function, class was deleted
    writesourcepath(source, function)
    write_source_to_target_mapping(source, function)

    tgv = TaggedValues(source)
    _args = tgv.direct('args', 'pyegg:function')
    _kwargs = tgv.direct('kwargs', 'pyegg:function')
    _code = tgv.direct('code', 'pyegg:function')
    if _args is not UNSET:
        function.s_args = _args
    if _kwargs is not UNSET:
        function.s_kwargs = _kwargs
    if _code is not UNSET:
        if not function.blocks():
            function.insertlast(Block(_code))

    other_decorators = function.decorators()
    for dec in decorators:
        exists = 0
        for other in other_decorators:
            if dec.equals(other):
                exists = 1
        if exists:
            continue
        function[str(dec.uuid)] = dec