From bf0b75dfc170fea9d1a73021a66bbc29f8704549 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20BERTHO?= Date: Sat, 21 Oct 2017 14:57:10 +0200 Subject: [PATCH] Add multiple input files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: RĂ©mi BERTHO --- SSnR.py | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/SSnR.py b/SSnR.py index f07cbdf..6b65030 100755 --- a/SSnR.py +++ b/SSnR.py @@ -24,6 +24,7 @@ import sys import os.path +from os import walk import argparse import regex @@ -35,12 +36,15 @@ def main(): parser.add_argument('-e', '--regex', help='Regex', required=True) parser.add_argument('-s', '--string', help='String', required=False) parser.add_argument('-i', '--input', help='Input file', required=False, nargs='+') + parser.add_argument('-p', '--regex_input', help='Regex input 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('-m', '--print_nb_match', help='Print the number of match in replace', required=False, action='store_true') parser.add_argument('-c', '--ignore_case', help='Ignore the case', required=False, action='store_true') + parser.add_argument('-u', '--recursive', help='Use the regex input recrusivly in the folders', + required=False, action='store_true') args = vars(parser.parse_args()) @@ -56,14 +60,33 @@ def main(): # Get input filenames = [] - if args["input"] is not None: + if args["regex_input"] is not None: + try: + input_ex = compile_regex(args["regex_input"]) + except SyntaxError as exception: + print("Error when compiling input regex: " + str(exception)) + return -1 + except regex.error as exception: + print("Error when compiling input regex: " + exception.msg) + return -1 + for (dirpath, dirnames, dir_filenames) in walk("."): + for filename in dir_filenames: + if input_ex.fullmatch(filename): + filenames.append(os.path.join(dirpath, filename)) + is_file = True + if not args["recursive"]: + break + if not filenames: + print("Error: no input file") + return -1 + elif args["input"] is not None: for input_file in args["input"]: if os.path.isfile(input_file): is_file = True filenames.append(input_file) else: - print("Error: file not found: " + args["input"]) - if filenames.count == 0: + print("Error: file not found: " + str(args["input"])) + if not filenames: print("Error: no input file") return -1 elif args["string"] is not None: