Created
January 6, 2020 05:32
-
-
Save junha1/3ca19ab666140b527fc9f368f785d5e9 to your computer and use it in GitHub Desktop.
Use-clauses merging script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
path = '/home/junha/Projects/foundry/' | |
import os | |
def FindFile(path, format): | |
result = [] | |
for f in os.listdir(path): | |
x = path + f | |
if os.path.isdir(x): | |
result += FindFile(x + '/', format) | |
elif(f.split('.')[-1] == format): | |
result.append(x) | |
return result | |
def FindPos(file: str): | |
x = file.split('\n') | |
n = len(x) | |
result = [] | |
for i in range(n): | |
if i >= n - 2: | |
break | |
if x[i][0:3] == 'use' and x[i+2][0:3] == 'use' and x[i+1] == '': | |
result.append(i+1) | |
return result | |
import typing | |
def ApplyWork(file: str, pos: typing.List[int]): | |
x = file.split('\n') | |
for i in sorted(pos, reverse = True): | |
del x[i] | |
result = "" | |
for l in x: | |
result += l + '\n' | |
return result[:-1] | |
for x in FindFile(path, 'rs'): | |
res = None | |
with open(x, 'r') as f: | |
s = f.read() | |
res = ApplyWork(s, FindPos(s)) | |
with open(x, 'w') as f: | |
f.write(res) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment