Exemplo n.º 1
0
def build_obj(code: str or tuple):
    tree = pyposast.parse(code, filename='__main__', mode='exec')
    varname = None
    type_ = None
    acronym = None
    name = None

    if (type(tree.body[0]) is not pyposast.ast.Assign):
        raise 'Invalid build a instace Place from {code}.'

    varname = tree.body[0].targets[0].id
    type_ = tree.body[0].value.func.id

    if (len(tree.body[0].value.args) > 0):
        acronym = tree.body[0].value.args[0].s
        name = tree.body[0].value.args[1].s

    elif (len(tree.body[0].value.keywords) > 0):
        get_keyword_index = lambda keyword: [
            k for k in tree.body[0].value.keywords[0] if k.arg == keyword
        ][0]
        acronym = tree.body[0].value.keywords[get_keyword_index(
            'acronym')].value.s
        name = tree.body[0].value.keywords[get_keyword_index('name')].value.s

    return Place(varname=varname, type_=type_, acronym=acronym, name=name)
Exemplo n.º 2
0
 def parse(self, code):
     metascript = {
         'name': 'name',
         'code': bytes_string(code, 'utf-8'),
         'path': NAME,
         'compiled': None,
     }
     self.visitor = SlicingVisitor(metascript)
     self.visitor.metascript['code'] = code
     return pyposast.parse(code)
Exemplo n.º 3
0
def work_operation(filename, lines, varname, operation, value=""):
    """Apply `:operation` for `:varname` in a year file `:filename`"""
    vis_class = EditVisitor
    if operation in ("delete", "insert", "detect"):
        vis_class = BodyVisitor
    visitor = vis_class(lines, varname, operation)
    tree = pyposast.parse("\n".join(lines), filename)
    visitor.visit(tree)
    if not visitor.result:
        return visitor, False
    visitor.result.apply(lines, value)
    return visitor, True
Exemplo n.º 4
0
def visit_ast(metascript):
    '''returns a visitor that visited the tree and filled the attributes:
        functions: map of function in the form:
            name -> (arguments, global_vars, calls, code_hash)
        name_refs[path]: map of identifiers in categories Load, Store
        dependencies[path]: map of dependencies
    '''
    tree = pyposast.parse(metascript['code'], metascript['path'])
    visitor = SlicingVisitor(metascript)
    visitor.result = visitor.visit(tree)
    visitor.extract_disasm()
    visitor.teardown()
    return visitor
Exemplo n.º 5
0
    def _visit_ast(self, file_definition):
        """Return a visitor that visited the tree"""
        metascript = self.metascript
        try:
            tree = pyposast.parse(file_definition.code, file_definition.name)
        except SyntaxError:
            print_msg("Syntax error on file {}. Skipping file.".format(
                file_definition.name))
            return None

        visitor = SlicingVisitor(metascript, file_definition)
        visitor.result = visitor.visit(tree)
        visitor.extract_disasm()
        visitor.teardown()
        return visitor
Exemplo n.º 6
0
def visit_ast(metascript, path):
    '''returns a visitor that visited the tree and filled the attributes:
        functions: map of function in the form:
            name -> (arguments, global_vars, calls, code_hash)
        name_refs[path]: map of identifiers in categories Load, Store
        dependencies[path]: map of dependencies
    '''
    with open(path, 'rb') as f:
        code = f.read()

    try:
        tree = pyposast.parse(code, path)
    except SyntaxError:
        print_msg('Syntax error on file {}. Skipping file.'.format(path))
        return None

    visitor = SlicingVisitor(metascript, code, path)
    visitor.result = visitor.visit(tree)
    visitor.extract_disasm()
    visitor.teardown()
    return visitor
Exemplo n.º 7
0
def citation_operation(filename, lines, varname, year, operation, value=""):
    """Apply `:operation` for `:varname` in a citation file `:filename`"""
    visitor = CitationVisitor(lines, varname, year, operation)
    if operation == "rename":
        regex = re.compile(varname + r"\s*?,")
        for i, line in enumerate(lines):
            lines[i] = regex.sub(value + ",", line)
        return visitor, True
    elif operation == "insert citation":
        if lines[-1]:
            lines.append("")
        lines.append("")
        lines[-1:-1] = value.split("\n")
        if lines[-1]:
            lines.append("")
        return visitor, True
    tree = pyposast.parse("\n".join(lines), filename)
    visitor.visit(tree)
    if not visitor.result:
        return visitor, False
    if operation == "insert import":
        value = "from ..work.y{} import {}".format(year, varname)
    visitor.result.apply(lines, value)
    return visitor, True
Exemplo n.º 8
0
 def parse(self, code):
     metascript = Metascript()
     metascript.fake_path(NAME, bytes_string(code, "utf-8"))
     self.visitor = SlicingVisitor(metascript, metascript.paths[NAME])
     return pyposast.parse(code)
Exemplo n.º 9
0
 def parse(self, code):
     metascript = Metascript()
     metascript.fake_path(NAME, bytes_string(code, "utf-8"))
     self.visitor = SlicingVisitor(metascript, metascript.paths[NAME])
     return pyposast.parse(code)