-
Notifications
You must be signed in to change notification settings - Fork 0
/
py3stringReplace.py
234 lines (197 loc) · 11.3 KB
/
py3stringReplace.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
Description:
py3str_Replace.py replaces strings in a text file with other strings from a replacement table.
- The replacement table is a text file with the first space used as a delimiter or first space after quoted text.
- New file is UTF-8 encoded unless --outputEncoding (-oe) is specified.
- Use showMatching option to see what would be replaced.
Usage: python py3stringReplace.py myinput.txt myTable.txt
#will output myinput.txt.sr.txt
Error handling documentation: docs.python.org/3.4/library/codecs.html#error-handlers
Example: www.w3schools.com/python/ref_string_encode.asp
Pick your license: GPL (any), or BSD (any), or MIT/Apache
###stop reading now###
"""
#set default options
defaultEncodingType='utf-8'
defaultReplacementListEncodingType='utf-8'
defaultConsoleEncodingType='utf-8'
#set static internal use variables
currentVersion='v0.4 - 2024Jan15'
usageHelp='\n Usage: python py3stringReplace.py myInputFile.txt myReplacementTable.txt'
import argparse #used to add command line options
import os.path #test if file exists
from pathlib import Path #override file in file system with another
import sys #end program on fail condition
import io #manipulate files (open/read/write/close)
from io import IOBase #test if variable is a file object (an "IOBase" object)
#import csv #the dream; replacementList.csv
try:
import resources.dealWithEncoding as dealWithEncoding
dealWithEncodingLibraryIsAvailable=True
except:
dealWithEncodingLibraryIsAvailable=False
#Using the 'namereplace' error handler for text encoding requires Python 3.5+, so use an older one if needed.
sysVersion=int(sys.version_info[1])
if sysVersion >= 5:
defaultOutputEncodingErrorHandler='namereplace'
elif sysVersion < 5:
defaultOutputEncodingErrorHandler='backslashreplace'
else:
sys.exit('Unspecified error.'.encode(defaultConsoleEncodingType))
#add command line options
commandLineParser=argparse.ArgumentParser(description='Description: Replaces strings in text files using a replacement table.' + usageHelp)
commandLineParser.add_argument('inputFile', help='The text file to process.',type=str)
commandLineParser.add_argument('replacementList', help='The replacementList.txt file with match pairs.',type=str)
commandLineParser.add_argument('-o', '--output', help='Specify the output file name. Default is to append \'.mod.txt\'')
commandLineParser.add_argument('-nc', '--noCopy', help='Modify the existing file instead of creating a copy. Default=create a copy. Takes precedence over -o',action='store_true')
commandLineParser.add_argument('-sm', '--showMatching', help='Show matches in stdout and exit.',action='store_true')
commandLineParser.add_argument('-e', '--encoding', help='Specify input file encoding. Will also be used for output file if -oe one is not specified. Default='+defaultEncodingType,type=str)
commandLineParser.add_argument('-rle', '--replacementListEncoding', help='Specify encoding for replacementList.txt. Default='+defaultReplacementListEncodingType,type=str)
commandLineParser.add_argument('-oe', '--outputEncoding', help='Specify output file encoding. Default='+defaultEncodingType,type=str)
commandLineParser.add_argument('-ce', '--consoleEncoding', help='Specify encoding for stdout. Default='+defaultConsoleEncodingType,default=defaultConsoleEncodingType,type=str)
commandLineParser.add_argument('-ieh', '--inputErrorHandling', help='If the wrong input codec is specified, how should the resulting conversion errors be handled? See: docs.python.org/3.4/library/codecs.html#error-handlers Default=\'strict\'.',default='strict',type=str)
commandLineParser.add_argument('-eh', '--outputErrorHandling', help='How should output conversion errors between incompatible encodings be handled? See: docs.python.org/3.4/library/codecs.html#error-handlers Default=\''+defaultOutputEncodingErrorHandler+'\'.',default=defaultOutputEncodingErrorHandler,type=str)
commandLineParser.add_argument('-v', '--version', help='Show version information and exit.',action='store_true')
commandLineParser.add_argument('-d', '--debug', help='Show generated replacementTable and exit.',action='store_true')
#parse command line settings
commandLineArguments=commandLineParser.parse_args() #magic
inputFileName=commandLineArguments.inputFile
replaceListName=commandLineArguments.replacementList
if commandLineArguments.output != None:
outputFileName=commandLineArguments.output
else:
outputFileName=inputFileName+'.mod.txt'
noCopy=commandLineArguments.noCopy
showMatching=commandLineArguments.showMatching
#The encoding of text files will be dealt with later. For now just set the stdout/console encoding.
consoleEncoding=commandLineArguments.consoleEncoding
inputErrorHandling=commandLineArguments.inputErrorHandling
outputErrorHandling=commandLineArguments.outputErrorHandling
if commandLineArguments.version == True:
sys.exit(('currentVersion:'+currentVersion).encode(consoleEncoding))
debug=commandLineArguments.debug
#Now that file input names have been decided, handle the encoding settings for those files.
#The dealWithEncoding library will internally check if the files exist before and if it attempts to open them.
#So do not deal with checking if they exist yet in the main program.
if dealWithEncodingLibraryIsAvailable == True:
#the dealWithEncoding library is available, so use it
#update internal library variables to match main program settings
dealWithEncoding.debug=debug
dealWithEncoding.consoleEncoding=consoleEncoding
#Syntax is: dealWithEncoding.ofThisFile(myFileName, rawCommandLineOption, fallbackEncoding):
inputFileEncodingType=dealWithEncoding.ofThisFile(inputFileName,commandLineArguments.encoding,defaultEncodingType)
replacementListEncodingType=dealWithEncoding.ofThisFile(replaceListName,commandLineArguments.replacementListEncoding,defaultReplacementListEncodingType)
elif dealWithEncodingLibraryIsAvailable == False:
#the dealWithEncoding library is not available, so check if an encoding was specified manually
if commandLineArguments.encoding != None:
inputFileEncodingType=commandLineArguments.encoding
else:
#just set to default and hope for the best
inputFileEncodingType=defaultEncodingType
print(('Warning: Encoding not specified for file: \''+inputFileName+'\'. Using default encoding:\''+defaultEncodingType+'\'').encode(consoleEncoding))
if commandLineArguments.replacementListEncoding != None:
replacementListEncodingType=commandLineArguments.replacementListEncoding
else:
#just set to default and hope for the best
replacementListEncodingType=defaultReplacementListEncodingType
print(('Warning: Encoding not specified for file: \''+replaceListName+'\'. Using default encoding:\''+defaultEncodingType+'\'').encode(consoleEncoding))
else:
sys.exit('Unspecified error.'.encode(consoleEncoding))
#encodingType=()commandLineArguments.encoding #Isn't this a syntax error? Huh?
#replacementListEncodingType=commandLineArguments.replacementListEncoding
#Set encoding for output file.
#if output encoding is specified
if commandLineArguments.outputEncoding != None:
#set output encoding to specified encoding
outputEncodingType=commandLineArguments.outputEncoding
#else if no output encoding is specified
elif commandLineArguments.outputEncoding == None:
#then set output encoding to utf-8
#outputEncodingType=defaultEncodingType
#Potentially, it would also be a sane setting to set the output the encoding as whatever the user input at the CLI. Yes, do that instead actually.
#If user inserted a value for inputFile encoding at the CLI,
if commandLineArguments.encoding != None:
#then use that setting
outputEncodingType=commandLineArguments.encoding
#otherwise use default value.
else:
#This could also be set to the auto detected encoding, but utf-8 is just better.
#If the user cannot be bothered to specify it, then just always output as utf-8.
outputEncodingType=defaultEncodingType
#debug code
#print('outputEncodingType'+outputEncodingType)
#print('inputFileName='+inputFileName)
#print('replaceListName='+replaceListName)
#print('outputFileName='+outputFileName)
#print('noCopy='+str(noCopy))
#print('showMatching='+str(showMatching))
#print('debug='+str(debug))
#First, check to make sure inputFile.txt and replacementList.txt actually exist
if os.path.isfile(inputFileName) != True:
sys.exit(('Error: Unable to find input file "' + inputFileName + '"' + usageHelp).encode(consoleEncoding))
if os.path.isfile(replaceListName) != True:
sys.exit(('Error. Unable to find replacement list "' + replaceListName + '"' + usageHelp).encode(consoleEncoding))
#read replacement list
replaceListFile=open(replaceListName,'r',encoding=replacementListEncodingType,errors=inputErrorHandling)
replacementTable=dict({})
#Use the following syntax to update the dictionary:
#myDictionary["color"] = "red"
#next(replaceListFile) #skip first line
for line in replaceListFile:
if len(line.strip()) != 0:
if line.count('"') == 2:
replacementTable[line.split(sep='"')[1].strip()]=line.split(sep='"')[2][1:-1].strip()
elif line.count('"') == 4:
replacementTable[str(line.split(sep='" "',maxsplit=1)[0]+'"').strip()]=str('"'+line.split(sep='" "',maxsplit=1)[1]).strip()
elif line[0] == '#':
pass
elif line[0] == ' ':
pass
elif line[0] == '':
pass
else:
replacementTable[line.split(maxsplit=1)[0]]=line.split(maxsplit=1)[1][0:-1]
replaceListFile.close()
if debug == True:
for i,v in replacementTable.items():
print(("_" + i + "_" + v +"_").encode(consoleEncoding))
sys.exit(0)
#For information on error handling: docs.python.org/3.4/library/codecs.html#error-handlers
#myInputFile=io.open(inputFileName,mode='r',encoding=inputFileEncodingType)
myInputFile=open(inputFileName,mode='r',encoding=inputFileEncodingType,errors=inputErrorHandling)
myInputFileContents=myInputFile.read()
myInputFile.close() #tidy tidy
if showMatching!=True:
#myWriteFile=io.open(outputFileName,mode='w',encoding=outputEncodingType)
myWriteFile=open(outputFileName,mode='w', encoding=outputEncodingType,errors=outputErrorHandling)
else:
myWriteFile=outputFileName
for line in myInputFileContents:
#newline=line
for i,v in replacementTable.items():
if line.rfind(i) != -1:
if showMatching==True:
print(('matchfound: ' + i).encode(consoleEncoding))
print(('matchfound: ' + i+" in " + line).encode(consoleEncoding))
print((line.replace(i,v)).encode(consoleEncoding))
print('\n')
else:
line=line.replace(i,v)
#print(newline.encode(consoleEncoding))
if showMatching!=True:
#print('wrote line')
myWriteFile.write(line)
#tidy up
if isinstance(myWriteFile, IOBase) == True:
if myWriteFile.closed != True:
myWriteFile.close()
if showMatching==True:
sys.exit(0)
if noCopy==True:
if os.path.isfile(outputFileName) == True:
Path(outputFileName).replace(inputFileName)
print(('Wrote ' + inputFileName).encode(consoleEncoding))
elif noCopy!=True:
print(('Wrote ' + outputFileName).encode(consoleEncoding))