Skip to content

Commit

Permalink
implemented new replace argument
Browse files Browse the repository at this point in the history
  • Loading branch information
SilenZcience committed Oct 20, 2024
1 parent 01dc69a commit cb5332b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 20 deletions.
33 changes: 32 additions & 1 deletion cat_win/src/cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ def _show_debug(args: list, unknown_args: list, known_files: list, unknown_files
('str(' + ('CI' if c else 'CS') + '):' if isinstance(v, str) else 're:') + str(v)
for v, c in arg_parser.file_queries
))
err_print('replace queries: ', end='')
err_print(repr(arg_parser.file_queries_replacement))
err_print('truncate file: ', end='')
err_print(arg_parser.file_truncate)
err_print('replace mapping: ', end='')
Expand Down Expand Up @@ -429,9 +431,36 @@ def print_file(content: list, stepper: More) -> bool:
print(*[prefix + line for prefix, line in content], sep='\n')
return False

contains_queried = False
string_finder = StringFinder(arg_parser.file_queries)

contains_queried = False
if arg_parser.file_queries and arg_parser.file_queries_replacement:
for line_prefix, line in content:
cleaned_line = remove_ansi_codes_from_line(line)
for query, ignore_case in arg_parser.file_queries:
contains_queried = True
if isinstance(query, str):
for f_s, f_e in string_finder.findLiterals(query, cleaned_line, ignore_case):
cleaned_line = (
cleaned_line[:f_s] + \
color_dic[CKW.REPLACE] + \
arg_parser.file_queries_replacement + \
color_dic[CKW.RESET_ALL] + \
cleaned_line[f_e:]
)
else:
cleaned_line = query.sub(
color_dic[CKW.REPLACE] + \
arg_parser.file_queries_replacement + \
color_dic[CKW.RESET_ALL],
cleaned_line
)
if u_args[ARGS_MORE]:
stepper.add_line(line_prefix + cleaned_line)
else:
print(line_prefix + cleaned_line)
return contains_queried

for line_prefix, line in content:
cleaned_line = remove_ansi_codes_from_line(line)
intervals, f_keywords, m_keywords = string_finder.find_keywords(cleaned_line)
Expand Down Expand Up @@ -1296,6 +1325,8 @@ def _command_see(self, _) -> None:
('str(' + ('CI' if c else 'CS') + '):' if isinstance(v, str) else '') + str(v)
for v, c in arg_parser.file_queries
))
if arg_parser.file_queries_replacement:
print(f"{'Replacement:':<12} {repr(arg_parser.file_queries_replacement)}")

def _command_exit(self, _) -> None:
self.exit_repl = True
Expand Down
8 changes: 4 additions & 4 deletions cat_win/src/service/stringfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class StringFinder:
def __init__(self, queries: set = None) -> None:
self.kw_queries = queries

def _findliterals(self, sub: str, _s: str, ignore_case: bool):
def findLiterals(self, sub: str, _s: str, ignore_case: bool):
"""
Generate lists containing the position of sub in s.
Expand All @@ -34,7 +34,7 @@ def _findliterals(self, sub: str, _s: str, ignore_case: bool):
yield [i, i+_l]
i = _s.find(sub, i+1)

def _findregex(self, pattern, _s: str):
def findregex(self, pattern, _s: str):
"""
Generate lists containing the position of pattern in s.
Expand Down Expand Up @@ -113,11 +113,11 @@ def find_keywords(self, line: str) -> tuple:

for query, ignore_case in self.kw_queries:
if isinstance(query, str):
for _f in self._findliterals(query, line, ignore_case):
for _f in self.findLiterals(query, line, ignore_case):
found_position.append(_f[:])
found_list.append((query, _f))
else:
for _m in self._findregex(query, line):
for _m in self.findregex(query, line):
matched_position.append(_m[:])
matched_list.append((query.pattern, _m))
# sort by start position (necessary for a deterministic output)
Expand Down
30 changes: 15 additions & 15 deletions cat_win/tests/src/service/test_stringfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,54 +10,54 @@
class TestStringFinder(TestCase):
def test_find_literals_true(self):
string_finder = StringFinder([])
_x = list(string_finder._findliterals('test', 'abctEStdef', True))
_x = list(string_finder.findLiterals('test', 'abctEStdef', True))
self.assertEqual(_x, [[3, 7]])

