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
59
SSnR.py
59
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())
|
||||||
|
|
||||||
# Get input
|
# Compile regex
|
||||||
if args["input"] is not None:
|
|
||||||
try:
|
try:
|
||||||
with open(args["input"], "r") as file:
|
ex = compile_regex(args["regex"])
|
||||||
string = file.read()
|
except SyntaxError as exception:
|
||||||
except OSError as exception:
|
print("Error when compiling regex: " + str(exception))
|
||||||
print("Error: file not found: " + str(exception))
|
|
||||||
return -1
|
return -1
|
||||||
|
except regex.error as exception:
|
||||||
|
print("Error when compiling regex: " + exception.msg)
|
||||||
|
return -1
|
||||||
|
|
||||||
|
# Get input
|
||||||
|
filenames = []
|
||||||
|
if args["input"] is not None:
|
||||||
|
for input_file in args["input"]:
|
||||||
|
if os.path.isfile(input_file):
|
||||||
is_file = True
|
is_file = True
|
||||||
|
filenames.append(input_file)
|
||||||
|
else:
|
||||||
|
print("Error: file not found: " + args["input"])
|
||||||
|
if filenames.count == 0:
|
||||||
|
print("Error: no input file")
|
||||||
|
return -1
|
||||||
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,17 +84,26 @@ 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 is_file:
|
||||||
|
for filename in filenames:
|
||||||
|
try:
|
||||||
|
with open(filename, "r") as file:
|
||||||
|
string = file.read()
|
||||||
|
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:
|
||||||
if args["replace"] is not None:
|
if args["replace"] is not None:
|
||||||
replace_string = replace(ex, string, args["replace"], args["print_nb_match"])
|
replace_string = replace(ex, string, args["replace"], args["print_nb_match"])
|
||||||
if use_output_file:
|
if use_output_file:
|
||||||
|
@ -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