2525
2626from linksmith .settings import help_config
2727from linksmith .sphinx .inventory import InventoryManager
28+ from linksmith .sphinx .util import RemoteObjectsInv
2829from linksmith .util .data import multikeysort
2930
3031logger = logging .getLogger (__name__ )
@@ -82,7 +83,24 @@ def to_list(self):
8283 data .append (item .to_dict ())
8384 return data
8485
85- def suggest (self , project : str , term : str , threshold : int = 50 ):
86+ def get_project_documentation_url (self , project : str ) -> str :
87+ """
88+ Given a project name, attempt to resolve it via curated list, RTD, or PyPI.
89+ """
90+ logger .info (f"Attempting to resolve project from curated list: { project } " )
91+ for item in self .items :
92+ if item .name == project :
93+ return item .url
94+
95+ logger .info (f"Attempting to resolve project from Internet: { project } " )
96+ try :
97+ return RemoteObjectsInv (project ).discover ()
98+ except FileNotFoundError as ex :
99+ logger .warning (ex )
100+
101+ raise KeyError (f"Project not found: { project } " )
102+
103+ def suggest (self , project : str , term : str , threshold : int = 50 ) -> t .List [str ]:
86104 """
87105 Find occurrences for "term" in Sphinx inventory.
88106 A wrapper around sphobjinv's `suggest`.
@@ -95,20 +113,17 @@ def suggest(self, project: str, term: str, threshold: int = 50):
95113 https://sphobjinv.readthedocs.io/en/stable/cli/suggest.html
96114 https://sphobjinv.readthedocs.io/en/stable/api/inventory.html#sphobjinv.inventory.Inventory.suggest
97115 """
98- for item in self .items :
99- if item .name == project :
100- url = f"{ item .url .rstrip ('/' )} /objects.inv"
101- inv = InventoryManager (url ).soi_factory ()
102- results = inv .suggest (term , thresh = threshold )
103- if results :
104- hits = len (results )
105- logger .info (f"{ hits } hits for project/term: { project } /{ term } " )
106- return results
107- else :
108- logger .warning (f"No hits for project/term: { project } /{ term } " )
109- return []
116+ documentation_url = self .get_project_documentation_url (project )
117+ url = f"{ documentation_url .rstrip ('/' )} /objects.inv"
118+ inv = InventoryManager (url ).soi_factory ()
119+ results = inv .suggest (term , thresh = threshold )
120+ if results :
121+ hits = len (results )
122+ logger .info (f"{ hits } hits for project/term: { project } /{ term } " )
123+ return results
110124 else :
111- raise KeyError (f"Project not found: { project } " )
125+ logger .warning (f"No hits for project/term: { project } /{ term } " )
126+ return []
112127
113128
114129@click .group ()
@@ -155,7 +170,7 @@ def cli_suggest(ctx: click.Context, project: str, term: str, threshold: int = 50
155170 try :
156171 results = library .suggest (project , term , threshold = threshold )
157172 print ("\n " .join (results )) # noqa: T201
158- except Exception as ex :
173+ except ( KeyError , FileNotFoundError ) as ex :
159174 logger .error (str (ex ).strip ("'" ))
160175 sys .exit (1 )
161176
0 commit comments