Compare commits
No commits in common. "65dd8bf97da6745d9e76109f2b4fd43cd9420fed" and "dfd1015e8cdff47805c20e689339b09b4ef1f0e8" have entirely different histories.
65dd8bf97d
...
dfd1015e8c
1 changed files with 7 additions and 80 deletions
87
SSnR.py
87
SSnR.py
|
@ -31,30 +31,9 @@ def main():
|
||||||
Main function
|
Main function
|
||||||
"""
|
"""
|
||||||
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('-r', '--regex', help='Regex', required=True)
|
||||||
parser.add_argument('-s', '--string', help='String', required=False)
|
parser.add_argument('-s', '--string', help='String', required=True)
|
||||||
parser.add_argument('-i', '--input', help='Input file', required=False, type=open)
|
args = vars(parser.parse_args())
|
||||||
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')
|
|
||||||
try:
|
|
||||||
args = vars(parser.parse_args())
|
|
||||||
except FileNotFoundError as exception:
|
|
||||||
print("Error: file not found: " + str(exception))
|
|
||||||
return -1
|
|
||||||
|
|
||||||
# Get input
|
|
||||||
if args["input"] is not None:
|
|
||||||
string = args["input"].read()
|
|
||||||
is_file = True
|
|
||||||
elif args["string"] is not None:
|
|
||||||
string = args["string"]
|
|
||||||
is_file = False
|
|
||||||
else:
|
|
||||||
print("Error: You need an input string or file")
|
|
||||||
return -1
|
|
||||||
|
|
||||||
# Compile regex
|
# Compile regex
|
||||||
try:
|
try:
|
||||||
|
@ -66,11 +45,7 @@ def main():
|
||||||
print("Error when compiling regex: " + exception.msg)
|
print("Error when compiling regex: " + exception.msg)
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
# Search or replace
|
search(ex, args["string"])
|
||||||
if args["replace"] is not None:
|
|
||||||
replace(ex, string, args["replace"], args["print_nb_match"])
|
|
||||||
else:
|
|
||||||
search(ex, string, is_file)
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def compile_regex(ex):
|
def compile_regex(ex):
|
||||||
|
@ -79,73 +54,25 @@ def compile_regex(ex):
|
||||||
:param ex: Regular expression
|
:param ex: Regular expression
|
||||||
"""
|
"""
|
||||||
regex_compile = regex.compile(ex, regex.MULTILINE)
|
regex_compile = regex.compile(ex, regex.MULTILINE)
|
||||||
|
|
||||||
if regex_compile is None:
|
if regex_compile is None:
|
||||||
raise SyntaxError('Error in the regex')
|
raise SyntaxError('Error in the regex')
|
||||||
else:
|
else:
|
||||||
return regex_compile
|
return regex_compile
|
||||||
|
|
||||||
def search(ex, string, is_file):
|
def search(ex, string):
|
||||||
"""
|
"""
|
||||||
Search in a string
|
Search in a string
|
||||||
:param ex: Regular expression
|
:param ex: Regular expression
|
||||||
:param string: A string
|
:param string: A string
|
||||||
"""
|
"""
|
||||||
if is_file:
|
|
||||||
new_lines = get_line_pos(string)
|
|
||||||
|
|
||||||
ite = ex.finditer(string)
|
ite = ex.finditer(string)
|
||||||
nb_match = 0
|
nb_match = 0
|
||||||
for match in ite:
|
for match in ite:
|
||||||
nb_match += 1
|
nb_match += 1
|
||||||
if is_file:
|
print(" - Found \"" + match.group(0) + "\" at [" + str(match.start(0)) +
|
||||||
num_line, begin_pos, end_pos = find_line(match.start(0), match.end(0), new_lines)
|
":" + str(match.end(0)) + "]")
|
||||||
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))
|
print("Number of match: " + str(nb_match))
|
||||||
|
|
||||||
|
|
||||||
def replace(ex, string, replace_string, print_nb):
|
|
||||||
"""
|
|
||||||
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)
|
|
||||||
print(res[0])
|
|
||||||
if print_nb:
|
|
||||||
print("Number of match: " + str(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
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
sys.exit(main())
|
sys.exit(main())
|
||||||
|
|
Loading…
Reference in a new issue