From 3c242c821210b84ccf69b609630aa0154b2e7faf Mon Sep 17 00:00:00 2001 From: Andrew Boltachev Date: Sat, 12 Feb 2022 05:59:40 +0300 Subject: [PATCH] reading --- fs_utils.py | 5 +++++ mega-copy.py | 14 ++++++++++++-- parser_utils.py | 24 ++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/fs_utils.py b/fs_utils.py index d704d4b..74d9338 100644 --- a/fs_utils.py +++ b/fs_utils.py @@ -1,3 +1,8 @@ +def read_file(filename): + with open(filename, 'r') as f: + return f.read() + + def _run(command, timeout_s=False, shell=False): ### run a process, capture the output and wait for it to finish. if timeout is specified then Kill the subprocess and its childrens when the timeout is reached (if parent did not detach) ## usage: _run(arg1, arg2, arg3) diff --git a/mega-copy.py b/mega-copy.py index 571c7f3..7ab64a0 100644 --- a/mega-copy.py +++ b/mega-copy.py @@ -1,6 +1,9 @@ import sys -from fs_utils import _run -from termcolor import cprint +from fs_utils import _run, read_file +from termcolor import cprint, colored +import glob +import libcst + print("hello", sys.argv) @@ -11,3 +14,10 @@ if len(a[1]): cprint("Error: Git isn\'t clean, can\'t perform work\n", "red") sys.exit(1) + files = glob.glob("**/*.py") + terms = sys.argv[1:-1] + replace = sys.argv[-1] + print(f"Will try replace {', '.join(terms)} by {replace} in files above") + for filename in files: + print("Reading", colored(filename, "green")) + f = libcst.parse_module(read_file(filename)) diff --git a/parser_utils.py b/parser_utils.py index a0cb539..351d5f2 100644 --- a/parser_utils.py +++ b/parser_utils.py @@ -41,3 +41,27 @@ def serialize_dc(obj, *args): for k, v in obj.items()) else: return copy.deepcopy(obj) + + +def unserialize_dc(s, k=None): + from libcst import MaybeSentinel + if s == "MaybeSentinel.DEFAULT": + return MaybeSentinel.DEFAULT + if type(s) == list: + s = [unserialize_dc(x) for x in s] + if k in ['lpar', 'rpar']: + s = tuple(s) + return s + if type(s) == tuple: + return tuple([unserialize_dc(x) for x in list(s)]) + if type(s) != dict or not 'type' in s: + return s + args = {"type" if k == "typeparam" else k: unserialize_dc(v, k) for k, v in s.items() if k != "type"} + klass = node_class(s['type']) + try: + return klass(**args) + except Exception as e: + print(klass) + print(s) + print(args) + raise