swh.lister.pattern module

class swh.lister.pattern.ListerStats(pages: int = 0, origins: int = 0)[source]

Bases: object

pages: int = 0
origins: int = 0
dict()Dict[str, int][source]
class swh.lister.pattern.Lister(scheduler: swh.scheduler.interface.SchedulerInterface, url: str, instance: str, credentials: Optional[Dict[str, Dict[str, List[Dict[str, str]]]]] = None)[source]

Bases: Generic[swh.lister.pattern.StateType, swh.lister.pattern.PageType]

The base class for a Software Heritage lister.

A lister scrapes a page by page list of origins from an upstream (a forge, the API of a package manager, …), and massages the results of that scrape into a list of origins that are recorded by the scheduler backend.

The main loop of the lister, run(), basically revolves around the get_pages() iterator, which sets up the lister state, then yields the scrape results page by page. The get_origins_from_page() method converts the pages into a list of model.ListedOrigin, sent to the scheduler at every page. The commit_page() method can be used to update the lister state after a page of origins has been recorded in the scheduler backend.

The finalize() method is called at lister teardown (whether the run has been successful or not) to update the local state object before it’s sent to the database. This method must set the updated attribute if an updated state needs to be sent to the scheduler backend. This method can call get_state_from_scheduler() to refresh and merge the lister state from the scheduler before it’s finalized (and potentially minimize the risk of race conditions between concurrent runs of the lister).

The state of the lister is serialized and deserialized from the dict stored in the scheduler backend, using the state_from_dict() and state_to_dict() methods.

Parameters
  • scheduler – the instance of the Scheduler being used to register the origins listed by this lister

  • url – a URL representing this lister, e.g. the API’s base URL

  • instance – the instance name used, in conjunction with LISTER_NAME, to uniquely identify this lister instance.

  • credentials – dictionary of credentials for all listers. The first level identifies the LISTER_NAME, the second level the lister instance. The final level is a list of dicts containing the expected credentials for the given instance of that lister.

Generic types:
  • StateType: concrete lister type; should usually be a dataclass for stricter typing

  • PageType: type of scrape results; can usually be a requests.Response, or a dict

LISTER_NAME: str = ''
run()swh.lister.pattern.ListerStats[source]

Run the lister.

Returns

A counter with the number of pages and origins seen for this run of the lister.

get_state_from_scheduler()StateType[source]

Update the state in the current instance from the state in the scheduler backend.

This updates lister_obj, and returns its (deserialized) current state, to allow for comparison with the local state.

Returns

the state retrieved from the scheduler backend

set_state_in_scheduler()None[source]

Update the state in the scheduler backend from the state of the current instance.

Raises

swh.scheduler.exc.StaleData

:raises concurrent listers (from swh.scheduler.Scheduler.update_lister()).:

state_from_dict(d: Dict[str, Any])StateType[source]

Convert the state stored in the scheduler backend (as a dict), to the concrete StateType for this lister.

state_to_dict(state: StateType)Dict[str, Any][source]

Convert the StateType for this lister to its serialization as dict for storage in the scheduler.

Values must be JSON-compatible as that’s what the backend database expects.

finalize()None[source]

Custom hook to finalize the lister state before returning from the main loop.

This method must set updated if the lister has done some work.

If relevant, this method can use :meth`get_state_from_scheduler` to merge the current lister state with the one from the scheduler backend, reducing the risk of race conditions if we’re running concurrent listings.

This method is called in a finally block, which means it will also run when the lister fails.

get_pages()Iterator[PageType][source]

Retrieve a list of pages of listed results. This is the main loop of the lister.

Returns

an iterator of raw pages fetched from the platform currently being listed.

get_origins_from_page(page: PageType)Iterator[swh.scheduler.model.ListedOrigin][source]

Extract a list of model.ListedOrigin from a raw page of results.

Parameters

page – a single page of results

Returns

an iterator for the origins present on the given page of results

commit_page(page: PageType)None[source]

Custom hook called after the current page has been committed in the scheduler backend.

This method can be used to update the state after a page of origins has been successfully recorded in the scheduler backend. If the new state should be recorded at the point the lister completes, the updated attribute must be set.

send_origins(origins: Iterable[swh.scheduler.model.ListedOrigin])int[source]

Record a list of model.ListedOrigin in the scheduler.

Returns

the number of listed origins recorded in the scheduler

classmethod from_config(scheduler: Dict[str, Any], **config: Any)[source]

Instantiate a lister from a configuration dict.

This is basically a backwards-compatibility shim for the CLI.

Parameters
  • scheduler – instantiation config for the scheduler

  • config – the configuration dict for the lister, with the following keys: - credentials (optional): credentials list for the scheduler - any other kwargs passed to the lister.

Returns

the instantiated lister

classmethod from_configfile(**kwargs: Any)[source]

Instantiate a lister from the configuration loaded from the SWH_CONFIG_FILENAME envvar, with potential extra keyword arguments if their value is not None.

Parameters

kwargs – kwargs passed to the lister instantiation

class swh.lister.pattern.StatelessLister(scheduler: swh.scheduler.interface.SchedulerInterface, url: str, instance: str, credentials: Optional[Dict[str, Dict[str, List[Dict[str, str]]]]] = None)[source]

Bases: swh.lister.pattern.Lister[None, swh.lister.pattern.PageType], Generic[swh.lister.pattern.PageType]

state_from_dict(d: Dict[str, Any])None[source]

Always return empty state

state_to_dict(state: None)Dict[str, Any][source]

Always set empty state