#!/usr/bin/env python3 # -*- coding: utf-8 -*- # # SSnR.py # # Copyright 2017 RĂ©mi BERTHO # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. # # import sys import os.path from os import walk import argparse import regex import pyperclip def main(): """ Main function """ # Parse arguments parser = argparse.ArgumentParser(description='Search and replace tool for UTF-8 files', prog='SSnR', allow_abbrev=False) regex_group = parser.add_argument_group('Regular expression', "Search and replace regular expression") regex_group.add_argument('-ex', '--regex', help='Regex', required=True) regex_group.add_argument('-rex', '--replace', help='Replace', required=False) input_group = parser.add_argument_group('Input', "Input arguments, if none set use stdin") input_group.add_argument('-if', '--input_file', help='Input file', required=False, nargs='+') input_group.add_argument('-iex', '--input_regex', help='Regex input file', required=False) input_group.add_argument('-str', '--input_string', help='Input string', required=False) input_group.add_argument('-ic', '--input_clipboard', help='Use the clipboard as input', required=False, action='store_true') output_group = parser.add_argument_group('Output', "In replace mode, ouput arguments, if none set use stdout") output_group.add_argument('-oex', '--output_regex', help='Regex output file', required=False) output_group.add_argument('-of', '--output_file', help='Output file', required=False) output_group.add_argument('-oc', '--output_clipboard', help='Use the clipboard as output', required=False, action='store_true') option_group = parser.add_argument_group('Options', "Some options") option_group.add_argument('-pm', '--print_nb_match', help='Print the number of match in replace', required=False, action='store_true') option_group.add_argument('-igc', '--ignore_case', help='Ignore the case', required=False, action='store_true') option_group.add_argument('-r', '--recursive', help='Use the regex input recursivly in the folders', required=False, action='store_true') args = vars(parser.parse_args()) # Compile regex try: ex = compile_regex(args["regex"], args["ignore_case"]) 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 input_filenames = [] if args["input_regex"] is not None: try: input_ex = compile_regex(args["input_regex"], False) 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): input_filenames.append(os.path.join(dirpath, filename)) is_file = True if not args["recursive"]: break if not input_filenames: print("Error: no input file") return -1 elif args["input_file"] is not None: for input_file in args["input_file"]: if os.path.isfile(input_file): is_file = True input_filenames.append(input_file) else: print("Error: file not found: " + str(args["input"])) if not input_filenames: print("Error: no input file") return -1 elif args["input_string"] is not None: string = args["input_string"] is_file = False elif args["input_clipboard"]: string = pyperclip.paste() is_file = False else: string = sys.stdin.read() is_file = False # Get output output_filenames = [] if args["output_regex"] is not None: if input_ex is None: print("Error: You need a regex input file to use a regex output file") return -1 for input_filename in input_filenames: output_filenames.append(input_ex.subn(args["output_regex"], input_filename)[0]) use_output_file = True elif args["output_file"] is not None: output_filenames.append(args["output_file"]) use_output_file = True elif args["output_clipboard"]: use_output_file = False use_output_clipboard = True else: use_output_file = False use_output_clipboard = False # Search or replace file_index = 0 if is_file: for filename in input_filenames: try: with open(filename, "r", encoding="utf8") as file: string = file.read() except OSError as exception: print("Error: file \"" + filename + "\" not found: " + str(exception)) continue except UnicodeDecodeError as exception: print("Error: the file \"" + filename + "\" is not UTF-8 encoded: " + str(exception)) continue if args["replace"] is not None: replace_string, nb_replace = replace(ex, string, args["replace"]) if use_output_file: try: output_file = open(output_filenames[file_index], "w", encoding="utf8") except OSError as exception: print("Error: file not found: " + str(exception)) return -1 output_file.write(replace_string) print("File: " + filename) print(" - Number of replace: " + str(nb_replace)) if len(output_filenames) > 1: file_index += 1 elif use_output_clipboard: pyperclip.copy(replace_string) else: print(replace_string) if args["print_nb_match"]: print("File: " + filename) print(" - Number of replace: " + str(nb_replace)) else: nb_match, str_found = search(ex, string, is_file) if nb_match > 0: print("File: " + filename) print(" - Number of match: " + str(nb_match)) print(str_found) else: if args["replace"] is not None: replace_string, nb_replace = replace(ex, string, args["replace"]) if use_output_file: print("Number of replace: " + str(nb_replace)) try: output_file = open(output_filenames[file_index], "w", encoding="utf8") except OSError as exception: print("Error: file not found: " + str(exception)) return -1 output_file.write(replace_string) elif use_output_clipboard: pyperclip.copy(replace_string) else: print(replace_string) if args["print_nb_match"]: print("Number of replace: " + str(nb_replace)) else: nb_match, str_found = search(ex, string, is_file) print("Number of match: " + str(nb_match)) print(str_found) return 0 def compile_regex(ex, ignore_case): """ Compile regex :param ex: Regular expression """ if ignore_case: regex_compile = regex.compile(ex, regex.MULTILINE | regex.IGNORECASE) else: regex_compile = regex.compile(ex, regex.MULTILINE) if regex_compile is None: raise SyntaxError('Error in the regex') else: return regex_compile def search(ex, string, is_file): """ Search in a string :param ex: Regular expression :param string: A string """ if is_file: new_lines = get_line_pos(string) ite = ex.finditer(string) nb_match = 0 str_found = "" for match in ite: nb_match += 1 if is_file: num_line, begin_pos, end_pos = find_line(match.start(0), match.end(0), new_lines) str_found += " - Found \"%s\" at line %d [%d:%d]\n" % (match.group(0), num_line, begin_pos, end_pos) else: str_found += " - Found \"%s\" at [%d:%d]\n" % (match.group(0), match.start(0), match.end(0)) return nb_match, str_found def replace(ex, string, replace_string): """ Replace in a string :param ex: Regular expression :param string: A string :param print_nb: Print the number of match """ res = ex.subn(replace_string, string) return res[0], res[1] def get_line_pos(string): """ Get new lines postion in a string :param string: a string """ ex = regex.compile("^", regex.MULTILINE) ite = ex.finditer(string) new_lines = [] for match in ite: new_lines.append(match.start(0)) return new_lines def find_line(begin_pos, end_pos, new_lines): """ Find the line number and the position in the line :param pos: the position to find the line :param new_lines: then new lines """ num_line = 0 old_pos_line = 0 for pos_line in new_lines: if pos_line > begin_pos: return num_line, begin_pos - old_pos_line + 1, end_pos - old_pos_line num_line += 1 old_pos_line = pos_line return num_line, begin_pos - old_pos_line + 1, end_pos - old_pos_line if __name__ == '__main__': sys.exit(main())