예제 #1
0
 def _createastchild(self, astnode):
     if hasattr(astnode, 'lineno'):
         if astnode.lineno - 1 in self.model.readlines:
             return
     if isinstance(astnode, _ast.Import) \
       or isinstance(astnode, _ast.ImportFrom):
         import_ = Import(None, [], astnode, self.model.buffer)
         import_.readlines = self.model.readlines
         self.model[str(import_.uuid)] = import_
     elif isinstance(astnode, _ast.FunctionDef):
         function = Function(None, astnode, self.model.buffer)
         function.readlines = self.model.readlines
         self.model[str(function.uuid)] = function
         for childastnode in astnode.body:
             function.parser._createastchild(childastnode)
         function.initdecorators()
     elif isinstance(astnode, _ast.ClassDef):
         class_ = Class(None, astnode, self.model.buffer)
         class_.readlines = self.model.readlines
         self.model[str(class_.uuid)] = class_
         for childastnode in astnode.body:
             class_.parser._createastchild(childastnode)
         class_.initdecorators()
     elif isinstance(astnode, _ast.Expr) \
       and isinstance(astnode.value, _ast.Str):
         docstring = Docstring(None, astnode, self.model.buffer)
         docstring.readlines = self.model.readlines
         self.model[str(docstring.uuid)] = docstring
     elif isinstance(astnode, _ast.Assign):
         if not IFunction.providedBy(self.model):
             attribute = Attribute([], None, astnode, self.model.buffer)
             attribute.readlines = self.model.readlines
             self.model[str(attribute.uuid)] = attribute
예제 #2
0
 def _createastchild(self, astnode):
     if hasattr(astnode, 'lineno'):
         if astnode.lineno - 1 in self.model.readlines:
             return
     if isinstance(astnode, _ast.Import) \
       or isinstance(astnode, _ast.ImportFrom):
         import_ = Import(None, [], astnode, self.model.buffer)
         import_.readlines = self.model.readlines
         self.model[str(import_.uuid)] = import_
     elif isinstance(astnode, _ast.FunctionDef):
         function = Function(None, astnode, self.model.buffer)
         function.readlines = self.model.readlines
         self.model[str(function.uuid)] = function
         for childastnode in astnode.body:
             function.parser._createastchild(childastnode)
         function.initdecorators()
     elif isinstance(astnode, _ast.ClassDef):
         class_ = Class(None, astnode, self.model.buffer)
         class_.readlines = self.model.readlines
         self.model[str(class_.uuid)] = class_
         for childastnode in astnode.body:
             class_.parser._createastchild(childastnode)
         class_.initdecorators()
     elif isinstance(astnode, _ast.Expr) \
       and isinstance(astnode.value, _ast.Str):
         docstring = Docstring(None, astnode, self.model.buffer)
         docstring.readlines = self.model.readlines
         self.model[str(docstring.uuid)] = docstring
     elif isinstance(astnode, _ast.Assign):
         if not IFunction.providedBy(self.model):
             attribute = Attribute([], None, astnode, self.model.buffer)
             attribute.readlines = self.model.readlines
             self.model[str(attribute.uuid)] = attribute
def zcaadapterdefaultinit(self, source, target):
    """Set default __init__ function on adapter class if not present yet.
    """
    if source.stereotype('pyegg:function'):
        # XXX: <<function>> <<adapter>> on class
        return
    adapter_class = read_target_node(source, target.target)
    exists = False
    for function in adapter_class.filteredvalues(IFunction):
        if function.functionname == '__init__':
            exists = function
            break
    if not exists:
        tok = token(str(adapter_class.uuid), False)
        adapts = token(str(source.uuid), False).adapts
        if len(adapts) == 1:
            params = ['context']
        else:
            params = []
            for cl in adapts:
                if cl.name.startswith('I'):
                    params.append(cl.name[1:].lower())
                else:
                    params.append(cl.name.lower())
        func = Function('__init__')
        func.args = params
        block = Block()
        for param in params:
            block.lines.append('self.%s = %s' % (param, param))
        func[block.uuid] = block
        adapter_class[func.uuid] = func