-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefactor.py
More file actions
47 lines (37 loc) · 1.45 KB
/
Copy pathrefactor.py
File metadata and controls
47 lines (37 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import os
import re
moves = {
"crawler": "core",
"scraper": "core",
"pipeline": "core",
"browser": "utils",
"detection": "utils",
"proxy": "utils",
"robots": "utils",
"screenshot": "utils",
"auth": "auth",
"default_creds": "auth",
"extractor": "extractors",
"storage": "data",
"config": "config",
}
def process_file(filepath):
with open(filepath, "r", encoding="utf-8") as f:
content = f.read()
# We need to replace imports
for mod, folder in moves.items():
# from mod import ... -> from folder.mod import ...
content = re.sub(rf"^from {mod} import ", rf"from {folder}.{mod} import ", content, flags=re.MULTILINE)
content = re.sub(rf"^from {mod}\.", rf"from {folder}.{mod}.", content, flags=re.MULTILINE)
# import mod -> from folder import mod
content = re.sub(rf"^import {mod}(\s|$)", rf"from {folder} import {mod}\1", content, flags=re.MULTILINE)
# What if it says: import x, mod? We assume they are mostly on separate lines based on our previous grep
with open(filepath, "w", encoding="utf-8") as f:
f.write(content)
for root, dirs, files in os.walk("."):
if "scrapy-master" in root or "venv" in root or "__pycache__" in root or ".git" in root:
continue
for file in files:
if file.endswith(".py") and file != "refactor.py":
process_file(os.path.join(root, file))
print("Imports updated!")