-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcdif_config.py
More file actions
134 lines (107 loc) · 4.8 KB
/
Copy pathcdif_config.py
File metadata and controls
134 lines (107 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#! /bin/env python3
"""
CDIF Configuration Loader
- Maps feature types to categories
- Provides mandatory attributes per feature type
Copyright (c) 2025-2026 IQGeo Group Plc. Use subject to conditions at license.txt
"""
import os
from typing import Dict, List, Optional
import yaml
class CDIFConfig:
"""
Minimal CDIF configuration loader.
Replaces cdif_mandatory_attributes.py with YAML-driven approach.
"""
def __init__(
self, user_model_path: str | None, mandatory_spec_path: str | None = None
):
"""
Load configuration from YAML files
Args:
user_model_path: Path to user's data model YAML (feature types & categories, defaults
to data_model.yaml in the same dir)
mandatory_spec_path: Path to mandatory attributes spec (defaults to
cdif_mandatory_spec.yaml in same dir)
"""
# Lookup structures
self.feature_to_category: Dict[str, str] = {}
self.category_mandatory: Dict[str, List[str]] = {}
self.category_to_features: Dict[str, List[str]] = {}
this_dir = os.path.dirname(os.path.abspath(__file__))
if user_model_path is None:
# default to data_model.yaml in the same dir as this file.
user_model_path = os.path.join(this_dir, "data_model.yaml")
# Load user's data model
with open(user_model_path, "r", encoding="utf-8") as f:
self.user_model = yaml.safe_load(f)
# Load mandatory attributes spec (internal file)
# ENH : add data directory support when tool is packaged
if mandatory_spec_path is None:
# Default to cdif_mandatory_attributes.yaml in the same directory as this file
mandatory_spec_path = os.path.join(this_dir, "cdif_mandatory_attributes.yaml")
with open(mandatory_spec_path, "r", encoding="utf-8") as f:
self.mandatory_spec = yaml.safe_load(f)
# Build lookup structures
self._build_lookups()
def _build_lookups(self):
"""Build efficient lookup structures from config"""
# Load mandatory attributes from spec file
category_attrs = self.mandatory_spec.get("category_mandatory_attributes", {})
for category, attrs in category_attrs.items():
self.category_mandatory[category] = attrs
# Build feature type mappings from user model
feature_types = self.user_model.get("feature_types", {})
for category, feature_list in feature_types.items():
if not isinstance(feature_list, list):
continue
for feature_name in feature_list:
self.feature_to_category[feature_name] = category
# Build category to features mapping
for category, feature_list in feature_types.items():
if isinstance(feature_list, list):
self.category_to_features[category] = feature_list
for feature_name in feature_list:
self.feature_to_category[feature_name] = category
def get_category(self, feature_name: str) -> Optional[str]:
"""Get category for a feature type."""
return self.feature_to_category.get(feature_name)
def get_features_by_category(self, category: str) -> List[str]:
"""Get all feature type names belonging to a category."""
return self.category_to_features.get(category, [])
def get_mandatory_attributes(self, feature_name: str) -> List[str]:
"""
Get mandatory attributes for a feature type.
"""
# Category defaults
category = self.get_category(feature_name)
if category and category in self.category_mandatory:
return self.category_mandatory[category].copy()
# Unknown feature type - return empty list
return []
def get_mandatory_attributes_dict(
self, files_by_name: Dict[str, str]
) -> Dict[str, List[str]]:
"""
Build mandatory attributes dictionary for all loaded files.
"""
mandatory = {}
for feature_name in files_by_name.keys():
attrs = self.get_mandatory_attributes(feature_name)
if attrs:
mandatory[feature_name] = attrs
return mandatory
# Example usage
if __name__ == "__main__":
# Load config with both files
config = CDIFConfig("data_model.yaml", "cdif_mandatory_spec.yaml")
print("Loaded data model configuration")
print(f"Categories: {list(config.category_to_features.keys())}")
# Example lookups
if config.feature_to_category:
example_feature = list(config.feature_to_category.keys())[0]
print(f"\nExample - {example_feature}:")
print(f" Category: {config.get_category(example_feature)}")
print(
f" Mandatory attributes: {config.get_mandatory_attributes(example_feature)}"
)