Coverage for python/felis/browser/navdata.py: 100%
28 statements
« prev ^ index » next coverage.py v7.17.0, created at 2026-10-30 08:42 +0000
« prev ^ index » next coverage.py v7.17.0, created at 2026-10-30 08:42 +0000
1"""The navigation data shared by all pages, for the sidebar tree and the
2search.
3"""
5# This file is part of felis.
6#
7# Developed for the LSST Data Management System.
8# This product includes software developed by the LSST Project
9# (https://www.lsst.org).
10# See the COPYRIGHT file at the top-level directory of this distribution
11# for details of code ownership.
12#
13# This program is free software: you can redistribute it and/or modify
14# it under the terms of the GNU General Public License as published by
15# the Free Software Foundation, either version 3 of the License, or
16# (at your option) any later version.
17#
18# This program is distributed in the hope that it will be useful,
19# but WITHOUT ANY WARRANTY; without even the implied warranty of
20# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21# GNU General Public License for more details.
22#
23# You should have received a copy of the GNU General Public License
24# along with this program. If not, see <https://www.gnu.org/licenses/>.
26from __future__ import annotations
28from typing import Any
30from felis.datamodel import BaseObject, Column, Schema, Table
32from .schemas import _sorted_by_name
33from .sitemap import SiteMap
36class _NavData:
37 """The navigation data shared by all pages, which the sidebar tree and the
38 search are built from.
40 It lists the schemas, their tables, and each table's columns (with UCDs),
41 indexes and constraints, sorted by name. Paths are relative to the site
42 root; empty descriptions and UCDs are left out.
43 """
45 def __init__(self, site: SiteMap) -> None:
46 self._site = site
48 def data(self) -> dict[str, Any]:
49 return {"schemas": [self._schema(schema) for schema in self._site.schemas]}
51 def _schema(self, schema: Schema) -> dict[str, Any]:
52 tables = [self._table(table) for table in _sorted_by_name(schema.tables)]
53 return self._entry(schema, path=self._site.path(schema), tables=tables)
55 def _table(self, table: Table) -> dict[str, Any]:
56 return self._entry(
57 table,
58 path=self._site.path(table),
59 anchor=self._site.anchor(table),
60 columns=[self._column(column) for column in _sorted_by_name(table.columns)],
61 indexes=[self._anchored(index) for index in _sorted_by_name(table.indexes)],
62 constraints=[self._anchored(constraint) for constraint in _sorted_by_name(table.constraints)],
63 )
65 def _column(self, column: Column) -> dict[str, Any]:
66 entry = self._anchored(column)
67 if column.ivoa_ucd:
68 entry["ucd"] = column.ivoa_ucd
69 return entry
71 def _anchored(self, obj: BaseObject) -> dict[str, Any]:
72 return self._entry(obj, anchor=self._site.anchor(obj))
74 @staticmethod
75 def _entry(obj: BaseObject, **fields: Any) -> dict[str, Any]:
76 """Return an object's entry: its name, the fields, and its description
77 if any.
78 """
79 entry = {"name": obj.name, **fields}
80 if obj.description:
81 entry["description"] = obj.description
82 return entry