@@ -270,7 +270,7 @@ def setup(current_file):
270270
271271
272272def section_order ():
273- report_dir = os .path .join (os .path .dirname (__file__ ), ". " )
273+ report_dir = os .path .join (os .path .dirname (__file__ ), "3-report " )
274274 report_files = os .listdir (report_dir )
275275 return report_files
276276
@@ -283,6 +283,11 @@ def update_readme(
283283 image_caption ,
284284 entry_text = None ,
285285):
286+ logger = args .logger
287+ paths = args .paths
288+ ordered_sections = section_order ()
289+ logger .info ("ordered_sections:" , ordered_sections )
290+ logger .info ("section_title:" , repr (section_title ))
286291 """
287292 Update the README.md file with the generated images and descriptions.
288293 """
@@ -299,9 +304,6 @@ def update_readme(
299304 " caption is provided"
300305 )
301306
302- logger = args .logger
303- paths = args .paths
304-
305307 readme_path = path_join (paths ["data" ], args .quarter , "README.md" )
306308
307309 # Define section markers for each data source
@@ -324,26 +326,39 @@ def update_readme(
324326 lines .insert (0 , title_line )
325327 lines .insert (1 , "\n " )
326328
327- # We only need to know the position of the end to append new entries
329+ # Locate the data source section if it is already present
328330 if section_start_line in lines :
329- # Locate the data source section if it is already present
330331 section_end_index = lines .index (section_end_line )
331332 else :
332- # Add the data source section if it is absent
333- lines .extend (
334- [
335- f"{ section_start_line } " ,
336- "\n " ,
337- "\n " ,
338- f"## { section_title } \n " ,
339- "\n " ,
340- "\n " ,
341- f"{ section_end_line } " ,
342- "\n " ,
343- ]
344- )
345- section_end_index = lines .index (section_end_line )
333+ insert_index = None
334+ # If not present, we find the position to insert the section
335+ current_postion = ordered_sections .index (section_title )
336+ # Sections that should come before this section
337+ sections_before = ordered_sections [:current_postion ]
338+ # we find the last existing section that comes before this section
339+ for prev_section in reversed (sections_before ):
340+ prev_end_line = f"<!-- section end { prev_section } -->\n "
341+ if prev_end_line in lines :
342+ insert_index = lines .index (prev_end_line ) + 1
343+ break
346344
345+ # If none exist, insert at the top (after README title)
346+ if insert_index is None :
347+ insert_index = 2 if len (lines ) >= 2 else len (lines )
348+ # Insert the new data source section at correct position
349+ new_section_line = [
350+ f"{ section_start_line } " ,
351+ "\n " ,
352+ "\n " ,
353+ f"## { section_title } \n " ,
354+ "\n " ,
355+ "\n " ,
356+ f"{ section_end_line } " ,
357+ "\n " ,
358+ ]
359+ # Insert the section at the correct position
360+ lines = lines [:insert_index ] + new_section_line + lines [insert_index :]
361+ section_end_index = lines .index (section_end_line )
347362 # Locate the entry if it is already present
348363 if entry_start_line in lines :
349364 entry_start_index = lines .index (entry_start_line )
0 commit comments