-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy path__init__.py
50 lines (40 loc) · 1.72 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
Module for the file sections feature.
This feature is used to store the title and path name of each page in the index.
"""
import json
import logging
from readthedocs.builds.models import Version
from readthedocs.filesections.dataclasses import FileSectionManifest
from readthedocs.projects.constants import MEDIA_TYPE_SECTIONS
from readthedocs.storage import build_media_storage
SECTION_MANIFEST_FILE_NAME = "sections_manifest.json"
log = logging.getLogger(__name__)
def get_section_manifest(version: Version) -> FileSectionManifest | None:
storage_path = version.project.get_storage_path(
type_=MEDIA_TYPE_SECTIONS,
version_slug=version.slug,
include_file=False,
version_type=version.type,
)
manifest_path = build_media_storage.join(storage_path, SECTION_MANIFEST_FILE_NAME)
try:
with build_media_storage.open(manifest_path) as manifest_file:
manifest = json.load(manifest_file)
log.info(f"Loaded section manifest from {manifest_path}")
except FileNotFoundError:
log.warning(f"Section manifest not found at {manifest_path}")
return None
return FileSectionManifest.from_dict(manifest)
def write_section_manifest(version: Version, manifest: FileSectionManifest):
storage_path = version.project.get_storage_path(
type_=MEDIA_TYPE_SECTIONS,
version_slug=version.slug,
include_file=False,
version_type=version.type,
)
manifest_path = build_media_storage.join(storage_path, SECTION_MANIFEST_FILE_NAME)
with build_media_storage.open(manifest_path, "w") as f:
manifest_dict = manifest.as_dict()
log.info(f"Writing section manifest: {manifest_dict}")
json.dump(manifest_dict, f)