readabs.print_abs_catalogue

Print the ABS Catalogue of time-series data.

 1"""Print the ABS Catalogue of time-series data."""
 2
 3from readabs.abs_catalogue import CatalogueError, abs_catalogue
 4from readabs.download_cache import CacheError, HttpError
 5
 6# Constants for display formatting
 7DISPLAY_COLUMNS = ["Theme", "Parent Topic", "Topic", "Status"]
 8
 9
10def print_abs_catalogue(*, cache_only: bool = False, verbose: bool = False) -> None:
11    """Print a table of ABS Catalogue Numbers with their metadata.
12
13    Displays catalogue numbers that contain time-series data along with
14    their theme, parent topic, topic, and status information. The URL
15    column is excluded from the display for readability.
16
17    This is primarily a convenience function to help users identify the
18    correct catalogue number for data retrieval functions.
19
20    Parameters
21    ----------
22    cache_only : bool, default False
23        If True, only use cached catalogue data.
24    verbose : bool, default False
25        If True, print progress messages during catalogue retrieval.
26
27    Raises
28    ------
29    CatalogueError
30        If the catalogue data cannot be retrieved or processed.
31    HttpError
32        If there's a network error downloading the catalogue.
33    CacheError
34        If cache_only=True but no cached data is available.
35
36    Example
37    -------
38    >>> import readabs as ra
39    >>> ra.print_abs_catalogue()
40
41    """
42    try:
43        # Retrieve the catalogue data
44        catalogue = abs_catalogue(cache_only=cache_only, verbose=verbose)
45
46        # Validate catalogue is not empty
47        if catalogue.empty:
48            print("No catalogue data available.")
49            return
50
51        # Select columns for display (exclude URL for readability)
52        available_columns = [col for col in DISPLAY_COLUMNS if col in catalogue.columns]
53        if not available_columns:
54            print("Catalogue data does not contain expected columns.")
55            return
56
57        display_data = catalogue[available_columns]
58
59        # Generate and print markdown table
60        try:
61            markdown_output = display_data.to_markdown()
62            print(markdown_output)
63        except Exception:  # noqa: BLE001
64            print(display_data.to_string())
65
66    except (CatalogueError, HttpError, CacheError) as e:
67        print(f"Error retrieving catalogue: {e}")
68
69
70if __name__ == "__main__":
71    print_abs_catalogue()
DISPLAY_COLUMNS = ['Theme', 'Parent Topic', 'Topic', 'Status']