def slice_string(currentline, source, name, invert=False, formatter=programslice.formatter.LineFormatter): """ Slices the given source code from the given currentline. :param currentline: A line from which to start the slicing. :type currentline: int :param source: The source code to parse. :type source: string :param name: filename of the given source code. :type name: string :param invert: Invert the result and return lines which don't depend on the ``currentline``. Defaults to **False**. :type invert: bool :param formatter: Formatter class to format the slice result. Defaults to LineFormatter which only outputs the line numbers. :type formatter: class """ lines = [] # catch encoding declarations and shebangs head = re.compile(r'#!\/.*\n|#.*coding[:=]\s*(?P<enc>[-\w.]+).*') source = head.sub('', source) node = ast.parse(source, name) visitor = programslice.visitor.LineDependencyVisitor() visitor.visit(node) graph = visitor.get_graph_for(currentline) if graph: lines = graph.slice_forward(currentline) inverted = set(range(graph.first, graph.last + 1)) - set(lines) result = list(inverted) if invert else lines return formatter(result, source)()
def get_sliced_testdata(filename, start_criteria): """ Parses given filename and slices the source by given start_criteria. Returns the slice result """ node = load_testdata(filename) visitor = programslice.visitor.LineDependencyVisitor() visitor.visit(node) graph = visitor.graph return programslice.graph.Slice(graph)(start_criteria)
def slice_string(varname, currentline, offset, source, filename, formatter=programslice.formatter.VimOutPutFormatter): """ Slices the given source code from the given currentline. :param varname: The variable name to slice from. :type varname: str :param currentline: A line from which to start the slicing. :type currentline: int :param offset: The position offset of the variable. :type offset: int :param source: The source code to parse. :type source: str :param filename: filename of the given source code. :type filename: str :param formatter: Formatter class to format the slice result. Defaults to VimOutPutFormatter which only outputs the line numbers. :type formatter: class .. deprecated:: 0.3 :param invert: Invert the result and return lines which don't depend on the ``currentline``. Defaults to **False**. :type invert: bool """ result = [] # catch encoding declarations and shebangs head = re.compile(r'#!\/.*\n|#.*coding[:=]\s*(?P<enc>[-\w.]+).*') source = head.sub('', source) node = ast.parse(source, filename) visitor = programslice.visitor.LineDependencyVisitor() visitor.visit(node) graph = visitor.graph if graph: start = programslice.graph.Edge(varname, currentline, offset) result = programslice.graph.Slice(graph)(start) return formatter(result, source)()
def build_graph(node): visitor = programslice.visitor.LineDependencyVisitor() visitor.visit(node) return visitor.graph