Created
May 29, 2022 10:28
-
-
Save entirelymagic/b5f3a475f23069f75fee257f933d9b8c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import count | |
import re | |
import os | |
import sys | |
import_1 = 'from django.utils.translation import ugettext.*' | |
regex_text_1 = r' _\(.*' | |
regex_text_2 = r'[^_](_\()[^()]*\)' | |
front_remove = r' _\(' | |
last_remove = r'\)' | |
_include_format = r'\.format' | |
#having import_1 in the file remove the line from the file and create a removed.txt file that inclide all the file names that have import_1 and have been removed | |
def remove_text_from_file(file_name, regex_text): | |
""" | |
find the line with the regex text in it using regex. | |
remove the line from the file | |
append the file_name to the removed.txt file | |
""" | |
with open(file_name, 'r') as file: | |
lines = file.readlines() | |
with open(file_name, 'w') as file: | |
for line in lines: | |
if not re.search(regex_text, line): | |
file.write(line) | |
else: | |
with open('removed.txt', 'a') as removed_file: | |
removed_file.write(file_name + '\n') | |
def remove_text_from_files(text): | |
""" | |
Remove all lines from all files from all subdirectories from this directory that contain the text but not from current file | |
create a new file if not exists, if exists append the file_name to the removed.txt file | |
""" | |
for root, dirs, files in os.walk(os.getcwd()): | |
for file in files: | |
if file.endswith('.py') and file != 'remove_text_from_files.py': | |
remove_text_from_file(os.path.join(root, file), text) | |
def min_remove_to_make_valid(S: str) -> str: | |
S, stack = list(S), [] | |
for i, c in enumerate(S): | |
if c == ")": | |
if stack: stack.pop() | |
else: S[i] = "" | |
elif c == "(": stack.append(i) | |
for i in stack: S[i] = "" | |
return "".join(S) | |
def remove_the_first_2_chars_and_last_accolade_from_text_in_file(file, regex_text): | |
""" | |
given the file, extraxt the list of files presetn in the file | |
for each file in the list, find the line with the regex text in it using regex. | |
if the regex text is found do the following: | |
- remove the first 2 chars from the line | |
-search the last accolade in the line and remove it | |
""" | |
with open(file, 'r') as file1: | |
lines = file1.readlines() | |
with open(file, 'w') as file2: | |
for line in lines: | |
if not re.search(regex_text, line): | |
file2.write(line) | |
else: | |
# if in the line include _include_format pass the line, else remove the first 2 chars and the last accolade | |
if re.search(_include_format, line): | |
file2.write(line) | |
else: | |
line = re.sub(front_remove, ' ', line) | |
#remove only the first encountered last_remove and leave the other ones | |
line = re.sub(last_remove, '', line[::-1], count=1) | |
# add 1 space at the begining of the line | |
file2.write(line[::-1]) | |
def get_list_of_files_from_text_file(file): | |
""" | |
given the file, extraxt the list of files presetn in the file without the r"\n" and return them | |
""" | |
list_of_files = [] | |
with open(file, 'r') as file: | |
lines = file.readlines() | |
for line in lines: | |
list_of_files.append(str(line.replace('\n', ''))) | |
return list_of_files | |
def remove_second_and_3rd_chars_and_solve_paranthesis_from_text_in_file(file, regex_text): | |
""" | |
Using regex_text 2 extract all patterns from the file. | |
Replace the text extracted by deleting the second and 3rd char and remove the closing parantheses asociated with second deleted char | |
""" | |
with open(file, 'r') as file1: | |
lines = file1.readlines() | |
with open(file, 'w') as file2: | |
for line in lines: | |
if not re.search(regex_text, line): | |
file2.write(line) | |
else: | |
# if in the line include _include_format pass the line, else remove the first 2 chars and the last accolade | |
if re.search(_include_format, line): | |
file2.write(line) | |
else: | |
# asign to a new variable the regex_text extracted from the line | |
line_regex_text = re.search(regex_text, line) | |
# remove second char | |
line = line.replace(line_regex_text.group(1), '') | |
# remove the last parantheses | |
line = re.sub(last_remove, '', line[::-1], count=1) | |
file2.write(line[::-1]) | |
remove_text_from_files(import_1) | |
list_of_files = get_list_of_files_from_text_file('removed.txt') | |
for file in list_of_files: | |
remove_the_first_2_chars_and_last_accolade_from_text_in_file(file, regex_text_1) | |
for file in list_of_files: | |
remove_second_and_3rd_chars_and_solve_paranthesis_from_text_in_file(file, regex_text_2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment