Skip to content

Commit e3f8ea8

Browse files
committed
Extracts method
1 parent 8af4d21 commit e3f8ea8

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

gmnspy/schema.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,27 @@
3737
}
3838

3939

40+
def read_schema_for_resource(resource_df: pd.DataFrame, table_name: str, raise_error: bool) -> dict:
41+
"""
42+
Read in schema from schema json file and returns as dictionary.
43+
44+
##TODO validate schema itself
45+
46+
Args:
47+
schema_file: File location of the schema json file.
48+
49+
Returns: The schema as a dictionary
50+
"""
51+
matching_schema_paths = resource_df.loc[resource_df["name"] == table_name, "fullpath_schema"]
52+
if matching_schema_paths.empty:
53+
msg = f"FAIL. Could not find schema path for table {table_name}"
54+
logger.error(msg)
55+
if raise_error:
56+
raise Exception(msg)
57+
return dict()
58+
return read_schema(schema_file=matching_schema_paths.iloc[0])
59+
60+
4061
def read_schema(schema_file: str) -> dict:
4162
"""
4263
Read in schema from schema json file and returns as dictionary.

gmnspy/validation/foreign_keys.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import pandas as pd
66

7-
from gmnspy.schema import read_schema
7+
from gmnspy.schema import read_schema_for_resource
88
from gmnspy.utils import logger
99

1010

@@ -28,14 +28,9 @@ def validate_foreign_keys(gmns_net_d: Dict[str, pd.DataFrame], resource_df: pd.D
2828

2929
fkey_errors = []
3030
for table_name, df in gmns_net_d.items():
31-
matching_schema_paths = resource_df.loc[resource_df["name"] == table_name, "fullpath_schema"]
32-
if matching_schema_paths.empty:
33-
msg = f"FAIL. Could not find schema path for table {table_name}"
34-
logger.error(msg)
35-
if raise_error:
36-
raise Exception(msg)
31+
schema = read_schema_for_resource(resource_df, table_name, raise_error)
32+
if len(schema) == 0:
3733
continue
38-
schema = read_schema(schema_file=matching_schema_paths.iloc[0])
3934

4035
foreign_keys = [
4136
(f["name"], f["foreign_key"])

0 commit comments

Comments
 (0)