python-osw-validation 0.0.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,80 @@
1
+ import os
2
+ import json
3
+ import jsonschema
4
+ from typing import Dict, Any, Optional, List
5
+ from .zipfile_handler import ZipFileHandler
6
+ from .extracted_data_validator import ExtractedDataValidator
7
+
8
+ SCHEMA_PATH = os.path.join(os.path.dirname(__file__), 'schema')
9
+
10
+
11
+ class ValidationResult:
12
+ def __init__(self, is_valid: bool, errors: Optional[List[str]] = None):
13
+ self.is_valid = is_valid
14
+ self.errors = errors
15
+
16
+
17
+ class OSWValidation:
18
+ default_schema_file_path = os.path.join(SCHEMA_PATH, 'opensidewalks.schema.json')
19
+
20
+ def __init__(self, zipfile_path: str, schema_file_path=None):
21
+ self.zipfile_path = zipfile_path
22
+ self.extracted_dir = None
23
+ self.errors = []
24
+ if schema_file_path is None:
25
+ self.schema_file_path = OSWValidation.default_schema_file_path
26
+ else:
27
+ self.schema_file_path = schema_file_path
28
+
29
+ def load_osw_schema(self, schema_path: str) -> Dict[str, Any]:
30
+ '''Load OSW Schema'''
31
+ try:
32
+ with open(schema_path, 'r') as file:
33
+ return json.load(file)
34
+ except Exception as e:
35
+ self.errors.append(f'Invalid or missing schema file: {e}')
36
+ raise Exception(f'Invalid or missing schema file: {e}')
37
+
38
+ def validate(self) -> ValidationResult:
39
+ try:
40
+ # Extract the zipfile
41
+ zip_handler = ZipFileHandler(self.zipfile_path)
42
+ self.extracted_dir = zip_handler.extract_zip()
43
+
44
+ if not self.extracted_dir:
45
+ self.errors.append(zip_handler.error)
46
+ return ValidationResult(False, self.errors)
47
+
48
+ # Validate the folder structure
49
+ validator = ExtractedDataValidator(self.extracted_dir)
50
+ if not validator.is_valid():
51
+ self.errors.append(validator.error)
52
+ return ValidationResult(False, self.errors)
53
+
54
+ for file in validator.files:
55
+ file_path = os.path.join(file)
56
+ is_valid = self.validate_osw_errors(self.load_osw_file(file_path))
57
+ if not is_valid:
58
+ zip_handler.remove_extracted_files()
59
+ return ValidationResult(False, self.errors)
60
+
61
+ return ValidationResult(True)
62
+ except Exception as e:
63
+ self.errors.append(f'Unable to validate: {e}')
64
+ return ValidationResult(False, self.errors)
65
+
66
+ def load_osw_file(self, graph_geojson_path: str) -> Dict[str, Any]:
67
+ '''Load OSW Data'''
68
+ with open(graph_geojson_path, 'r') as file:
69
+ return json.load(file)
70
+
71
+ def validate_osw_errors(self, geojson_data: Dict[str, Any]) -> bool:
72
+ '''Validate OSW Data against the schema and process all errors'''
73
+ validator = jsonschema.Draft7Validator(self.load_osw_schema(self.schema_file_path))
74
+ errors = list(validator.iter_errors(geojson_data))
75
+
76
+ if errors:
77
+ for error in errors:
78
+ self.errors.append(f'Validation error: {error.message}')
79
+ return False
80
+ return True
@@ -0,0 +1,34 @@
1
+ import os
2
+ import glob
3
+
4
+
5
+ class ExtractedDataValidator:
6
+ def __init__(self, extracted_dir: str):
7
+ self.extracted_dir = extracted_dir
8
+ self.files = []
9
+ self.error = None
10
+
11
+ def is_valid(self) -> bool:
12
+ if not os.path.exists(self.extracted_dir) or not os.listdir(self.extracted_dir):
13
+ self.error = 'Folder is empty or does not exist.'
14
+ return False
15
+
16
+ geojson_files = glob.glob(os.path.join(self.extracted_dir, '*.geojson'))
17
+ if len(geojson_files) < 3:
18
+ self.error = 'There are not enough .geojson files in the folder.'
19
+ return False
20
+
21
+ for filename in geojson_files:
22
+ base_name = os.path.basename(filename)
23
+ if 'edges' in base_name and base_name.endswith('.geojson'):
24
+ self.files.append(filename)
25
+ elif 'nodes' in base_name and base_name.endswith('.geojson'):
26
+ self.files.append(filename)
27
+ elif 'points' in base_name and base_name.endswith('.geojson'):
28
+ self.files.append(filename)
29
+
30
+ if len(self.files) != 3:
31
+ self.error = 'Missing one or more required .geojson files.'
32
+ return False
33
+
34
+ return True