2017-10-08 16:12:27 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- 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
|
2017-10-08 16:25:04 +00:00
|
|
|
import argparse
|
2017-10-08 16:12:27 +00:00
|
|
|
import regex
|
|
|
|
|
2017-10-08 16:25:04 +00:00
|
|
|
def main():
|
2017-10-08 16:12:27 +00:00
|
|
|
"""
|
|
|
|
Main function
|
|
|
|
"""
|
2017-10-08 16:25:04 +00:00
|
|
|
parser = argparse.ArgumentParser(description='Search and replace tool', prog='SSnR')
|
2017-10-13 16:36:54 +00:00
|
|
|
parser.add_argument('-e', '--regex', help='Regex', required=True)
|
2017-10-08 16:25:04 +00:00
|
|
|
parser.add_argument('-s', '--string', help='String', required=True)
|
2017-10-13 16:36:54 +00:00
|
|
|
parser.add_argument('-r', '--replace', help='Replace', required=False)
|
2017-10-13 16:48:50 +00:00
|
|
|
parser.add_argument('-m', '--print_nb_match', help='Print the number of match in replace',
|
|
|
|
required=False, action='store_true')
|
2017-10-08 16:25:04 +00:00
|
|
|
args = vars(parser.parse_args())
|
2017-10-08 16:12:27 +00:00
|
|
|
|
2017-10-12 18:54:29 +00:00
|
|
|
# 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
|
|
|
|
|
2017-10-13 16:36:54 +00:00
|
|
|
if args["replace"] is not None:
|
2017-10-13 16:48:50 +00:00
|
|
|
replace(ex, args["string"], args["replace"], args["print_nb_match"])
|
2017-10-13 16:36:54 +00:00
|
|
|
else:
|
|
|
|
search(ex, args["string"])
|
2017-10-08 16:12:27 +00:00
|
|
|
return 0
|
|
|
|
|
2017-10-12 18:54:29 +00:00
|
|
|
def compile_regex(ex):
|
|
|
|
"""
|
|
|
|
Compile regex
|
|
|
|
:param ex: Regular expression
|
|
|
|
"""
|
|
|
|
regex_compile = regex.compile(ex, regex.MULTILINE)
|
|
|
|
if regex_compile is None:
|
|
|
|
raise SyntaxError('Error in the regex')
|
|
|
|
else:
|
|
|
|
return regex_compile
|
|
|
|
|
2017-10-08 16:12:27 +00:00
|
|
|
def search(ex, string):
|
|
|
|
"""
|
|
|
|
Search in a string
|
2017-10-08 16:25:04 +00:00
|
|
|
:param ex: Regular expression
|
|
|
|
:param string: A string
|
2017-10-08 16:12:27 +00:00
|
|
|
"""
|
2017-10-12 18:54:29 +00:00
|
|
|
ite = ex.finditer(string)
|
2017-10-08 16:12:27 +00:00
|
|
|
nb_match = 0
|
|
|
|
for match in ite:
|
|
|
|
nb_match += 1
|
|
|
|
print(" - Found \"" + match.group(0) + "\" at [" + str(match.start(0)) +
|
|
|
|
":" + str(match.end(0)) + "]")
|
|
|
|
print("Number of match: " + str(nb_match))
|
|
|
|
|
|
|
|
|
2017-10-13 16:48:50 +00:00
|
|
|
def replace(ex, string, replace_string, print_nb):
|
2017-10-13 16:36:54 +00:00
|
|
|
"""
|
|
|
|
Replace in a string
|
|
|
|
:param ex: Regular expression
|
|
|
|
:param string: A string
|
2017-10-13 16:48:50 +00:00
|
|
|
:param print_nb: Print the number of match
|
2017-10-13 16:36:54 +00:00
|
|
|
"""
|
|
|
|
res = ex.subn(replace_string, string)
|
|
|
|
print(res[0])
|
2017-10-13 16:48:50 +00:00
|
|
|
if print_nb:
|
|
|
|
print("Number of match: " + str(res[1]))
|
2017-10-13 16:36:54 +00:00
|
|
|
|
2017-10-08 16:12:27 +00:00
|
|
|
if __name__ == '__main__':
|
2017-10-08 16:25:04 +00:00
|
|
|
sys.exit(main())
|