Skip to content

Commit b2dab03

Browse files
authored
Update gcs_reports.py
1 parent 9b13f94 commit b2dab03

File tree

1 file changed

+114
-34
lines changed

1 file changed

+114
-34
lines changed

scripts/3-report/gcs_reports.py

+114-34
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ def update_readme(image_path, description, section_title, args):
7676
section_marker_end = "<!-- GCS End -->"
7777
data_source_title = "## Data Source: Google Custom Search"
7878

79+
# Define section markers for each report type
80+
specific_section_start = f"<!-- {section_title} Start -->"
81+
specific_section_end = f"<!-- {section_title} End -->"
82+
data_source_title = "## Data Source: Google Custom Search"
83+
7984
# Convert image path to a relative path
8085
rel_image_path = os.path.relpath(image_path, os.path.dirname(readme_path))
8186

@@ -85,6 +90,7 @@ def update_readme(image_path, description, section_title, args):
8590
else:
8691
lines = []
8792

93+
# Main GCS Section
8894
section_start = None
8995
section_end = None
9096
for i, line in enumerate(lines):
@@ -93,24 +99,98 @@ def update_readme(image_path, description, section_title, args):
9399
if section_marker_end in line:
94100
section_end = i
95101

102+
# Check if the main section is present
96103
if section_start is None or section_end is None:
97-
# If the section does not exist, add it at the end
98-
lines.append(f"\n# {args.quarter} Quantifying the Commons\n")
99-
lines.append(f"{section_marker_start}\n")
100-
lines.append(f"{data_source_title}\n\n")
101-
lines.append(f"{section_marker_end}\n")
102-
section_start = len(lines) - 3
104+
# If the main section is not present, add it
105+
lines.extend(
106+
[
107+
f"# {args.quarter} Quantifying the Commons\n",
108+
f"{section_marker_start}\n",
109+
f"{data_source_title}\n\n",
110+
f"{section_marker_end}\n",
111+
]
112+
)
113+
section_start = len(lines) - 2
103114
section_end = len(lines) - 1
104115

105-
# Prepare the content to be added
106-
new_content = [
107-
f"\n### {section_title}\n",
108-
f"![{description}]({rel_image_path})\n",
109-
f"{description}\n",
110-
]
111-
112-
# Insert the new content before the section end marker
113-
lines = lines[:section_end] + new_content + lines[section_end:]
116+
# Locate the specific section markers within the main section
117+
specific_start = None
118+
specific_end = None
119+
for i in range(section_start, section_end):
120+
if specific_section_start in lines[i]:
121+
specific_start = i
122+
if specific_section_end in lines[i]:
123+
specific_end = i
124+
125+
# If the specific section is found, replace the content
126+
if specific_start is not None and specific_end is not None:
127+
# Prepare the new content for this specific section
128+
new_content = [
129+
f"{specific_section_start}\n",
130+
f"### {section_title}\n",
131+
f"![{description}]({rel_image_path})\n",
132+
f"{description}\n",
133+
f"{specific_section_end}\n",
134+
]
135+
# Replace the content between the specific markers
136+
lines = (
137+
lines[:specific_start] + new_content + lines[specific_end + 1 :]
138+
)
139+
else:
140+
# If the specific section does not exist, add it before the main end marker
141+
new_content = [
142+
f"{specific_section_start}\n",
143+
f"### {section_title}\n",
144+
f"![{description}]({rel_image_path})\n",
145+
f"{description}\n",
146+
f"{specific_section_end}\n",
147+
]
148+
lines = lines[:section_end] + new_content + lines[section_end:]
149+
150+
# # If markers are found, replace the content between them
151+
# if section_start is not None and section_end is not None:
152+
# # Prepare the new content to replace the old one
153+
# new_content = [
154+
# f"{section_marker_start}\n",
155+
# f"{data_source_title}\n\n",
156+
# f"### {section_title}\n",
157+
# f"![{description}]({rel_image_path})\n",
158+
# f"{description}\n",
159+
# f"{section_marker_end}\n"
160+
# ]
161+
162+
# # Replace the content between the start and end markers
163+
# lines = lines[:section_start] + new_content + lines[section_end + 1:]
164+
# else:
165+
# # If the section does not exist, add it at the end
166+
# new_content = [
167+
# f"\n{section_marker_start}\n",
168+
# f"{data_source_title}\n\n",
169+
# f"### {section_title}\n",
170+
# f"![{description}]({rel_image_path})\n",
171+
# f"{description}\n",
172+
# f"{section_marker_end}\n"
173+
# ]
174+
# lines.extend(new_content)
175+
176+
# if section_start is None or section_end is None:
177+
# # If the section does not exist, add it at the end
178+
# lines.append(f"\n# {args.quarter} Quantifying the Commons\n")
179+
# lines.append(f"{section_marker_start}\n")
180+
# lines.append(f"{data_source_title}\n\n")
181+
# lines.append(f"{section_marker_end}\n")
182+
# section_start = len(lines) - 3
183+
# section_end = len(lines) - 1
184+
185+
# # Prepare the content to be added
186+
# new_content = [
187+
# f"\n### {section_title}\n",
188+
# f"![{description}]({rel_image_path})\n",
189+
# f"{description}\n",
190+
# ]
191+
192+
# # Insert the new content before the section end marker
193+
# lines = lines[:section_end] + new_content + lines[section_end:]
114194

115195
# Write back to the README.md file
116196
with open(readme_path, "w") as f:
@@ -336,12 +416,12 @@ def visualize_by_language(data, args):
336416

337417
def main():
338418

339-
try:
340-
# Fetch and merge changes
341-
shared.fetch_and_merge(PATHS["repo"])
342-
except shared.GitOperationError as e:
343-
LOGGER.error(f"Fetch and merge failed: {e}")
344-
sys.exit(e.exit_code)
419+
# try:
420+
# # Fetch and merge changes
421+
# shared.fetch_and_merge(PATHS["repo"])
422+
# except shared.GitOperationError as e:
423+
# LOGGER.error(f"Fetch and merge failed: {e}")
424+
# sys.exit(e.exit_code)
345425

346426
args = parse_arguments()
347427

@@ -356,19 +436,19 @@ def main():
356436
visualize_by_license_type(data, args)
357437
visualize_by_language(data, args)
358438

359-
try:
360-
# Add and commit changes
361-
shared.add_and_commit(PATHS["repo"], "Added and committed new reports")
362-
except shared.GitOperationError as e:
363-
LOGGER.error(f"Add and commit failed: {e}")
364-
sys.exit(e.exit_code)
365-
366-
try:
367-
# Push changes
368-
shared.push_changes(PATHS["repo"])
369-
except shared.GitOperationError as e:
370-
LOGGER.error(f"Push changes failed: {e}")
371-
sys.exit(e.exit_code)
439+
# try:
440+
# # Add and commit changes
441+
# shared.add_and_commit(PATHS["repo"], "Added and committed new reports")
442+
# except shared.GitOperationError as e:
443+
# LOGGER.error(f"Add and commit failed: {e}")
444+
# sys.exit(e.exit_code)
445+
446+
# try:
447+
# # Push changes
448+
# shared.push_changes(PATHS["repo"])
449+
# except shared.GitOperationError as e:
450+
# LOGGER.error(f"Push changes failed: {e}")
451+
# sys.exit(e.exit_code)
372452

373453

374454
if __name__ == "__main__":

0 commit comments

Comments
 (0)