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
示例#2
0
 def finalize(self, source, target, set_anchor=True):
     writesourcepath(source, target)
     write_source_to_target_mapping(source, target)
     if set_anchor:
         self.anchor = target