-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
137 lines (101 loc) · 3.71 KB
/
app.py
File metadata and controls
137 lines (101 loc) · 3.71 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from fileFindAndReplace import *
from fileSystemHelper import *
from csvParser import *
from inputHelper import *
import os
import time
working_dir = getWorkingDirectory()
step_pause_duration = 0.5
def step_pause():
time.sleep(step_pause_duration)
print("Working directory: " + working_dir)
class App():
def __init__( self ):
self.path_csv = None
self.parser = None
self.replacement_keys = None
self.replacer = None
self.path_run = None
self.file_list = None
self.extension = ''
## LOAD IN THE REPLACEMENT PAIRS
## Process the CSV file
def getReplacementKeys( self ):
self.path_csv = getValidInput("Path of the CSV containing the replacement pairings","Path does not exist. Try again.", validatePath )
self.parser = csvParser()
self.parser.setCodec("ISO-8859-1")
self.parser.setPath(self.path_csv)
print("Parsing CSV file...")
self.replacement_keys = self.parser.parseFile()
step_pause()
print("Found (" + str( len( self.replacement_keys ) ) + ") keys")
step_pause()
if( getConfirmation("Display the keys?") ):
for key in self.replacement_keys:
if(len(key) >= 2):
print( key[0] + ": " + key[1] )
## CHOOSE TARGET DIRECTORY/FILES
## Determine which files to run the find and replace operation in
def chooseTargetDirectory( self ):
self.replacer = FileFindAndReplace()
self.replacer.setCodec("ISO-8859-1")
shouldGetDirectory = True
while shouldGetDirectory:
self.path_run = getValidInput("Path of the target directory or file","Path does not exist. Try again.", validatePath)
self.file_list = []
self.extension = ''
if( os.path.isdir(self.path_run) ):
os.chdir(self.path_run);
print("Directory contains " + str( len(os.listdir(".")) ) + " files.")
self.extension = input("Please provide the file extension to use (e.g: txt)")
self.file_list = []
for _file in os.listdir("."):
if( _file.endswith("." + self.extension) ):
self.file_list.append( os.path.join(self.path_run, _file) )
print("Found " + str( len(self.file_list) ) + " files with the extension: " + self.extension )
if getConfirmation("List files found?"):
for _file in self.file_list:
print("\t" + _file)
else:
self.file_list.append(self.path_run)
if( len(self.file_list) > 0 ):
shouldGetDirectory = not getConfirmation("Use these files?")
## FIND AND REPLACE
## Run the find and replace operation on the selected files
def runFindAndReplace( self, simulated ):
shouldRun = True
while(shouldRun):
replacements = 0
if simulated:
print("Simulating find and replace")
for _file in self.file_list:
self.replacer.setPath( _file )
print("Simulating: " + _file)
file_replacements = self.replacer.simulatedReplace( self.replacement_keys )
replacements += file_replacements
print( str(file_replacements) + " replacements\r" )
step_pause()
print("Finished. Simulated (" + str(replacements) + ") replacements.")
simulated = False
shouldRun = getConfirmation("Run actual find and replace now?")
else:
for _file in self.file_list:
print("Simulating: " + _file)
self.replacer.setPath( _file )
file_replacements = self.replacer.replace( self.replacement_keys )
replacements += file_replacements
print( str(file_replacements) + " replacements\r" )
step_pause()
shouldRun = False
print("Finished. Made (" + str(replacements) + ") replacements.")
def run( self ):
# Actual process
continueProcess = True
while( continueProcess ):
os.system('cls')
self.getReplacementKeys()
self.chooseTargetDirectory()
self.runFindAndReplace( getConfirmation("Run simulation first?") )
continueProcess = getConfirmation("Process another batch?")
app = App()
app.run()