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