1616
1717from sphinx .application import Sphinx
1818from sphinx .environment import BuildEnvironment
19- from sphinx_needs .data import SphinxNeedsData
19+ from sphinx_needs .data import NeedsMutable , SphinxNeedsData , NeedsInfoType
2020from sphinx_needs .logging import get_logger
2121
2222from src .extensions .score_source_code_linker .parse_source_files import GITHUB_BASE_URL
@@ -59,6 +59,21 @@ def setup(app: Sphinx) -> dict[str, str | bool]:
5959 }
6060
6161
62+ def find_need (
63+ all_needs : NeedsMutable , id : str , prefixes : list [str ]
64+ ) -> NeedsInfoType | None :
65+ if id in all_needs :
66+ return all_needs [id ]
67+
68+ # Try all possible prefixes
69+ for prefix in prefixes :
70+ prefixed_id = f"{ prefix } { id } "
71+ if prefixed_id in all_needs :
72+ return all_needs [prefixed_id ]
73+
74+ return None
75+
76+
6277# re-qid: gd_req__req__attr_impl
6378def add_source_link (app : Sphinx , env : BuildEnvironment ) -> None :
6479 """
@@ -77,28 +92,25 @@ def add_source_link(app: Sphinx, env: BuildEnvironment) -> None:
7792 p5 = Path (__file__ ).parents [5 ]
7893
7994 if str (p5 ).endswith ("src" ):
80- LOGGER .info ("DEBUG: WE ARE IN THE IF" )
95+ LOGGER .debug ("DEBUG: WE ARE IN THE IF" )
8196 path = str (p5 .parent / Path (app .confdir ).name / "score_source_code_parser.json" )
8297 else :
83- LOGGER .info ("DEBUG: WE ARE IN THE ELSE" )
98+ LOGGER .debug ("DEBUG: WE ARE IN THE ELSE" )
8499 path = str (p5 / "score_source_code_parser.json" )
85100
86101 if app .config .score_source_code_linker_file_overwrite :
87102 path = app .config .score_source_code_linker_file_overwrite
88103
104+ # For some reason the prefix 'sphinx_needs internally' is CAPSLOCKED.
105+ # So we have to make sure we uppercase the prefixes
106+ prefixes = [x ["id_prefix" ].upper () for x in app .config .needs_external_needs ]
89107 try :
90108 with open (path ) as f :
91109 gh_json = json .load (f )
92110 for id , link in gh_json .items ():
93111 id = id .strip ()
94- try :
95- # NOTE: Removing & adding the need is important to make sure
96- # the needs gets 're-evaluated'.
97- need = needs_copy [id ] # NeedsInfoType
98- Needs_Data .remove_need (need ["id" ])
99- need ["source_code_link" ] = "," .join (link )
100- Needs_Data .add_need (need )
101- except KeyError :
112+ need = find_need (needs_copy , id , prefixes )
113+ if need is None :
102114 # NOTE: manipulating link to remove git-hash,
103115 # making the output file location more readable
104116 files = [x .replace (GITHUB_BASE_URL , "" ).split ("/" , 1 )[- 1 ] for x in link ]
@@ -107,6 +119,13 @@ def add_source_link(app: Sphinx, env: BuildEnvironment) -> None:
107119 + f"Found in file(s): { files } " ,
108120 type = "score_source_code_linker" ,
109121 )
122+ continue
123+
124+ # NOTE: Removing & adding the need is important to make sure
125+ # the needs gets 're-evaluated'.
126+ Needs_Data .remove_need (need ["id" ])
127+ need ["source_code_link" ] = "," .join (link )
128+ Needs_Data .add_need (need )
110129 except Exception as e :
111130 LOGGER .warning (
112131 f"An unexpected error occurred while adding source_code_links to needs."
0 commit comments