示例#1
0
文件: demoize.py 项目: jlipps/demoize
 def start_server(self, script_file, language=None, port=5000, **params):
     print "Starting server with code file at %s" % script_file
     lexer_opts = {
         'stripall': True
     }
     if language:
         lexer = get_lexer_by_name(language, **lexer_opts)
     else:
         lexer = get_lexer_for_filename(script_file, **lexer_opts)
     formatter = HtmlFormatter(linenos=True, linespans="line",
                               anchorlinenos="aline")
     code = ''
     with open(script_file, 'r') as f:
         code = f.read()
     result = highlight(code, lexer, formatter)
     params['code'] = result
     run_app(port=port, **params)
示例#2
0
文件: demoize.py 项目: jlipps/demoize
            extra_stmt = 'DEMOIZE_OBJ.goto_line(line=%s)' % node.lineno
            extra_node = ast.parse(extra_stmt)
            return (extra_node.body[0], node)
        else:
            parent_nodes.append(node)
            for field, old_value in ast.iter_fields(node):
                old_value = getattr(node, field, None)
                if isinstance(old_value, list):
                    new_values = []
                    for value in old_value:
                        if isinstance(value, ast.AST):
                            value = self.generic_visit(value, parent_nodes)
                            if value is None:
                                continue
                            elif not isinstance(value, ast.AST):
                                new_values.extend(value)
                                continue
                        new_values.append(value)
                    old_value[:] = new_values
                elif isinstance(old_value, ast.AST):
                    new_node = self.generic_visit(old_value, parent_nodes)
                    if new_node is None:
                        delattr(node, field)
                    else:
                        setattr(node, field, new_node)
            return node


if __name__ == "__main__":
    run_app()