SSnR/SSnR.py

245 lines
7.2 KiB
Python
Executable File

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# SSnR.py
#
# Copyright 2017 Rémi BERTHO <remi.bertho@dalan.fr>
#
# 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
def main():
"""
Main function
"""
parser = argparse.ArgumentParser(description='Search and replace tool', prog='SSnR')
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('-l', '--regex_output', help='Regex 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('-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())
# 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
filenames = []
if args["regex_input"] is not None:
try:
input_ex = compile_regex(args["regex_input"], 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):
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: " + str(args["input"]))
if not filenames:
print("Error: no input file")
return -1
elif args["string"] is not None:
string = args["string"]
is_file = False
else:
string = sys.stdin.read()
is_file = False
# Get output
output_files = []
if args["regex_output"] 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 filenames:
try:
output_file = open((input_ex.subn(args["regex_output"], input_filename))[0], "w", encoding="utf8")
output_files.append(output_file)
except OSError as exception:
print("Error: file not found: " + str(exception))
return -1
use_output_file = True
elif args["output"] is not None:
try:
output_file = open(args["output"], "w", encoding="utf8")
output_files.append(output_file)
except OSError as exception:
print("Error: file not found: " + str(exception))
return -1
use_output_file = True
else:
use_output_file = False
# Search or replace
file_index = 0
if is_file:
for filename in filenames:
try:
with open(filename, "r", encoding="utf8") 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, nb_replace = replace(ex, string, args["replace"])
if use_output_file:
output_files[file_index].write(replace_string)
print("File: " + filename)
print(" - Number of replace: " + str(nb_replace))
if len(output_files) > 1:
file_index += 1
else:
print(replace_string)
if args["print_nb_match"]:
print("File: " + filename)
print(" - Number of replace: " + str(nb_replace))
else:
print("File: " + filename)
search(ex, string, is_file)
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))
output_files[0].write(replace_string)
else:
print(replace_string)
if args["print_nb_match"]:
print("Number of replace: " + str(nb_replace))
else:
search(ex, string, is_file)
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
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)
print(" - Found \"" + match.group(0) + "\" at line " + str(num_line) + " [" + str(begin_pos) +
":" + str(end_pos) + "]")
else:
print(" - Found \"" + match.group(0) + "\" at [" + str(match.start(0) + 1) +
":" + str(match.end(0)) + "]")
print(" - Number of match: " + str(nb_match))
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())