-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtemplate_loader.py
More file actions
251 lines (203 loc) · 7.84 KB
/
Copy pathtemplate_loader.py
File metadata and controls
251 lines (203 loc) · 7.84 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/env python3
"""
Template Loader for ASTM Mock Server
Loads and validates analyzer templates against the JSON schema.
Templates define analyzer-specific field lists, ASTM identifiers,
and deterministic seed values for reproducible testing.
Usage:
from template_loader import TemplateLoader
loader = TemplateLoader()
template = loader.load_template('horiba_pentra60')
CLI:
python template_loader.py --list
python template_loader.py --validate templates/horiba_pentra60.json
"""
import json
import argparse
import sys
from pathlib import Path
from typing import Dict, List, Optional, Any
# Try to import jsonschema, provide helpful message if not installed
try:
import jsonschema
HAS_JSONSCHEMA = True
except ImportError:
HAS_JSONSCHEMA = False
class TemplateLoader:
"""Loads and validates analyzer templates from JSON files."""
def __init__(self, templates_dir: str = None):
"""Initialize loader with templates directory.
Args:
templates_dir: Path to templates directory. Defaults to 'templates/'
relative to this script.
"""
if templates_dir is None:
self.templates_dir = Path(__file__).parent / 'templates'
else:
self.templates_dir = Path(templates_dir)
self.schema = self._load_schema()
self._template_cache: Dict[str, Dict] = {}
def _load_schema(self) -> Optional[Dict]:
"""Load JSON schema for template validation."""
schema_path = self.templates_dir / 'schema.json'
if not schema_path.exists():
print(f"Warning: Schema file not found at {schema_path}", file=sys.stderr)
return None
try:
with open(schema_path, 'r', encoding='utf-8') as f:
return json.load(f)
except json.JSONDecodeError as e:
print(f"Error: Invalid JSON in schema file: {e}", file=sys.stderr)
return None
def load_template(self, template_name: str, validate: bool = True) -> Dict:
"""Load a template by name.
Args:
template_name: Template name (without .json extension)
validate: Whether to validate against schema (default True)
Returns:
Template dictionary
Raises:
FileNotFoundError: If template file doesn't exist
json.JSONDecodeError: If template is invalid JSON
jsonschema.ValidationError: If template fails schema validation
"""
# Check cache first
if template_name in self._template_cache:
return self._template_cache[template_name]
# Build template path
template_path = self.templates_dir / f'{template_name}.json'
if not template_path.exists():
raise FileNotFoundError(f"Template not found: {template_path}")
# Load template
with open(template_path, 'r', encoding='utf-8') as f:
template = json.load(f)
# Validate against schema
if validate and self.schema and HAS_JSONSCHEMA:
jsonschema.validate(template, self.schema)
# Cache and return
self._template_cache[template_name] = template
return template
def validate_template(self, template_path: str) -> bool:
"""Validate a template file against the schema.
Args:
template_path: Path to template file
Returns:
True if valid, False otherwise
"""
if not HAS_JSONSCHEMA:
print("Warning: jsonschema not installed. Install with: pip install jsonschema")
print("Performing basic JSON syntax check only.")
try:
with open(template_path, 'r', encoding='utf-8') as f:
template = json.load(f)
if HAS_JSONSCHEMA and self.schema:
jsonschema.validate(template, self.schema)
return True
except json.JSONDecodeError as e:
print(f"Invalid JSON: {e}", file=sys.stderr)
return False
except Exception as e:
# Handle jsonschema.ValidationError when jsonschema is installed
if HAS_JSONSCHEMA and isinstance(e, jsonschema.ValidationError):
print(f"Schema validation failed: {e.message}", file=sys.stderr)
else:
print(f"Validation error: {e}", file=sys.stderr)
return False
def list_templates(self) -> List[str]:
"""List all available template names.
Returns:
List of template names (without .json extension)
"""
templates = []
if self.templates_dir.exists():
for f in self.templates_dir.glob('*.json'):
if f.stem != 'schema': # Exclude schema.json
templates.append(f.stem)
return sorted(templates)
def get_template_info(self, template_name: str) -> Dict[str, Any]:
"""Get summary information about a template.
Args:
template_name: Template name
Returns:
Dictionary with template info (name, manufacturer, field count, etc.)
"""
template = self.load_template(template_name, validate=False)
analyzer = template.get('analyzer', {})
protocol = template.get('protocol', {})
fields = template.get('fields', [])
return {
'name': analyzer.get('name', 'Unknown'),
'manufacturer': analyzer.get('manufacturer', 'Unknown'),
'model': analyzer.get('model', ''),
'protocol': protocol.get('type', 'Unknown'),
'version': protocol.get('version', ''),
'field_count': len(fields),
'fields': [f.get('code', '') for f in fields]
}
def main():
"""CLI entry point for template loader."""
parser = argparse.ArgumentParser(
description='Load and validate ASTM mock server analyzer templates'
)
parser.add_argument(
'--list', '-l',
action='store_true',
help='List all available templates'
)
parser.add_argument(
'--validate', '-v',
type=str,
metavar='FILE',
help='Validate a template file against the schema'
)
parser.add_argument(
'--info', '-i',
type=str,
metavar='NAME',
help='Show information about a template'
)
parser.add_argument(
'--templates-dir', '-d',
type=str,
help='Templates directory path'
)
args = parser.parse_args()
loader = TemplateLoader(args.templates_dir)
if args.list:
templates = loader.list_templates()
if templates:
print("Available templates:")
for t in templates:
try:
info = loader.get_template_info(t)
print(f" {t}: {info['name']} ({info['field_count']} fields)")
except Exception as e:
print(f" {t}: (error loading: {e})")
else:
print("No templates found.")
return 0
if args.validate:
print(f"Validating: {args.validate}")
if loader.validate_template(args.validate):
print("✅ Template is valid")
return 0
else:
print("❌ Template validation failed")
return 1
if args.info:
try:
info = loader.get_template_info(args.info)
print(f"Template: {args.info}")
print(f" Analyzer: {info['name']}")
print(f" Manufacturer: {info['manufacturer']}")
print(f" Model: {info['model']}")
print(f" Protocol: {info['protocol']} {info['version']}")
print(f" Fields ({info['field_count']}): {', '.join(info['fields'])}")
return 0
except FileNotFoundError:
print(f"Template not found: {args.info}")
return 1
parser.print_help()
return 0
if __name__ == '__main__':
sys.exit(main())