feat: Add make genconfig to generate sample config

1. add a cmd script to generate sample config
2. set setup as False for generate_swagger, so we can make swagger
without skyline-apiserver yaml config
3. add make genconfig into Makefile
4. add config-sample-generator scripts into pyproject.toml
5. replace async with sync for generate_swagger script
6. add items, keys, values function for Group and Configuration

Change-Id: I4c7196f1d766a6abdc24c71c4b3135cd40cc26ef
This commit is contained in:
zhu.boxiang 2021-06-30 16:05:54 +08:00
parent c1bb509391
commit a65e543bdf
5 changed files with 82 additions and 19 deletions

View File

@ -39,6 +39,7 @@ help:
@echo " db_revision Generate database alembic version revision with model." @echo " db_revision Generate database alembic version revision with model."
@echo " db_sync Sync database from alembic version revision." @echo " db_sync Sync database from alembic version revision."
@echo " swagger Generate swagger json file." @echo " swagger Generate swagger json file."
@echo " genconfig Generate sample config file."
@echo " future_check Find python files without 'type annotations'.(Alpha)" @echo " future_check Find python files without 'type annotations'.(Alpha)"
@echo @echo
@ -131,6 +132,11 @@ swagger:
poetry run swagger-generator -o $(ROOT_DIR)/docs/api/swagger.json poetry run swagger-generator -o $(ROOT_DIR)/docs/api/swagger.json
.PHONY: genconfig
genconfig:
poetry run config-sample-generator -o $(ROOT_DIR)/etc/skyline-apiserver.yaml.sample
# Find python files without "type annotations" # Find python files without "type annotations"
future_check: future_check:
@find src ! -size 0 -type f -name *.py -exec grep -L 'from __future__ import annotations' {} \; @find src ! -size 0 -type f -name *.py -exec grep -L 'from __future__ import annotations' {} \;

View File

@ -59,6 +59,7 @@ skyline-log = {path = "./libs/skyline-log", develop = true}
[tool.poetry.scripts] [tool.poetry.scripts]
swagger-generator = 'skyline_apiserver.cmd.generate_swagger:main' swagger-generator = 'skyline_apiserver.cmd.generate_swagger:main'
config-sample-generator = 'skyline_apiserver.cmd.generate_sample_config:main'
[tool.black] [tool.black]
line-length = 98 line-length = 98

View File

@ -0,0 +1,52 @@
# Copyright 2021 99cloud
#
# 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.
from __future__ import annotations
import sys
import click
import yaml
from skyline_apiserver.config import CONF, configure
@click.command(help="Generate skyline-apiserver sample config file.")
@click.option(
"-o",
"--output-file",
"output_file_path",
default="skyline-apiserver.yaml.sample",
help=(
"The path of the output file, this file is used to generate a sample config file "
"for use. (Default value: skyline-apiserver.yaml.sample)"
),
)
def main(output_file_path: str) -> None:
try:
configure("skyline-apiserver", setup=False)
result = {}
for group_name, group in CONF.items():
result[group_name] = {i.name: i.default for i in group.values()}
with open(output_file_path, mode="w") as f:
f.write(yaml.safe_dump(result, allow_unicode=True))
except Exception as e:
print(f"Generate skyline-apiserver sample config file failed: {str(e)}")
sys.exit(1)
if __name__ == "__main__":
main()

View File

@ -16,19 +16,10 @@ from __future__ import annotations
import json import json
import sys import sys
from logging import StreamHandler
import aiofiles
import click import click
from skyline_log import LOG, setup as log_setup
from skyline_apiserver.config import configure
from skyline_apiserver.main import app from skyline_apiserver.main import app
from skyline_apiserver.utils.coroutine import sync_run
class CommandException(Exception):
EXIT_CODE = 1
@click.command(help="Generate swagger file.") @click.command(help="Generate swagger file.")
@ -42,19 +33,15 @@ class CommandException(Exception):
"use in the development process. (Default value: swagger.json)" "use in the development process. (Default value: swagger.json)"
), ),
) )
@sync_run def main(output_file_path: str) -> None:
async def main(output_file_path: str) -> None:
try: try:
configure("skyline-apiserver")
log_setup(StreamHandler())
swagger_dict = app.openapi() swagger_dict = app.openapi()
async with aiofiles.open(output_file_path, mode="w") as f: with open(output_file_path, mode="w") as f:
await f.write(json.dumps(swagger_dict, indent=4)) f.write(json.dumps(swagger_dict, indent=4))
except CommandException as e: except Exception as e:
LOG.error(e) print(f"Generate swagger file failed: {str(e)}")
sys.exit(e.EXIT_CODE) sys.exit(1)
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -83,6 +83,15 @@ class Group:
items = ", ".join((f"{opt}=Opt(name='{opt}')" for opt in self._opts)) items = ", ".join((f"{opt}=Opt(name='{opt}')" for opt in self._opts))
return f"Group({items})" return f"Group({items})"
def items(self) -> Iterator[Any]:
return self._opts.items()
def keys(self) -> Iterator[Any]:
return self._opts.keys()
def values(self) -> Iterator[Any]:
return self._opts.values()
@dataclass(repr=False, frozen=True) @dataclass(repr=False, frozen=True)
class Configuration: class Configuration:
@ -148,5 +157,13 @@ class Configuration:
items = ", ".join((f"{group}=Group(name='{group}')" for group in self._groups)) items = ", ".join((f"{group}=Group(name='{group}')" for group in self._groups))
return f"Configuration({items})" return f"Configuration({items})"
def items(self) -> Iterator[Any]:
return self._groups.items()
def keys(self) -> Iterator[Any]:
return self._groups.keys()
def values(self) -> Iterator[Any]:
return self._groups.values()
__all__ = ("Opt", "Group", "Configuration") __all__ = ("Opt", "Group", "Configuration")