mod.py

 1import os, sys, re
 2
 3
 4def camel_to_snake(inputstring):
 5    REG = r'(?<!^)(?=[A-Z])'
 6    return re.sub(REG, '_', inputstring).lower()
 7
 8
 9def change_case(mypath):
10    if os.path.isabs(mypath):
11        raise ValueError
12    else:
13        abs_path_to_dir = os.path.normpath(os.path.join(os.path.dirname(__file__), mypath))
14        with os.scandir(abs_path_to_dir) as iter:
15            dirs = []
16            typescriptfiles = []
17            for entry in iter:
18                if (entry.is_dir() and entry.name not in ["node_modules", "target"]):
19                    dirs.append(entry.name)
20                if (entry.is_file() and entry.name.endswith('.ts')):
21                    typescriptfiles.append(entry.name)
22            if len(dirs) != 0:
23                for dir in dirs:
24                    change_case(os.path.normpath(os.path.join(mypath,dir)))
25            for entry in typescriptfiles:
26                relative_path = os.path.normpath(os.path.join(mypath,entry))
27                dst = camel_to_snake(relative_path)
28                abs_path = os.path.normpath(os.path.join(os.path.dirname(__file__), relative_path))
29                abs_dst = os.path.normpath(os.path.join(os.path.dirname(__file__), dst))
30                (head, tail) = os.path.split(abs_dst)
31                if not os.path.exists(head):
32                    os.makedirs(head)
33                os.rename(abs_path, abs_dst)
34
35def main():
36    dir = os.path.dirname(__file__)
37    path = sys.argv[1]
38    change_case(path)
39
40
41if __name__ == '__main__':
42    main()