208 lines
7.1 KiB
Python
208 lines
7.1 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 warnings
|
|
|
|
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 _sort_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['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
|
|
|
|
@property
|
|
def service_categories(self):
|
|
"""List services categories
|
|
"""
|
|
res = []
|
|
for cat in self._service_data["service_categories"]:
|
|
res.append(copy.deepcopy(cat))
|
|
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 services_with_docs_by_category(self, category, environment=None):
|
|
"""Retrieve service category docs data
|
|
|
|
:param str category: Optional Category filter
|
|
:param str env: Optional service environment. Influeces "repository"
|
|
field
|
|
"""
|
|
res = dict()
|
|
services = self.service_dict
|
|
for doc in self.all_docs:
|
|
cat = doc["service_type"]
|
|
service = services.get(cat)
|
|
if not service:
|
|
warnings.warn("No Service defition of type %s" % (cat))
|
|
continue
|
|
if category and service["service_category"] != category:
|
|
continue
|
|
res.setdefault(cat, service)
|
|
res_doc = copy.deepcopy(doc)
|
|
res_doc.update(**service)
|
|
if environment:
|
|
if "environment" in doc and doc["environment"] != environment:
|
|
continue
|
|
res[cat].setdefault("docs", [])
|
|
res[cat]["docs"].append(res_doc)
|
|
return res
|
|
|
|
def docs_by_service_category(self, category, environment=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:
|
|
cat = doc["service_type"]
|
|
service = services.get(cat)
|
|
if not service:
|
|
warnings.warn("No Service defition of type %s" % (cat))
|
|
continue
|
|
if service["service_category"] == category:
|
|
res_doc = copy.deepcopy(doc)
|
|
res_doc.update(**service)
|
|
if environment:
|
|
for srv_env in service["repositories"]:
|
|
if srv_env.get("environment") == environment:
|
|
res_doc["repository"] = srv_env["repo"]
|
|
res.append(res_doc)
|
|
return res
|
|
|
|
def docs_by_service_type(self, service_type):
|
|
"""List documents of the service
|
|
|
|
:param str service_type: Service type
|
|
:returns: generator for documents
|
|
"""
|
|
for doc in self.all_docs:
|
|
if doc["service_type"] != service_type:
|
|
continue
|
|
yield copy.deepcopy(doc)
|
|
|
|
def all_docs_full(self, environment):
|
|
"""Return list or documents with full service data
|
|
"""
|
|
services = self.service_dict
|
|
for doc in self.all_docs:
|
|
if not doc["service_type"] in services:
|
|
print(f"No service type {doc['service_type']}")
|
|
continue
|
|
service = services[doc["service_type"]]
|
|
res_doc = copy.deepcopy(doc)
|
|
res_doc.update(**service)
|
|
if environment:
|
|
for srv_env in service["repositories"]:
|
|
if srv_env.get("environment") == environment:
|
|
res_doc["repository"] = srv_env["repo"]
|
|
for srv_assignees in service.get("assignees", []):
|
|
if srv_assignees.get("environment") == environment:
|
|
res_doc["assignees"] = srv_assignees["names"]
|
|
yield res_doc
|
|
|
|
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)
|