Skip to content

Commit 1cec0e9

Browse files
committed
Updated review comments
1 parent 559d511 commit 1cec0e9

File tree

5 files changed

+79
-86
lines changed

5 files changed

+79
-86
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ This package validates the OSW geojson file. Package requires a OSW zip file pat
2828
from python_lib_osw_validation import OSWValidation
2929

3030
validator = OSWValidation(zipfile_path='<Zip file path>')
31-
is_valid = validator.validate()
32-
if not is_valid:
33-
print(validator.error)
31+
result = validator.validate()
32+
print(result.is_valid)
33+
print(result.error)
3434

3535
```
3636

setup_env.sh

100644100755
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
#!/bin/bash
22
env_name="tdei"
33
# check if conda exists in the system
4-
conda_path=$(which conda)
4+
conda_path=$(command -v conda)
55
echo $conda_path
66
echo "Checking if conda is installed on the system ..."
7-
if ! hash which conda 2>/dev/null
8-
then
7+
if [ -z "$conda_path" ]; then
98
echo " conda not found on the system"
109
echo " !!! Please install conda on your system before proceeding ..."
11-
return 1
10+
exit 1
1211
else
13-
echo " found conda at $conda_path"
12+
echo " Found conda at $conda_path"
1413
fi
1514

1615
echo "Checking if the environment already exists ..."
17-
if conda env list | grep "$env_name" >/dev/null 2>&1
18-
then
19-
echo " !!! $env_name already in the system. Exiting..."
20-
return 1
16+
if conda env list | grep -q "$env_name"; then
17+
echo " !!! $env_name already exists in the system. Exiting..."
18+
exit 1
2119
fi
2220

23-
echo "Creating $env_name env with conda ..."
24-
y | conda create -n $env_name python==3.10.3
21+
echo "Creating $env_name environment with conda ..."
22+
yes | conda create -n $env_name python=3.10
2523

2624
echo "Activating $env_name ..."
2725
source ~/anaconda3/etc/profile.d/conda.sh
2826
conda activate $env_name
2927

3028
echo "Installing required packages using pip in $env_name ..."
3129
pip install -r requirements.txt
32-
echo " ***********************************************"
33-
echo " TDEI env set up and activation complete..."
34-
echo " ***********************************************"
30+
31+
echo "***********************************************"
32+
echo "TDEI environment setup and activation complete."
33+
echo "***********************************************"

src/example.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,26 @@
1111

1212
def valid_test_with_provided_schema():
1313
validator = OSWValidation(zipfile_path=VALID_ZIP_FILE, schema_file_path=SCHEMA_FILE_PATH)
14-
is_valid = validator.validate()
15-
print(f'Valid Test With Provided Schema: {"Passed" if is_valid else "Failed"}')
14+
validation = validator.validate()
15+
print(f'Valid Test With Provided Schema: {"Passed" if validation.is_valid else "Failed"}')
1616

1717

1818
def valid_test_without_provided_schema():
1919
validator = OSWValidation(zipfile_path=VALID_ZIP_FILE)
20-
is_valid = validator.validate()
21-
print(f'Valid Test Without Schema: {"Passed" if is_valid else "Failed"}')
20+
validation = validator.validate()
21+
print(f'Valid Test Without Schema: {"Passed" if validation.is_valid else "Failed"}')
2222

2323

2424
def invalid_test_with_provided_schema():
2525
validator = OSWValidation(zipfile_path=INVALID_ZIP_FILE, schema_file_path=SCHEMA_FILE_PATH)
26-
is_valid = validator.validate()
27-
print(f'Invalid Test With Provided Schema: {"Failed" if is_valid else "Passed"}')
26+
validation = validator.validate()
27+
print(f'Invalid Test With Provided Schema: {"Failed" if validation.is_valid else "Passed"}')
2828

2929

3030
def invalid_test_without_provided_schema():
3131
validator = OSWValidation(zipfile_path=INVALID_ZIP_FILE)
32-
is_valid = validator.validate()
33-
print(f'Invalid Test Without Schema: {"Failed" if is_valid else "Passed"}')
32+
validation = validator.validate()
33+
print(f'Invalid Test Without Schema: {"Failed" if validation.is_valid else "Passed"}')
3434

3535

3636
if __name__ == '__main__':
Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
import os
22
import json
33
import jsonschema
4-
from typing import Dict, Any
4+
from typing import Dict, Any, Optional
55
from .zipfile_handler import ZipFileHandler
66
from .extracted_data_validator import ExtractedDataValidator
77

88
SCHEMA_PATH = os.path.join(os.path.dirname(__file__), 'schema')
99

1010

11+
class ValidationResult:
12+
def __init__(self, is_valid: bool, error: Optional[str] = None):
13+
self.is_valid = is_valid
14+
self.error = error
15+
16+
1117
class OSWValidation:
12-
schema_dir = os.path.join(SCHEMA_PATH, 'opensidewalks.schema.json')
18+
default_schema_file_path = os.path.join(SCHEMA_PATH, 'opensidewalks.schema.json')
1319

1420
def __init__(self, zipfile_path: str, schema_file_path=None):
1521
self.zipfile_path = zipfile_path
1622
self.extracted_dir = None
1723
if schema_file_path is None:
18-
self.schema = self.load_osw_schema(OSWValidation.schema_dir)
24+
self.schema = self.load_osw_schema(OSWValidation.default_schema_file_path)
1925
else:
2026
self.schema = self.load_osw_schema(schema_file_path)
2127

@@ -31,43 +37,33 @@ def load_osw_schema(self, schema_path: str) -> Dict[str, Any]:
3137
self.error = e
3238
raise Exception(f'Invalid or missing schema file: {e}')
3339

40+
def validate(self) -> ValidationResult:
41+
try:
42+
# Extract the zipfile
43+
zip_handler = ZipFileHandler(self.zipfile_path)
44+
self.extracted_dir = zip_handler.extract_zip()
3445

46+
if not self.extracted_dir:
47+
self.error = zip_handler.error
48+
return ValidationResult(False, self.error)
3549

36-
def validate(self) -> bool:
37-
self.error = None
38-
39-
# Extract the zipfile
40-
zip_handler = ZipFileHandler(self.zipfile_path)
41-
self.extracted_dir = zip_handler.extract_zip()
42-
43-
if not self.extracted_dir:
44-
self.error = zip_handler.error
45-
return False
46-
47-
# Validate the folder structure
48-
validator = ExtractedDataValidator(self.extracted_dir)
49-
if not validator.is_valid():
50-
self.error = validator.error
51-
return False
50+
# Validate the folder structure
51+
validator = ExtractedDataValidator(self.extracted_dir)
52+
if not validator.is_valid():
53+
self.error = validator.error
54+
return ValidationResult(False, self.error)
5255

53-
try:
5456
for file in validator.files:
5557
file_path = os.path.join(file)
56-
filename = os.path.basename(file_path)
5758
is_valid = self.validate_osw_errors(self.load_osw_file(file_path))
58-
# print(f'{filename} validation result: {is_valid}')
59-
6059
if not is_valid:
6160
zip_handler.remove_extracted_files()
62-
return False
63-
zip_handler.remove_extracted_files()
64-
return True
61+
return ValidationResult(False, self.error)
62+
63+
return ValidationResult(True, None)
6564
except Exception as e:
66-
zip_handler.remove_extracted_files()
6765
self.error = f'Unable to validate: {e}'
68-
return False
69-
70-
66+
return ValidationResult(False, self.error)
7167

7268
def load_osw_file(self, graph_geojson_path: str) -> Dict[str, Any]:
7369
'''Load OSW Data'''
@@ -79,11 +75,9 @@ def load_osw_file(self, graph_geojson_path: str) -> Dict[str, Any]:
7975
def validate_osw_errors(self, geojson_data: Dict[str, Any]) -> bool:
8076
'''Validate OSW Data against the schema and process all errors'''
8177
validator = jsonschema.Draft7Validator(self.schema)
82-
errors = validator.iter_errors(geojson_data)
78+
errors = list(validator.iter_errors(geojson_data))
8379

84-
error_count = 0
85-
for error in errors:
86-
error_count += 1
87-
self.error = error
88-
89-
return error_count == 0
80+
if errors:
81+
self.error = errors[0]
82+
return False
83+
return True

tests/unit_tests/test_osw_validation.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ def setUp(self):
2525
def test_valid_zipfile(self):
2626
validation = OSWValidation(zipfile_path=self.valid_zipfile)
2727
result = validation.validate()
28-
self.assertTrue(result)
29-
self.assertIsNone(validation.error)
28+
self.assertTrue(result.is_valid)
29+
self.assertIsNone(result.error)
3030

3131
def test_valid_zipfile_with_schema(self):
3232
validation = OSWValidation(zipfile_path=self.valid_zipfile, schema_file_path=self.schema_file_path)
3333
result = validation.validate()
34-
self.assertTrue(result)
35-
self.assertIsNone(validation.error)
34+
self.assertTrue(result.is_valid)
35+
self.assertIsNone(result.error)
3636

3737
def test_valid_zipfile_with_invalid_schema(self):
3838
with self.assertRaises(Exception):
@@ -42,14 +42,14 @@ def test_valid_zipfile_with_invalid_schema(self):
4242
def test_invalid_zipfile(self):
4343
validation = OSWValidation(zipfile_path=self.invalid_zipfile)
4444
result = validation.validate()
45-
self.assertFalse(result)
46-
self.assertIsNotNone(validation.error)
45+
self.assertFalse(result.is_valid)
46+
self.assertIsNotNone(result.error)
4747

4848
def test_invalid_zipfile_with_schema(self):
4949
validation = OSWValidation(zipfile_path=self.invalid_zipfile, schema_file_path=self.schema_file_path)
5050
result = validation.validate()
51-
self.assertFalse(result)
52-
self.assertIsNotNone(validation.error)
51+
self.assertFalse(result.is_valid)
52+
self.assertIsNotNone(result.error)
5353

5454
def test_invalid_zipfile_with_invalid_schema(self):
5555
with self.assertRaises(Exception):
@@ -60,14 +60,14 @@ def test_invalid_zipfile_with_invalid_schema(self):
6060
def test_nodes_invalid_zipfile(self):
6161
validation = OSWValidation(zipfile_path=self.nodes_invalid_zipfile)
6262
result = validation.validate()
63-
self.assertFalse(result)
64-
self.assertIsNotNone(validation.error)
63+
self.assertFalse(result.is_valid)
64+
self.assertIsNotNone(result.error)
6565

6666
def test_nodes_invalid_zipfile_with_schema(self):
6767
validation = OSWValidation(zipfile_path=self.nodes_invalid_zipfile, schema_file_path=self.schema_file_path)
6868
result = validation.validate()
69-
self.assertFalse(result)
70-
self.assertIsNotNone(validation.error)
69+
self.assertFalse(result.is_valid)
70+
self.assertIsNotNone(result.error)
7171

7272
def test_nodes_invalid_zipfile_with_invalid_schema(self):
7373
with self.assertRaises(Exception):
@@ -78,14 +78,14 @@ def test_nodes_invalid_zipfile_with_invalid_schema(self):
7878
def test_edges_invalid_zipfile(self):
7979
validation = OSWValidation(zipfile_path=self.edges_invalid_zipfile)
8080
result = validation.validate()
81-
self.assertFalse(result)
82-
self.assertIsNotNone(validation.error)
81+
self.assertFalse(result.is_valid)
82+
self.assertIsNotNone(result.error)
8383

8484
def test_edges_invalid_zipfile_with_schema(self):
8585
validation = OSWValidation(zipfile_path=self.edges_invalid_zipfile, schema_file_path=self.schema_file_path)
8686
result = validation.validate()
87-
self.assertFalse(result)
88-
self.assertIsNotNone(validation.error)
87+
self.assertFalse(result.is_valid)
88+
self.assertIsNotNone(result.error)
8989

9090
def test_edges_invalid_zipfile_with_invalid_schema(self):
9191
with self.assertRaises(Exception):
@@ -96,14 +96,14 @@ def test_edges_invalid_zipfile_with_invalid_schema(self):
9696
def test_points_invalid_zipfile(self):
9797
validation = OSWValidation(zipfile_path=self.points_invalid_zipfile)
9898
result = validation.validate()
99-
self.assertFalse(result)
100-
self.assertIsNotNone(validation.error)
99+
self.assertFalse(result.is_valid)
100+
self.assertIsNotNone(result.error)
101101

102102
def test_points_invalid_zipfile_with_schema(self):
103103
validation = OSWValidation(zipfile_path=self.points_invalid_zipfile, schema_file_path=self.schema_file_path)
104104
result = validation.validate()
105-
self.assertFalse(result)
106-
self.assertIsNotNone(validation.error)
105+
self.assertFalse(result.is_valid)
106+
self.assertIsNotNone(result.error)
107107

108108
def test_points_invalid_zipfile_with_invalid_schema(self):
109109
with self.assertRaises(Exception):
@@ -114,14 +114,14 @@ def test_points_invalid_zipfile_with_invalid_schema(self):
114114
def test_missing_files_inside_zipfile(self):
115115
validation = OSWValidation(zipfile_path=self.missing_files_zipfile)
116116
result = validation.validate()
117-
self.assertFalse(result)
118-
self.assertIsNotNone(validation.error)
117+
self.assertFalse(result.is_valid)
118+
self.assertIsNotNone(result.error)
119119

120120
def test_missing_files_inside_zipfile_with_schema(self):
121121
validation = OSWValidation(zipfile_path=self.missing_files_zipfile, schema_file_path=self.schema_file_path)
122122
result = validation.validate()
123-
self.assertFalse(result)
124-
self.assertIsNotNone(validation.error)
123+
self.assertFalse(result.is_valid)
124+
self.assertIsNotNone(result.error)
125125

126126
def test_missing_files_inside_zipfile_with_invalid_schema(self):
127127
with self.assertRaises(Exception):

0 commit comments

Comments
 (0)