Skip to content

Commit

Permalink
REST-API: fix Python syntax error in code examples when using booleans
Browse files Browse the repository at this point in the history
Change-Id: Ia14004c30f0d8712af8830921144e676ad008fe6
  • Loading branch information
Christoph Rauch committed Aug 3, 2020
1 parent 34abc24 commit a583057
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 189 deletions.
1 change: 1 addition & 0 deletions Pipfile
Expand Up @@ -130,6 +130,7 @@ dnspython = "==1.16.0" # needed by python-active-directory
python-active-directory = "==1.0.5"
rrdtool = "==0.1.15" # Actually we ship the binding which comes with rrdtool itself...
docstring-parser = "==0.7.2" # direct dependency
yapf = "==0.30.0" # formatter for REST-API documentation code examples

[requires]
python_version = "3.8"
94 changes: 51 additions & 43 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 15 additions & 2 deletions cmk/gui/plugins/openapi/restful_objects/code_examples.py
Expand Up @@ -15,6 +15,7 @@
from typing import Any, Dict, List, NamedTuple, Sequence

import jinja2
import yapf.yapflib.yapf_api # type: ignore[import]
from apispec.ext.marshmallow import resolve_schema_instance # type: ignore[import]
from marshmallow import Schema # type: ignore[import]

Expand Down Expand Up @@ -99,7 +100,7 @@
data=json.dumps({{
request_schema |
to_dict |
to_json(indent=4, sort_keys=True) |
to_python |
indent(skip_lines=1, spaces=4) }}).encode('utf-8'),
{%- endif %}
)
Expand Down Expand Up @@ -198,7 +199,7 @@
json={{
request_schema |
to_dict |
to_json(indent=4, sort_keys=True) |
to_python |
indent(skip_lines=1, spaces=4) }},
{%- endif %}
)
Expand Down Expand Up @@ -381,6 +382,17 @@ def _filter_params(
return query_parameters


def yapf_format(obj):
style = {
'COLUMN_LIMIT': 50,
'ALLOW_SPLIT_BEFORE_DICT_VALUE': False,
'COALESCE_BRACKETS': True,
'DEDENT_CLOSING_BRACKETS': True,
}
text, _ = yapf.yapflib.yapf_api.FormatCode(str(obj), style_config=style)
return text


def _jinja_environment() -> jinja2.Environment:
"""Create a map with code templates, ready to render.
Expand Down Expand Up @@ -421,6 +433,7 @@ def _jinja_environment() -> jinja2.Environment:
to_dict=to_dict,
to_env=_to_env,
to_json=json.dumps,
to_python=yapf_format,
)
# These objects will be available in the templates
tmpl_env.globals.update(
Expand Down
2 changes: 1 addition & 1 deletion omd/packages/python3-modules/python3-modules.make
Expand Up @@ -124,6 +124,7 @@ PYTHON3_MODULES_LIST += typing_extensions-3.7.4.1.tar.gz # direct dependency
PYTHON3_MODULES_LIST += dnspython-1.16.0.zip # needed by python-active-directory
PYTHON3_MODULES_LIST += python-active-directory-1.0.5.tar.gz # direct dependency
PYTHON3_MODULES_LIST += docstring_parser-0.7.2.tar.gz # direct dependency
PYTHON3_MODULES_LIST += yapf-0.30.0.tar.gz # formatter for REST-API documentation code examples

# TODO: Can we clean this up and use the intermediate install step results? Would be possible
# in the moment we merge the build and intermediate install in a single target
Expand Down Expand Up @@ -219,7 +220,6 @@ python3-modules-dump-Pipfile:
@echo 'pytest-cov = "*" # used (indirectly) by test/Makefile'"'"'s test-unit-coverage-html target, see comment there'
@echo 'pytest-mock = "*" # used by quite a few unit/integration tests via the mocker fixture'
@echo 'responses = "*" # used for unit tests'
@echo 'yapf = "*" # used for editor integration and the format-python Makefile target'
@echo 'polib = "*" # used by locale/add-authors for working with .po files'
@echo 'webtest = "*" # used by WSGI based tests'
@echo 'pre-commit = "*" # used to fix / find issues before commiting changes'
Expand Down
Binary file not shown.

0 comments on commit a583057

Please sign in to comment.