forked from infra/otc-metadata
- add script to generate metadata file for doc-exports repository - fix metadata issues which became visible with that script
132 lines
4.3 KiB
Python
132 lines
4.3 KiB
Python
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
# implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
import copy
|
|
|
|
import otc_metadata.data
|
|
|
|
__all__ = ['Services']
|
|
|
|
BUILTIN_DATA = otc_metadata.data.read_data('services.yaml')
|
|
|
|
|
|
def _normalize_type(service_type):
|
|
if service_type:
|
|
return service_type.replace('_', '-')
|
|
|
|
|
|
class Services(object):
|
|
"""Encapsulation of the OTC Services data
|
|
"""
|
|
|
|
def __init__(self):
|
|
self._service_data = BUILTIN_DATA
|
|
|
|
def _sorted_data(self):
|
|
self._service_data['documents'] = sorted(
|
|
self._service_data['documents'],
|
|
key=lambda x: f"{x.get('service_type')}{x.get('title')}"
|
|
)
|
|
self._service_data['services'] = sorted(
|
|
self._service_data['services'],
|
|
key=lambda x: f"{x.get('service_type')}{x.get('service_title')}"
|
|
)
|
|
|
|
def _rewrite_data(self):
|
|
otc_metadata.data.rewrite_data(
|
|
"services.yaml",
|
|
self._service_data
|
|
)
|
|
|
|
@property
|
|
def all_services(self):
|
|
"Service Categories data listing."
|
|
return copy.deepcopy(self._service_data['services'])
|
|
|
|
@property
|
|
def all_docs(self):
|
|
"Service Docs data listing."
|
|
return copy.deepcopy(self._service_data['documents'])
|
|
|
|
@property
|
|
def service_dict(self):
|
|
"Service Docs data listing."
|
|
res = dict()
|
|
for srv in self.all_services:
|
|
res[srv["service_type"]] = copy.deepcopy(srv)
|
|
return res
|
|
|
|
def services_by_category(self, category):
|
|
"""List services matching category
|
|
"""
|
|
res = []
|
|
for srv in self.all_services:
|
|
if srv['service_category'] == category:
|
|
res.append(copy.deepcopy(srv))
|
|
return res
|
|
|
|
def docs_by_service_category(self, category, env=None):
|
|
"""List services matching category
|
|
|
|
:param str category: Category name
|
|
:param str env: Optional service environment. Influeces "repository"
|
|
field
|
|
"""
|
|
res = []
|
|
services = self.service_dict
|
|
for doc in self.all_docs:
|
|
service = services[doc["service_type"]]
|
|
if service["service_category"] == category:
|
|
res_doc = copy.deepcopy(doc)
|
|
res_doc.update(**service)
|
|
if env:
|
|
for srv_env in service["repositories"]:
|
|
if srv_env.get("env") == env:
|
|
res_doc["repository"] = srv_env["repo"]
|
|
res.append(res_doc)
|
|
return res
|
|
|
|
def docs_html_by_category(self, environment):
|
|
"""Generate structure for doc-exports repository
|
|
|
|
"""
|
|
doc_struct = dict()
|
|
services = self.service_dict
|
|
for srv in self.all_services:
|
|
doc_struct.setdefault(srv["service_category"], [])
|
|
srv_res = dict(
|
|
service_title=srv['service_title'],
|
|
service_type=srv['service_type'],
|
|
docs=[]
|
|
)
|
|
if "repositories" in srv and environment:
|
|
for repo in srv["repositories"]:
|
|
if "environment" in repo and repo["environment"] == environment:
|
|
srv_res["repository"] = repo["repo"]
|
|
for doc in self.all_docs:
|
|
if (
|
|
"html_location" in doc
|
|
and doc["service_type"] == srv_res["service_type"]
|
|
):
|
|
doc_res = dict(
|
|
html_location=doc["html_location"],
|
|
rst_location=doc["rst_location"],
|
|
title=doc["title"],
|
|
pdf_name=doc["pdf_name"]
|
|
)
|
|
srv_res["docs"].append(doc_res)
|
|
if len(srv_res["docs"]) > 0:
|
|
doc_struct[srv["service_category"]].append(srv_res)
|
|
|
|
return dict(categories=doc_struct)
|