-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
95 lines (72 loc) · 2.22 KB
/
main.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from dataclasses import replace
import os
from pathlib import Path
from re import sub
files = [
"C:\Github\Python\FileSearchReplace\TestFiles\CreateHelloWorldController.txt",
"C:\Github\Python\FileSearchReplace\TestFiles\Subdir\HelloWorldAPI.txt",
]
searchWord="HelloWorld"
ReplaceWords=[
"Product",
"Order",
"ProductOrder",
"User",
]
def slash():
if ostype() == 'linux':
return '/'
if ostype() == 'win32':
return '\\'
def camel_case(s):
# actually it just lowercase the first letter
test_str=s
# Using lower() + string slicing
# Lowercase first character of String
res = test_str[0].lower() + test_str[1:]
return str(res)
def ostype():
import sys
if sys.platform.startswith('linux'):
# Linux specific procedures
return 'linux'
if sys.platform.startswith('darwin'):
# MacOs specific procedures
return 'darwin'
if sys.platform.startswith('win32'):
# Windows specific procedures
return 'win32'
def createNewFile(oldPath, NewPath, oldWord,newWord):
file_name = Path(oldPath)
if file_name.exists():
try:
file = open(oldPath, "r")
content = file.read()
content = content.replace(oldWord, newWord)
camelCaseOldWord = camel_case(oldWord)
camelCaseNewWord = camel_case(newWord)
content = content.replace(camelCaseOldWord, camelCaseNewWord)
file2 = open(NewPath,"w")
file2.write(content)
except Exception as e:
print("[ERROR]:",e)
finally:
file.close()
file2.close()
else:
print(f"[ERROR - File does not exist]: {oldPath}")
for path in files:
oldPath = path
dirname = os.path.dirname(path)
#basename with extension
basename = os.path.basename(path)
print(dirname, basename)
for word in ReplaceWords:
newFilbaseename = basename.replace(searchWord, word)
newFilePath = f"{dirname}{slash()}{newFilbaseename}"
print(newFilePath)
createNewFile(oldPath, newFilePath, searchWord, word)
# 1. camelCase
# 2. PascalCase
# 3. snake_case
# 4. kebab-case