Skip to content

Commit

Permalink
Add utility methods for replacing text areas and lines
Browse files Browse the repository at this point in the history
  • Loading branch information
Chrylo committed Jun 10, 2024
1 parent b30fb45 commit 2a150ee
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions _shared/src/shared_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,59 @@ def replace_in_file(file_path: str, text: str, replacement: str) -> None:
file.write(line)


def replace_line(
text: List[str], matching_text: str, replacement: str = ""
) -> List[str]:
"""Replace the whole line which matches the given text with a replacement.
Args:
text (List[str]): All text lines to replace lines within.
text (str): The text to find the line with.
replacement (str): The replacement for line.
"""
buffer = []
for line in text:
if line.__contains__(matching_text):
line = replacement

isEmptyLine = line == ""
if isEmptyLine:
continue

buffer.append(line)

return buffer


def replace_text_area(
text: List[str], start_occurence: str, end_occurence: str, replacement: str = ""
) -> List[str]:
"""Replace all occurrences of all text areas matching the parameters with a replacement.
Args:
text (List[str]): All text lines to replace text within.
start_occurence (str): The starting line which matches the occurence for replacement text area.
start_occurence (str): The ending line which matches the occurence for replacement text area.
replacement (str): The replacement for text area.
"""
buffer = []
is_capturing = False
for line in text:
if not is_capturing and line.__contains__(start_occurence):
is_capturing = True
continue

if is_capturing:
if line.__contains__(end_occurence):
replacement = line.replace(line, replacement)
buffer.append(replacement)
is_capturing = False
else:
buffer.append(line)

return buffer


def get_valid_arch(arch: str) -> str:
"""Return a known architecture for the given `arch`.
Expand Down

0 comments on commit 2a150ee

Please sign in to comment.