From 64ce509d438ea79c75872c7c1c1948e2cc122172 Mon Sep 17 00:00:00 2001 From: Alex Kanitz Date: Sat, 25 Jun 2022 16:54:02 +0200 Subject: [PATCH 1/4] bla --- docs/api/conf.py | 4 ++-- foca/models/config.py | 6 +----- setup.py | 21 ++++++++++----------- tests/test_foca.py | 2 -- 4 files changed, 13 insertions(+), 20 deletions(-) diff --git a/docs/api/conf.py b/docs/api/conf.py index 575933e8..9e1ba585 100644 --- a/docs/api/conf.py +++ b/docs/api/conf.py @@ -12,12 +12,12 @@ # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. # -import os +from pathlib import Path import sys from sphinx.ext import apidoc -sys.path.insert(0, os.path.abspath('../..')) +sys.path.insert(Path().parents[1].resolve()) # -- Project information ----------------------------------------------------- diff --git a/foca/models/config.py b/foca/models/config.py index f8d41ad6..59976106 100644 --- a/foca/models/config.py +++ b/foca/models/config.py @@ -6,7 +6,6 @@ import importlib import operator from pathlib import Path -import os from typing import (Any, Dict, List, Optional, Union) from pydantic import (BaseModel, Field, validator) # pylint: disable=E0611 @@ -607,10 +606,7 @@ def set_default_out_path(cls, v, *, values): # pylint: disable=E0213 """ if 'path' in values and values['path'] is not None: if not v: - return '.'.join([ - os.path.splitext(values['path'][0])[0], - "modified.yaml" - ]) + return f"{Path(values['path'][0]).stem}.modified.yaml" if not Path(v).is_absolute(): return str(Path.cwd() / v) return v diff --git a/setup.py b/setup.py index 76162a22..26c33b01 100644 --- a/setup.py +++ b/setup.py @@ -1,19 +1,18 @@ -import os +"""Package setup.""" + +from pathlib import Path from setuptools import setup, find_packages -root_dir = os.path.dirname(os.path.abspath(__file__)) +root_dir = Path(__file__).parent.resolve() -# Read long description from file -file_name = os.path.join(root_dir, "README.md") -with open(file_name, "r") as fh: - long_description = fh.read() +file_name = root_dir / "README.md" +with open(file_name, "r") as _file: + long_description = _file.read() -# Read requirements from file install_requires = [] -req = root_dir + '/requirements.txt' -if os.path.isfile(req): - with open(req) as f: - install_requires = f.read().splitlines() +req = root_dir / 'requirements.txt' +with open(req, "r") as _file: + install_requires = _file.read().splitlines() setup( name="foca", diff --git a/tests/test_foca.py b/tests/test_foca.py index 7efcb22a..c734b7a8 100644 --- a/tests/test_foca.py +++ b/tests/test_foca.py @@ -3,7 +3,6 @@ from pathlib import Path import pytest import shutil -import os from connexion import App from pydantic import ValidationError @@ -101,7 +100,6 @@ def test_foca_api(tmpdir): foca = Foca(config_file=temp_file) app = foca.create_app() assert isinstance(app, App) - os.remove(temp_file) def test_foca_db(): From f9719329fa25bf7456b37b4b32e708f0622ada8a Mon Sep 17 00:00:00 2001 From: Alex Kanitz Date: Sat, 25 Jun 2022 17:54:26 +0200 Subject: [PATCH 2/4] okay --- foca/config/config_parser.py | 6 ++-- foca/foca.py | 9 +++-- foca/models/config.py | 55 +++++++++++++++--------------- tests/api/test_register_openapi.py | 20 +++++------ tests/models/test_config.py | 6 ++-- 5 files changed, 50 insertions(+), 46 deletions(-) diff --git a/foca/config/config_parser.py b/foca/config/config_parser.py index be2ab212..7e053ea3 100644 --- a/foca/config/config_parser.py +++ b/foca/config/config_parser.py @@ -49,7 +49,7 @@ class ConfigParser(): def __init__( self, - config_file: Optional[str] = None, + config_file: Optional[Path] = None, custom_config_model: Optional[str] = None, format_logs: bool = True ) -> None: @@ -82,7 +82,7 @@ def _configure_logging(self) -> None: ) @staticmethod - def parse_yaml(conf: str) -> Dict: + def parse_yaml(conf: Path) -> Dict: """Parse YAML file. Args: @@ -109,7 +109,7 @@ def parse_yaml(conf: str) -> Dict: ) from exc @staticmethod - def merge_yaml(*args: str) -> Optional[Dict]: + def merge_yaml(*args: Path) -> Optional[Dict]: """Parse and merge a set of YAML files. Merging is done iteratively, from the first, second to the n-th diff --git a/foca/foca.py b/foca/foca.py index 484081e0..d5d9d8f2 100644 --- a/foca/foca.py +++ b/foca/foca.py @@ -1,6 +1,7 @@ """Class for setting up and initializing a FOCA-based microservice.""" import logging +from pathlib import Path from typing import Optional from connexion import App @@ -21,7 +22,7 @@ class Foca: def __init__( self, - config_file: Optional[str] = None, + config_file: Optional[Path] = None, custom_config_model: Optional[str] = None, ) -> None: """Instantiate FOCA class. @@ -58,8 +59,10 @@ def __init__( parameters, so as to make it easier for others to write/modify their app configuration. """ - self.config_file = config_file - self.custom_config_model = custom_config_model + self.config_file: Optional[Path] = Path( + config_file + ) if config_file is not None else None + self.custom_config_model: Optional[str] = custom_config_model def create_app(self) -> App: """Set up and initialize FOCA-based microservice. diff --git a/foca/models/config.py b/foca/models/config.py index 59976106..bb919ef8 100644 --- a/foca/models/config.py +++ b/foca/models/config.py @@ -531,9 +531,9 @@ class SpecConfig(FOCABaseConfig): Example: >>> SpecConfig(path="/my/path.yaml") - SpecConfig(path=['/my/path.yaml'], path_out='/my/path.modified.yaml', \ -append=None, add_operation_fields=None, add_security_fields=None, disable_auth\ -=False, connexion=None) + SpecConfig(path=[PosixPath('/my/path.yaml')], path_out=PosixPath('/my/\ +path.modified.yaml'), append=None, add_operation_fields=None, add_security_fie\ +lds=None, disable_auth=False, connexion=None) >>> SpecConfig( ... path=["/path/to/specs.yaml", "/path/to/add_specs.yaml"], @@ -562,16 +562,17 @@ class SpecConfig(FOCABaseConfig): ... }, ... disable_auth = False ... ) - SpecConfig(path=['/path/to/specs.yaml', '/path/to/add_specs.yaml'], pa\ -th_out='/path/to/specs.modified.yaml', append=[{'security': {'jwt': {'type': '\ -apiKey', 'name': 'Authorization', 'in': 'header'}}}, {'my_other_root_field': '\ -some_value'}], add_operation_fields={'x-swagger-router-controller': 'controlle\ -rs.my_specs', 'x-some-other-custom-field': 'some_value'}, add_security_fields=\ -{'x-apikeyInfoFunc': 'security.auth.validate_token', 'x-some-other-custom-fiel\ -d': 'some_value'}, disable_auth=False, connexion=None) + SpecConfig(path=[PosixPath('/path/to/specs.yaml'), PosixPath('/path/to\ +/add_specs.yaml')], path_out=PosixPath('/path/to/specs.modified.yaml'), append\ +=[{'security': {'jwt': {'type': 'apiKey', 'name': 'Authorization', 'in': 'head\ +er'}}}, {'my_other_root_field': 'some_value'}], add_operation_fields={'x-swagg\ +er-router-controller': 'controllers.my_specs', 'x-some-other-custom-field': 's\ +ome_value'}, add_security_fields={'x-apikeyInfoFunc': 'security.auth.validate_\ +token', 'x-some-other-custom-field': 'some_value'}, disable_auth=False, connex\ +ion=None) """ - path: Union[str, List[str]] - path_out: Optional[str] = None + path: Union[Path, List[Path]] + path_out: Optional[Path] = None append: Optional[List[Dict]] = None add_operation_fields: Optional[Dict] = None add_security_fields: Optional[Dict] = None @@ -584,16 +585,16 @@ def set_abs_path(cls, v): # pylint: disable=E0213 """Resolve path relative to caller's current working directory if no absolute path provided. """ - # if path is a str, convert it to list - if(isinstance(v, str)): - if not Path(v).is_absolute(): - return [str(Path.cwd() / v)] + # if path is not a list, convert it to single-item list + if(isinstance(v, Path)): + if not v.is_absolute(): + return [Path.cwd() / v] return [v] else: - # modify each relaive part of the list + # make each path absolute v = [ - str(Path.cwd() / path) - if not Path(path).is_absolute() + Path.cwd() / path + if not path.is_absolute() else path for path in v ] @@ -602,13 +603,13 @@ def set_abs_path(cls, v): # pylint: disable=E0213 # set default if no output file path provided @validator('path_out', always=True, allow_reuse=True) def set_default_out_path(cls, v, *, values): # pylint: disable=E0213 - """Set default output path for spec file if not supplied by user. - """ + """Set default output path for spec file if not supplied by user.""" if 'path' in values and values['path'] is not None: if not v: - return f"{Path(values['path'][0]).stem}.modified.yaml" - if not Path(v).is_absolute(): - return str(Path.cwd() / v) + path = values['path'][0] + return path.parent / f"{path.stem}.modified.yaml" + if not v.is_absolute(): + return Path.cwd() / v return v @@ -632,9 +633,9 @@ class APIConfig(FOCABaseConfig): >>> APIConfig( ... specs=[SpecConfig(path='/path/to/specs.yaml')], ... ) - APIConfig(specs=[SpecConfig(path='/path/to/specs.yaml', path_out='/pat\ -h/to/specs.modified.yaml', append=None, add_operation_fields=None, connexion=N\ -one)]) + APIConfig(specs=[SpecConfig(path=[PosixPath('/path/to/specs.yaml')], p\ +ath_out=PosixPath('/path/to/specs.modified.yaml'), append=None, add_operation_\ +fields=None, add_security_fields=None, disable_auth=False, connexion=None)]) """ specs: List[SpecConfig] = [] diff --git a/tests/api/test_register_openapi.py b/tests/api/test_register_openapi.py index 4770a8de..71f665e3 100644 --- a/tests/api/test_register_openapi.py +++ b/tests/api/test_register_openapi.py @@ -14,16 +14,16 @@ from foca.models.config import SpecConfig # Define mock data -DIR = Path(__file__).parent.parent / "test_files" -PATH_SPECS_2_YAML_ORIGINAL = str(DIR / "openapi_2_petstore.original.yaml") -PATH_SPECS_2_YAML_MODIFIED = str(DIR / "openapi_2_petstore.modified.yaml") -PATH_SPECS_2_JSON_ORIGINAL = str(DIR / "openapi_2_petstore.original.json") -PATH_SPECS_2_YAML_ADDITION = str(DIR / "openapi_2_petstore.addition.yaml") -PATH_SPECS_3_YAML_ORIGINAL = str(DIR / "openapi_3_petstore.original.yaml") -PATH_SPECS_3_YAML_MODIFIED = str(DIR / "openapi_3_petstore.modified.yaml") -PATH_SPECS_INVALID_JSON = str(DIR / "invalid.json") -PATH_SPECS_INVALID_YAML = str(DIR / "invalid.openapi.yaml") -PATH_NOT_FOUND = str(DIR / "does/not/exist.yaml") +DIR = Path(__file__).parents[1].resolve() / "test_files" +PATH_SPECS_2_YAML_ORIGINAL = DIR / "openapi_2_petstore.original.yaml" +PATH_SPECS_2_YAML_MODIFIED = DIR / "openapi_2_petstore.modified.yaml" +PATH_SPECS_2_JSON_ORIGINAL = DIR / "openapi_2_petstore.original.json" +PATH_SPECS_2_YAML_ADDITION = DIR / "openapi_2_petstore.addition.yaml" +PATH_SPECS_3_YAML_ORIGINAL = DIR / "openapi_3_petstore.original.yaml" +PATH_SPECS_3_YAML_MODIFIED = DIR / "openapi_3_petstore.modified.yaml" +PATH_SPECS_INVALID_JSON = DIR / "invalid.json" +PATH_SPECS_INVALID_YAML = DIR / "invalid.openapi.yaml" +PATH_NOT_FOUND = DIR / "does/not/exist.yaml" OPERATION_FIELDS_2 = {"x-swagger-router-controller": "controllers"} OPERATION_FIELDS_2_NO_RESOLVE = {"x-swagger-router-controller": YAMLError} OPERATION_FIELDS_3 = {"x-openapi-router-controller": "controllers"} diff --git a/tests/models/test_config.py b/tests/models/test_config.py index 8e61f0e7..b2ef62d7 100644 --- a/tests/models/test_config.py +++ b/tests/models/test_config.py @@ -366,19 +366,19 @@ def test_spec_config_list_no_out(): def test_SpecConfig_full(): """Test SpecConfig instantiation; full example""" res = SpecConfig(**SPEC_CONFIG) - assert res.path_out == SPEC_CONFIG['path_out'] + assert str(res.path_out) == SPEC_CONFIG['path_out'] def test_SpecConfig_minimal(): """Test SpecConfig instantiation; minimal example""" res = SpecConfig(path=PATH) - assert res.path_out == PATH_MODIFIED + assert str(res.path_out) == PATH_MODIFIED def test_SpecConfig_merge(): """Test SpecConfig instantiation; multiple config files""" res = SpecConfig(path=[PATH, PATH_ADDITION]) - assert res.path_out == PATH_MODIFIED + assert str(res.path_out) == PATH_MODIFIED def test_SpecConfig_extra_arg(): From bb5bc151fea0264bf547ccb852d722415b17079b Mon Sep 17 00:00:00 2001 From: Alex Kanitz Date: Sat, 25 Jun 2022 18:01:50 +0200 Subject: [PATCH 3/4] fix & bump version --- docs/api/conf.py | 2 +- foca/__init__.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/api/conf.py b/docs/api/conf.py index 9e1ba585..69e8b784 100644 --- a/docs/api/conf.py +++ b/docs/api/conf.py @@ -17,7 +17,7 @@ from sphinx.ext import apidoc -sys.path.insert(Path().parents[1].resolve()) +sys.path.insert(Path.cwd().resolve().parents[1]) # -- Project information ----------------------------------------------------- diff --git a/foca/__init__.py b/foca/__init__.py index be8500a8..3df5f612 100644 --- a/foca/__init__.py +++ b/foca/__init__.py @@ -2,4 +2,4 @@ from foca.foca import Foca # noqa: F401 -__version__ = '0.8.0' +__version__ = '0.9.0' From c6108e22cbb3b3b3aa7e393d984ef688fe23f308 Mon Sep 17 00:00:00 2001 From: Alex Kanitz Date: Sat, 25 Jun 2022 18:11:12 +0200 Subject: [PATCH 4/4] fix --- docs/api/conf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api/conf.py b/docs/api/conf.py index 69e8b784..57786ebf 100644 --- a/docs/api/conf.py +++ b/docs/api/conf.py @@ -17,7 +17,7 @@ from sphinx.ext import apidoc -sys.path.insert(Path.cwd().resolve().parents[1]) +sys.path.insert(0, Path.cwd().resolve().parents[1]) # -- Project information -----------------------------------------------------