2
2
3
3
##############################################################################
4
4
# Python imports.
5
+ from argparse import Namespace
5
6
from dataclasses import dataclass
6
7
from json import dumps , loads
7
8
from webbrowser import open as visit_url
@@ -189,6 +190,18 @@ class Main(EnhancedScreen[None]):
189
190
notes : var [Notes ] = var (Notes )
190
191
"""The user's notes about PEPs."""
191
192
193
+ def __init__ (self , arguments : Namespace ) -> None :
194
+ """Initialise the main screen.
195
+
196
+ Args:
197
+ arguments: The arguments passed to the application on the command line.
198
+ """
199
+ self ._arguments = arguments
200
+ """The arguments passed on the command line."""
201
+ super ().__init__ ()
202
+ self ._jump_to_on_load : str | None = self ._arguments .pep
203
+ """A PEP to jump to once the display is loaded."""
204
+
192
205
def compose (self ) -> ComposeResult :
193
206
"""Compose the content of the main screen."""
194
207
yield Header ()
@@ -243,9 +256,31 @@ async def download_pep_data(self) -> None:
243
256
self .notify ("Fresh PEP data downloaded from the PEP API" )
244
257
self .load_pep_data ()
245
258
259
+ @staticmethod
260
+ def _extract_pep (pep : str ) -> int | None :
261
+ """Try and extract a PEP number from a string.
262
+
263
+ Args:
264
+ pep: A string that should contain a PEP number.
265
+
266
+ Returns:
267
+ A PEP number or [`None`][None] if one could not be found.
268
+
269
+ Notes:
270
+ The likes of `2342` and `PEP2342` are handled.
271
+ """
272
+ try :
273
+ return int (pep .strip ().upper ().removeprefix ("PEP" ))
274
+ except ValueError :
275
+ return None
276
+
246
277
@on (Loaded )
247
278
def load_fresh_peps (self , message : Loaded ) -> None :
248
- """React to a fresh set of PEPs being made available."""
279
+ """React to a fresh set of PEPs being made available.
280
+
281
+ Args:
282
+ message: The message letting us know we have fresh PEPs.
283
+ """
249
284
if len (message .peps .authors ) == 0 :
250
285
self .notify (
251
286
"You likely have a cached copy of the older version of the PEP data; a redownload is recommended." ,
@@ -256,9 +291,20 @@ def load_fresh_peps(self, message: Loaded) -> None:
256
291
self .all_peps = message .peps .sorted_by (config .peps_sort_order ).reversed (
257
292
config .peps_sort_reversed
258
293
)
294
+ if self ._jump_to_on_load is not None :
295
+ if (pep := self ._extract_pep (self ._jump_to_on_load )) is not None :
296
+ self .post_message (GotoPEP (pep ))
297
+ self ._jump_to_on_load = None
259
298
260
299
def on_mount (self ) -> None :
261
300
"""Configure the application once the DOM is mounted."""
301
+ # The caller has passed sorting preferences on the command line;
302
+ # let's get them into the configuration before anything else kicks
303
+ # off.
304
+ if self ._arguments .sort_by is not None :
305
+ with update_configuration () as config :
306
+ config .peps_sort_reversed = self ._arguments .sort_by [0 ] == "~"
307
+ config .peps_sort_order = self ._arguments .sort_by .removeprefix ("~" )
262
308
self .set_class (load_configuration ().details_visble , "details-visible" )
263
309
# On startup, if we've got local PEP data...
264
310
if pep_data ().exists ():
0 commit comments