Skip to content

Commit 1aee5b6

Browse files
committed
chore(gen): added line by line processing WORKING
1 parent ae8dff0 commit 1aee5b6

File tree

1 file changed

+41
-6
lines changed

1 file changed

+41
-6
lines changed

replace_concepts.py

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
import os
22

3+
34
class AddLinkOnFirstConcept:
45

6+
57
def __init__(self):
68
pass
79

810

911
def to_kebab_case(self, value):
1012
return "-".join(value.lower().split())
1113

14+
1215
def select_folder_to_process(self):
1316
product=input("enter the product to process (in the \"category/product\" format: ")
1417
return product
1518

19+
1620
# This function retrieves the concepts of a product and , and store them as a list of list ([concept1, url1], [concept2, url2])
1721
def create_concepts_list(self, product):
1822
# gets current working directory
@@ -36,6 +40,7 @@ def create_concepts_list(self, product):
3640
concepts_list.append(concept_specs)
3741
return concepts_list
3842

43+
3944
def create_files_to_update_list(self, product):
4045
files_list = []
4146
for dirpath, dirnames, filenames in os.walk(product):
@@ -44,7 +49,21 @@ def create_files_to_update_list(self, product):
4449
file_path = f"{dirpath}/{filename}"
4550
files_list.append(file_path)
4651
return files_list
52+
53+
54+
def check_if_concept_in_other_concept(concepts_list):
55+
concepts = concepts_list
56+
control_list = concepts_list
57+
skip_concept = False
4758

59+
for i in concepts:
60+
for j in control_list:
61+
if i in j:
62+
skip_concept = True
63+
print(f"Concept \"{i}\" is contained in \n{j}\n")
64+
return skip_concept
65+
66+
# Working implementation
4867
def replace_string_in_file(self, current_file, old_string, new_string):
4968
with open(current_file, "r+") as file_to_process:
5069
content = file_to_process.read()
@@ -55,6 +74,20 @@ def replace_string_in_file(self, current_file, old_string, new_string):
5574
file_to_process.write(new_content)
5675
return
5776

77+
# Version with line-by-line processing
78+
def line_by_line_replace(self, current_file, old_string, new_string):
79+
with open(current_file, "r") as file_to_process:
80+
lines_of_file = file_to_process.readlines()
81+
# iterates over each line of the file
82+
for i in range(len(lines_of_file)):
83+
if old_string in lines_of_file[i]:
84+
# replace concept once in the line
85+
lines_of_file[i] = lines_of_file[i].replace(old_string, new_string, 1)
86+
break
87+
with open(current_file, "w") as file_to_write:
88+
file_to_write.writelines(lines_of_file)
89+
return
90+
5891
def replace(self):
5992
# product = self.select_folder_to_process()
6093
product = "serverless/jobs"
@@ -63,17 +96,19 @@ def replace(self):
6396
# Looks for each concept in each page
6497
for file in files_list:
6598
for concept in concepts_list:
99+
skip_concept = False
66100
old_string = concept[0]
67101
new_string = f"[{concept[0]}]({concept[1]})"
68102
current_file=file
69103
# Check if concept already has link to concepts page
70104
with open(file) as file_to_check:
71-
if new_string not in file_to_check.read():
72-
self.replace_string_in_file(current_file, old_string, new_string)
73-
# Add test new content and error handling before printing line below
74-
print(f"{old_string} replaced by {new_string} in file {file}.")
75-
else:
76-
print(f"{new_string} already in {file_to_check}")
105+
if new_string not in file_to_check.read() and skip_concept == False:
106+
self.line_by_line_replace(current_file, old_string, new_string)
107+
# Add test new content and error handling before printing line below
108+
print(f"{old_string} replaced by {new_string} in file {file}.")
109+
else:
110+
print(f"{new_string} already in {file_to_check}")
111+
file_to_check.close()
77112
return
78113

79114

0 commit comments

Comments
 (0)