Add multiple input files
Signed-off-by: Rémi BERTHO <remi.bertho@dalan.fr>
This commit is contained in:
parent
25d6729e03
commit
9e48368148
1 changed files with 48 additions and 25 deletions
73
SSnR.py
73
SSnR.py
|
@ -23,6 +23,7 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
import os.path
|
||||||
import argparse
|
import argparse
|
||||||
import regex
|
import regex
|
||||||
|
|
||||||
|
@ -33,7 +34,7 @@ def main():
|
||||||
parser = argparse.ArgumentParser(description='Search and replace tool', prog='SSnR')
|
parser = argparse.ArgumentParser(description='Search and replace tool', prog='SSnR')
|
||||||
parser.add_argument('-e', '--regex', help='Regex', required=True)
|
parser.add_argument('-e', '--regex', help='Regex', required=True)
|
||||||
parser.add_argument('-s', '--string', help='String', required=False)
|
parser.add_argument('-s', '--string', help='String', required=False)
|
||||||
parser.add_argument('-i', '--input', help='Input file', required=False)
|
parser.add_argument('-i', '--input', help='Input file', required=False, nargs='+')
|
||||||
parser.add_argument('-o', '--output', help='Output file', required=False)
|
parser.add_argument('-o', '--output', help='Output file', required=False)
|
||||||
parser.add_argument('-r', '--replace', help='Replace', required=False)
|
parser.add_argument('-r', '--replace', help='Replace', required=False)
|
||||||
parser.add_argument('-m', '--print_nb_match', help='Print the number of match in replace',
|
parser.add_argument('-m', '--print_nb_match', help='Print the number of match in replace',
|
||||||
|
@ -43,15 +44,28 @@ def main():
|
||||||
|
|
||||||
args = vars(parser.parse_args())
|
args = vars(parser.parse_args())
|
||||||
|
|
||||||
|
# Compile regex
|
||||||
|
try:
|
||||||
|
ex = compile_regex(args["regex"])
|
||||||
|
except SyntaxError as exception:
|
||||||
|
print("Error when compiling regex: " + str(exception))
|
||||||
|
return -1
|
||||||
|
except regex.error as exception:
|
||||||
|
print("Error when compiling regex: " + exception.msg)
|
||||||
|
return -1
|
||||||
|
|
||||||
# Get input
|
# Get input
|
||||||
|
filenames = []
|
||||||
if args["input"] is not None:
|
if args["input"] is not None:
|
||||||
try:
|
for input_file in args["input"]:
|
||||||
with open(args["input"], "r") as file:
|
if os.path.isfile(input_file):
|
||||||
string = file.read()
|
is_file = True
|
||||||
except OSError as exception:
|
filenames.append(input_file)
|
||||||
print("Error: file not found: " + str(exception))
|
else:
|
||||||
|
print("Error: file not found: " + args["input"])
|
||||||
|
if filenames.count == 0:
|
||||||
|
print("Error: no input file")
|
||||||
return -1
|
return -1
|
||||||
is_file = True
|
|
||||||
elif args["string"] is not None:
|
elif args["string"] is not None:
|
||||||
string = args["string"]
|
string = args["string"]
|
||||||
is_file = False
|
is_file = False
|
||||||
|
@ -70,25 +84,34 @@ def main():
|
||||||
else:
|
else:
|
||||||
use_output_file = False
|
use_output_file = False
|
||||||
|
|
||||||
# Compile regex
|
|
||||||
try:
|
|
||||||
ex = compile_regex(args["regex"])
|
|
||||||
except SyntaxError as exception:
|
|
||||||
print("Error when compiling regex: " + str(exception))
|
|
||||||
return -1
|
|
||||||
except regex.error as exception:
|
|
||||||
print("Error when compiling regex: " + exception.msg)
|
|
||||||
return -1
|
|
||||||
|
|
||||||
# Search or replace
|
# Search or replace
|
||||||
if args["replace"] is not None:
|
if is_file:
|
||||||
replace_string = replace(ex, string, args["replace"], args["print_nb_match"])
|
for filename in filenames:
|
||||||
if use_output_file:
|
try:
|
||||||
output_file.write(replace_string)
|
with open(filename, "r") as file:
|
||||||
else:
|
string = file.read()
|
||||||
print(replace_string)
|
except OSError as exception:
|
||||||
|
print("Error: file not found: " + str(exception))
|
||||||
|
continue
|
||||||
|
|
||||||
|
if args["replace"] is not None:
|
||||||
|
replace_string = replace(ex, string, args["replace"], args["print_nb_match"])
|
||||||
|
if use_output_file:
|
||||||
|
output_file.write(replace_string)
|
||||||
|
else:
|
||||||
|
print(replace_string)
|
||||||
|
else:
|
||||||
|
print("File: " + filename)
|
||||||
|
search(ex, string, is_file)
|
||||||
else:
|
else:
|
||||||
search(ex, string, is_file)
|
if args["replace"] is not None:
|
||||||
|
replace_string = replace(ex, string, args["replace"], args["print_nb_match"])
|
||||||
|
if use_output_file:
|
||||||
|
output_file.write(replace_string)
|
||||||
|
else:
|
||||||
|
print(replace_string)
|
||||||
|
else:
|
||||||
|
search(ex, string, is_file)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def compile_regex(ex):
|
def compile_regex(ex):
|
||||||
|
@ -123,7 +146,7 @@ def search(ex, string, is_file):
|
||||||
else:
|
else:
|
||||||
print(" - Found \"" + match.group(0) + "\" at [" + str(match.start(0) + 1) +
|
print(" - Found \"" + match.group(0) + "\" at [" + str(match.start(0) + 1) +
|
||||||
":" + str(match.end(0)) + "]")
|
":" + str(match.end(0)) + "]")
|
||||||
print("Number of match: " + str(nb_match))
|
print(" - Number of match: " + str(nb_match))
|
||||||
|
|
||||||
|
|
||||||
def replace(ex, string, replace_string, print_nb):
|
def replace(ex, string, replace_string, print_nb):
|
||||||
|
|
Loading…
Reference in a new issue