#!/usr/bin/env python"""Parse all files and write to a single file"""import osfrom pathlib import Pathfrom typing import List, NamedTuplefrom labml import logger, monitfrom parser import tokenizerfrom parser.tokenizer import encode, parse_stringCOMMENT = '#'MULTI_COMMENT = '"""'class _PythonFile(NamedTuple):relative_path: strproject: strpath: Pathclass _GetPythonFiles:"""Get list of python files and their paths inside `data/source` folder"""def __init__(self):self.source_path = Path(os.getcwd()) / 'data' / 'source'self.files: List[_PythonFile] = []self.get_python_files(self.source_path)logger.inspect([f.path for f in self.files])def add_file(self, path: Path):"""Add a file to the list of tiles"""project = path.relative_to(self.source_path).parentsproject = project[len(project) - 2]relative_path = path.relative_to(self.source_path / project)self.files.append(_PythonFile(relative_path=str(relative_path),project=str(project),path=path))def get_python_files(self, path: Path):"""Recursively collect files"""for p in path.iterdir():if p.is_dir():self.get_python_files(p)else:if p.suffix == '.py':self.add_file(p)def _fix_indentation(parsed: List[tokenizer.ParsedToken]) -> List[tokenizer.ParsedToken]:"""Change indentation tokens. Remove `DEDENT` tokens andadd `INDENT` tokens to each line.This is easier for prediction."""res: List[tokenizer.ParsedToken] = []indentation = 0indented = Falsefor t in parsed:if t.type == tokenizer.TokenType.indent:indentation += 1elif t.type == tokenizer.TokenType.dedent:indentation -= 1elif t.type in [tokenizer.TokenType.new_line,tokenizer.TokenType.eof]:indented = Falseres.append(t)else:if not indented:for _ in range(indentation):res.append(tokenizer.ParsedToken(tokenizer.TokenType.indent, 0))indented = Trueres.append(t)return resdef _remove_comments(parsed: List[tokenizer.ParsedToken]) -> List[tokenizer.ParsedToken]:"""Remove comment tokens"""res = []for p in parsed:if p.type == tokenizer.TokenType.comment:continueelse:res.append(p)return resdef _remove_empty_lines(parsed: List[tokenizer.ParsedToken]) -> List[tokenizer.ParsedToken]:"""Remove empty lines"""tokens = [tokenizer.TokenType.new_line, tokenizer.TokenType.new_line]res = []for p in parsed:for i in range(1):tokens[i] = tokens[i + 1]tokens[-1] = p.typeall_new_line = Truefor t in tokens:if t != tokenizer.TokenType.new_line:all_new_line = Falseif all_new_line:continueelse:res.append(p)return resdef _read_file(path: Path) -> List[int]:"""Read and encode a file"""with open(str(path)) as f:content = f.read()parsed = parse_string(content)parsed = _remove_comments(parsed)parsed = _remove_empty_lines(parsed)parsed = _fix_indentation(parsed)serialized = encode(parsed)# deserialized = tokenizer.deserialize(serialized)# for i in range(len(serialized)):# assert deserialized[i] == parsed[i]## res = to_text(deserialized)# print(res)return serializeddef main():source_files = _GetPythonFiles().fileslogger.inspect(source_files)with open(str(Path(os.getcwd()) / 'data' / 'all.py'), 'w') as f:for i, source in monit.enum("Parse", source_files):serialized = _read_file(source.path)# returnserialized = [str(t) for t in serialized]f.write(f"{str(source.path)}\n")f.write(" ".join(serialized) + "\n")if __name__ == '__main__':main()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。