Skip to content

Commit

Permalink
reading
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewboltachev committed Feb 12, 2022
1 parent 698cff4 commit 3c242c8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
5 changes: 5 additions & 0 deletions fs_utils.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
14 changes: 12 additions & 2 deletions mega-copy.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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))
24 changes: 24 additions & 0 deletions parser_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3c242c8

Please sign in to comment.