Exemple #1
0
    def get_lines(self):
        if self.func:
            source = inspect.getsource(self.func)
        else:
            source = asttools.dump_python_source(self.ast)

        source = "\n" * (self.ast.lineno - 2) + source
        return source.splitlines()
Exemple #2
0
    def get_lines(self):
        if self.func:
            source = inspect.getsource(self.func)
        else:
            try:
                from meta import asttools
                source = asttools.dump_python_source(self.ast)
            except Exception:
                source = ""

        source = "\n" * (self.ast.lineno - 2) + source
        return source.splitlines()
Exemple #3
0
    def get_lines(self):
        source = None
        if self.func:
            try:
                source = inspect.getsource(self.func)
            except EnvironmentError:
                pass

        if source is None:
            try:
                from meta import asttools
                source = asttools.dump_python_source(self.ast)
            except Exception:
                source = ""

        first_lineno = getattr(self.ast, "lineno", 2)
        line_offset = offset(source.splitlines())

        newlines = "\n" * (first_lineno - line_offset)
        source = newlines + source

        return source.splitlines()
Exemple #4
0
    def update(self, tag, kwargs):
        """update - Return
        """
        c = self.c
        if kwargs['c'] != c:
            return
        if not self.w.isVisible():
            return
        if c.p.v != self.v:
            self.status.setText("(paused - different node)")
            return
        if not self.active:
            return
        self.status.setText("ACTIVE")

        source = c.p.b
        lines = source.split('\n')

        try:
            top_level = ast.parse(source)
        except SyntaxError:
            self.status.setText("ACTIVE - INCOMPLETE CODE")
            return

        if self.dump:
            self.dump = False
            print(ast.dump(top_level))

        block = []  # blocks (strings) of source code
        nodes = list(ast.iter_child_nodes(top_level))
        self.scope['p'] = c.p
        run_count = 0

        # break source up into blocks corresponding to top level nodes
        for n, node in enumerate(nodes):
            if n == len(nodes) - 1:
                next_node = len(lines)
            else:
                next_node = nodes[n+1].lineno
            block.append("\n".join(lines[node.lineno-1:next_node-1]))

        result = []
        for n, node in enumerate(nodes):

            node_result = None

            if (n < len(self.codeblocks) and
                self.codeblocks[n].code == block[n]
            ):
                # same code, assume same result
                node_result = self.codeblocks[n].result
            else:
                run_count += 1
                # drop all remaining stored results (maybe none)
                del self.codeblocks[n:]

                try:
                    if isinstance(node, ast.Expr):
                        # pylint: disable=eval-used
                        node_result = repr(eval(block[n], self.scope))
                    else:
                        # exec block[n] in self.scope
                        # EKR: Python 3 compatibility.
                        exec(block[n],self.scope)
                except Exception:
                    self.status.setText("ACTIVE: fail at %s" %
                        block[n].split('\n')[0])
                    break

                if isinstance(node, ast.Expr):
                    pass  # already handled above
                elif isinstance(node, (ast.Assign, ast.AugAssign)):
                    node_result = []
                    if isinstance(node, ast.AugAssign):
                        todo = [node.target]
                    else:
                        todo = list(node.targets)
                    while todo:
                        target = todo.pop(0)
                        if isinstance(target, ast.Tuple):
                            todo.extend(target.elts)
                            continue
                        code = asttools.dump_python_source(target)
                        # pylint: disable=eval-used
                        node_result.append("%s = %r" %
                            (code.strip(), eval(code, self.scope)))

                    node_result = '\n'.join(node_result)

            assert node_result is None or isinstance(node_result, str)

            if node_result is not None:
                self.codeblocks.append(self.CodeBlock(block[n], node_result))
                result.append(node_result)
            else:
                self.codeblocks.append(self.CodeBlock(block[n], None))

        self.text.setText('\n\n'.join(result))
        if run_count:
            self.status.setText("ACTIVE: %d blocks" % run_count)
Exemple #5
0
def getsource(ast):
    from meta import asttools
    return asttools.dump_python_source(ast).strip()
Exemple #6
0
def from_string(string, filename=''):
    node = EmojiTransformer().visit(parse(string, filename))
    return encoding + dump_python_source(node)