_x = list(string_finder._findliterals('TeSt', 'abctEstdef', True))
_x = list(string_finder.findLiterals('TeSt', 'abctEstdef', True))
self.assertEqual(_x, [[3, 7]])

_x = list(string_finder._findliterals('tesT', 'testabctestdetestf', True))
_x = list(string_finder.findLiterals('tesT', 'testabctestdetestf', True))
self.assertEqual(_x, [[0, 4], [7, 11], [13, 17]])

_x = list(string_finder._findliterals('', '', True))
_x = list(string_finder.findLiterals('', '', True))
self.assertEqual(_x, [[0, 0]])

_x = list(string_finder._findliterals('', 'x', True))
_x = list(string_finder.findLiterals('', 'x', True))
self.assertEqual(_x, [[0, 0], [1, 1]])

def test_find_literals_false(self):
string_finder = StringFinder([])
_x = list(string_finder._findliterals('a', '', False))
_x = list(string_finder.findLiterals('a', '', False))
self.assertEqual(_x, [])

_x = list(string_finder._findliterals('TeSt', 'abctestdef', False))
_x = list(string_finder.findLiterals('TeSt', 'abctestdef', False))
self.assertEqual(_x, [])

_x = list(string_finder._findliterals('test', 'tsetabctesdeestf', False))
_x = list(string_finder.findLiterals('test', 'tsetabctesdeestf', False))
self.assertEqual(_x, [])

def test_find_regex_true(self):
string_finder = StringFinder([])
_x = list(string_finder._findregex(re.compile(r"test", re.DOTALL | re.IGNORECASE), 'TeSt'))
_x = list(string_finder.findregex(re.compile(r"test", re.DOTALL | re.IGNORECASE), 'TeSt'))
self.assertEqual(_x, [[0, 4]])

def test_find_regex_false(self):
string_finder = StringFinder([])
_x = list(string_finder._findregex(re.compile(r"[A-Z]{1}[a-z]+\s?.*\.+\s", re.DOTALL), 'silas A. Kraume'))
_x = list(string_finder.findregex(re.compile(r"[A-Z]{1}[a-z]+\s?.*\.+\s", re.DOTALL), 'silas A. Kraume'))
self.assertEqual(_x, [])
_x = list(string_finder._findregex(re.compile(r"[0-9]{2}", re.DOTALL), '123'))
_x = list(string_finder.findregex(re.compile(r"[0-9]{2}", re.DOTALL), '123'))
self.assertEqual(_x, [[0, 2]])

_x = list(string_finder._findregex(re.compile(r"[0-9]{2}", re.DOTALL), '1234'))
_x = list(string_finder.findregex(re.compile(r"[0-9]{2}", re.DOTALL), '1234'))
self.assertEqual(_x, [[0, 2], [2, 4]])

_x = list(string_finder._findregex(re.compile(r"[A-Z]{1}[a-z]*\s?.*\.+\s", re.DOTALL), 'Silas A. Kraume'))
_x = list(string_finder.findregex(re.compile(r"[A-Z]{1}[a-z]*\s?.*\.+\s", re.DOTALL), 'Silas A. Kraume'))
self.assertEqual(_x, [[0, 9]])

_x = list(string_finder._findregex(re.compile(r"[A-Z]{1}[a-z]*\s?.*\.+\s", re.DOTALL), 'silas A. Kraume'))
_x = list(string_finder.findregex(re.compile(r"[A-Z]{1}[a-z]*\s?.*\.+\s", re.DOTALL), 'silas A. Kraume'))
self.assertEqual(_x, [[6, 9]])

_x = list(string_finder._findregex(re.compile(r"test", re.DOTALL), 'TeSt'))
_x = list(string_finder.findregex(re.compile(r"test", re.DOTALL), 'TeSt'))
self.assertEqual(_x, [])

def test_optimize_intervals(self):
Expand Down

0 comments on commit cb5332b

Please sign in to comment.