From 43868ffea08cb1d0ab1425a6ec37a4930a92af6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Gschwind?= Date: Fri, 15 Sep 2023 17:05:32 +0200 Subject: [PATCH 1/4] Update validategeojson that used deprecated code --- pywps/validator/complexvalidator.py | 31 +++++++++-------------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/pywps/validator/complexvalidator.py b/pywps/validator/complexvalidator.py index 0325b82d8..b24727153 100644 --- a/pywps/validator/complexvalidator.py +++ b/pywps/validator/complexvalidator.py @@ -260,35 +260,22 @@ def validategeojson(data_input, mode): import json import jsonschema + import referencing # this code comes from # https://github.com/om-henners/GeoJSON_Validation/blob/master/geojsonvalidation/geojson_validation.py + schema_uri = "http://json-schema.org/geojson" schema_home = os.path.join(_get_schemas_home(), "geojson") - base_schema = os.path.join(schema_home, "geojson.json") - with open(base_schema) as fh: - geojson_base = json.load(fh) + registry = referencing.Registry() - with open(os.path.join(schema_home, "crs.json")) as fh: - crs_json = json.load(fh) + for fn in ["geojson.json", "crs.json", "bbox.json", "geometry.json"]: + with open(os.path.join(schema_home, fn)) as fh: + schema_data = json.load(fh) + registry = registry.with_resource(f"{schema_uri}/{fn}", + referencing.Resource(schema_data, referencing.jsonschema.DRAFT4)) - with open(os.path.join(schema_home, "bbox.json")) as fh: - bbox_json = json.load(fh) - - with open(os.path.join(schema_home, "geometry.json")) as fh: - geometry_json = json.load(fh) - - cached_json = { - "http://json-schema.org/geojson/crs.json": crs_json, - "http://json-schema.org/geojson/bbox.json": bbox_json, - "http://json-schema.org/geojson/geometry.json": geometry_json - } - - resolver = jsonschema.RefResolver( - "http://json-schema.org/geojson/geojson.json", - geojson_base, store=cached_json) - - validator = jsonschema.Draft4Validator(geojson_base, resolver=resolver) + validator = jsonschema.Draft4Validator({"$ref": f"{schema_uri}/geojson.json"}, registry=registry) try: validator.validate(json.loads(data_input.stream.read())) passed = True From f0a5174ec52d3fa3caac2572fd257e739fe5f627 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Gschwind?= Date: Mon, 18 Sep 2023 10:15:07 +0200 Subject: [PATCH 2/4] Track database configuration change The database may change, in particular during tests, thus track change and update database connection accordingly. --- pywps/dblog.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pywps/dblog.py b/pywps/dblog.py index 934e45bc3..2882ea9f2 100644 --- a/pywps/dblog.py +++ b/pywps/dblog.py @@ -29,6 +29,7 @@ from pywps.response.status import WPS_STATUS LOGGER = logging.getLogger('PYWPS') +_SESSION_MAKER_DATABASE = None _SESSION_MAKER = None _tableprefix = configuration.get_config_value('logging', 'prefix') @@ -200,13 +201,19 @@ def get_session(): """Get Connection for database """ LOGGER.debug('Initializing database connection') - global _SESSION_MAKER + global _SESSION_MAKER, _SESSION_MAKER_DATABASE + + database = configuration.get_config_value('logging', 'database') + + # Check if database has changed, and clear session maker if needed + if _SESSION_MAKER_DATABASE != database: + _SESSION_MAKER_DATABASE = None + _SESSION_MAKER = None if _SESSION_MAKER: return _SESSION_MAKER() with lock: - database = configuration.get_config_value('logging', 'database') echo = True level = configuration.get_config_value('logging', 'level') level_name = logging.getLevelName(level) @@ -232,6 +239,7 @@ def get_session(): ProcessInstance.metadata.create_all(engine) RequestInstance.metadata.create_all(engine) + _SESSION_MAKER_DATABASE = database _SESSION_MAKER = Session return _SESSION_MAKER() From 789025a82a17f89fcba90375742849a223a74343 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Gschwind?= Date: Mon, 18 Sep 2023 10:08:12 +0200 Subject: [PATCH 3/4] Create temporary directory for storing test_assync database --- tests/test_assync.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/test_assync.py b/tests/test_assync.py index f64a53b79..d3bd87d2e 100644 --- a/tests/test_assync.py +++ b/tests/test_assync.py @@ -12,6 +12,8 @@ from .processes import Sleep from owslib.wps import WPSExecution from pathlib import Path +from tempfile import TemporaryDirectory +from pywps import dblog VERSION = "1.0.0" @@ -20,12 +22,19 @@ class ExecuteTest(unittest.TestCase): def setUp(self) -> None: + + # Create temporary directory to create test database. + self.tmpdir = TemporaryDirectory() + # Running processes using the MultiProcessing scheduler and a file-based database configuration.CONFIG.set('processing', 'mode', 'distributed') - configuration.CONFIG.set("logging", "database", "sqlite:////tmp/test-pywps-logs.sqlite3") + + configuration.CONFIG.set("logging", "database", f"sqlite:///{self.tmpdir.name}/test-pywps-logs.sqlite3") def tearDown(self) -> None: + # Cleanup temporary database configuration.load_configuration() + self.tmpdir.cleanup() def test_async(self): client = client_for(Service(processes=[Sleep()])) From 371474fe84dcd5ed71a0ac83b3dbb96a74c44031 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Gschwind?= Date: Mon, 18 Sep 2023 10:34:42 +0200 Subject: [PATCH 4/4] Fix flake8 E721 --- pywps/ext_autodoc.py | 2 +- pywps/inout/literaltypes.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pywps/ext_autodoc.py b/pywps/ext_autodoc.py index 82ee1f888..2751b6b98 100644 --- a/pywps/ext_autodoc.py +++ b/pywps/ext_autodoc.py @@ -121,7 +121,7 @@ class instance. for m in obj.metadata: if isinstance(m, Metadata): title, href = m.title, m.href - elif type(m) == dict: + elif isinstance(m, dict): title, href = m['title'], m['href'] else: title, href = None, None diff --git a/pywps/inout/literaltypes.py b/pywps/inout/literaltypes.py index f7e6ec485..6fc45778d 100644 --- a/pywps/inout/literaltypes.py +++ b/pywps/inout/literaltypes.py @@ -388,7 +388,7 @@ def make_allowedvalues(allowed_values): # value is an instance of one of the allowed classes new_allowedvalues.append(value) - elif type(value) == tuple or type(value) == list: + elif isinstance(value, (tuple, list)): spacing = None if len(value) == 2: minval = value[0]