def _protocol_count_pair(self, src_path: Path, stub_path: Path): with open(src_path, "r", encoding="utf-8") as src_file: with open(stub_path, "r", encoding="utf-8") as stub_file: src_tokens = asttokens.ASTTokens(src_file.read(), True) stub_tokens = asttokens.ASTTokens(stub_file.read(), True) srcProtocolCounter = ProtocolCounterSingle( src_tokens, src_path.relative_to(self.src_dir.parent)) stubProtocolCounter = ProtocolCounterSingle( stub_tokens, stub_path.relative_to(self.stub_dir.parent)) srcProtocolCounter.workflow() stubProtocolCounter.workflow() self.protocol_num += srcProtocolCounter.protocol_num self.protocol_impl_implicit += srcProtocolCounter.protocol_impl_implicit self.protocol_impl_explict += srcProtocolCounter.protocol_impl_explicit self.protocol_num += stubProtocolCounter.protocol_num self.protocol_impl_implicit += stubProtocolCounter.protocol_impl_implicit self.protocol_impl_explict += stubProtocolCounter.protocol_impl_explicit self.protocol_set.update(srcProtocolCounter.protocols, stubProtocolCounter.protocols) self.protocol_implicit_set.update( srcProtocolCounter.implicit_impls, stubProtocolCounter.implicit_impls) self.protocol_explicit_set.update( srcProtocolCounter.explicit_impls, stubProtocolCounter.explicit_impls)
def class_count_single(self, file_path: Path): with open(file_path, "r", encoding="utf-8") as file: tokens = asttokens.ASTTokens(file.read(), parse=True) classCounter = ClassCounterSingle( tokens, file_path.relative_to(self.src_dir.parent)) classCounter.collectClass() self.class_set.update(classCounter.class_sigs)
def class_count_pair(self, src_path: Path, stub_path: Path): with open(src_path, "r", encoding="utf-8") as src_file: with open(stub_path, "r", encoding="utf-8") as stub_file: src_tokens = asttokens.ASTTokens(src_file.read(), True) stub_tokens = asttokens.ASTTokens(stub_file.read(), True) srcClassCounter = ClassCounterSingle( src_tokens, src_path.relative_to(self.src_dir.parent)) stubClassCounter = ClassCounterSingle( stub_tokens, stub_path.relative_to(self.stub_dir.parent)) srcClassCounter.collectClass() stubClassCounter.collectClass() self.class_set.update(srcClassCounter.class_sigs) stub_classes = stubClassCounter.class_sigs self.stub_classes.update(stub_classes) self.class_set.update(stub_classes) newClasses = getNewClass(srcClassCounter, stubClassCounter) self.new_class_set.update(newClasses) self.new_class_inherit_set.update( getSubTypeRel(newClasses, stub_classes))
def _protocol_count_single(self, file_path: Path): with open(file_path, "r", encoding="utf-8") as file: tokens = asttokens.ASTTokens(file.read(), parse=True) protocolCounter = ProtocolCounterSingle( tokens, file_path.relative_to(self.src_dir.parent)) protocolCounter.workflow() self.protocol_num += protocolCounter.protocol_num self.protocol_impl_implicit += protocolCounter.protocol_impl_implicit self.protocol_impl_explict += protocolCounter.protocol_impl_explicit self.protocol_set.update(protocolCounter.protocols) self.protocol_implicit_set.update(protocolCounter.implicit_impls) self.protocol_explicit_set.update(protocolCounter.explicit_impls)
def get_values_from_execution(source, args, raise_exceptions=False): """Execute the source and return a dictionary mapping source code character ranges to values (for nodes whose values we saved).""" tree = asttokens.ASTTokens(source, parse=True).tree SaveTransformer().visit(tree) ast.fix_missing_locations(tree) environment = get_execution_environment() run(compile(tree, filename="<ast>", mode="exec"), args, environment=environment, raise_exceptions=raise_exceptions) # For simplicity, turn the defaultdict into a dict. return dict(environment['_seerun_saved_values'])
def _get_tree_node_from_file(file_path, repo_path): """ Parse a TreeNode representing the module in the specified file. Arguments: file_path {string} -- Path of file to parse the TreeNode from. Returns: TreeNode -- TreeNode parsed from the specified file. """ atok = asttokens.ASTTokens(_read_whole_file(file_path), True) root = atok.tree lino = LineNumbers(atok.get_text(root)) return TreeNode(atok, lino, root, relpath(file_path, repo_path))
def check_content(source_text, fix=False): """ Perform OCD-check on a single file. :param source_text: source to be checked. :param fix: if false, file won't be modified, only report the issues. """ atok = asttokens.ASTTokens(source_text, parse=True) changeset = set() for node in ast.walk(atok._tree): if isinstance(node, CHECK_NODES): tokens = list(atok.get_tokens(node, include_extra=True))[::-1] if not check_node(tokens, changeset, node.__class__): if not fix: log.error('OCD node:, line {}\n{}\n'.format( node.lineno, atok.get_text(node))) if fix and changeset: return False, util.replace(source_text, [(change, change, ',') for change in changeset]) return not len(changeset), source_text