xref: /aosp_15_r20/external/google-cloud-java/owl-bot-postprocessor/synthtool/gcp/partials.py (revision 55e87721aa1bc457b326496a7ca40f3ea1a63287)
1# Copyright 2020 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import os
16import yaml
17from pathlib import Path
18from typing import Dict, List
19
20# these are the default locations to look up
21_DEFAULT_PARTIAL_FILES = [".readme-partials.yml", ".readme-partials.yaml"]
22
23
24def load_partials(files: List[str] = _DEFAULT_PARTIAL_FILES) -> Dict:
25    """
26    hand-crafted artisinal markdown can be provided in a .readme-partials.yml.
27    The following fields are currently supported:
28
29    body: custom body to include in the usage section of the document.
30    samples_body: an optional body to place below the table of contents
31        in samples/README.md.
32    introduction: a more thorough introduction than metadata["description"].
33    title: provide markdown to use as a custom title.
34    deprecation_warning: a warning to indicate that the library has been
35        deprecated and a pointer to an alternate option
36    """
37    cwd_path = Path(os.getcwd())
38    partials_file = None
39    for file in files:
40        if os.path.exists(cwd_path / file):
41            partials_file = cwd_path / file
42            break
43    if not partials_file:
44        return {}
45    with open(partials_file) as f:
46        return yaml.load(f, Loader=yaml.SafeLoader)
47