Mercurial > hgrepos > Python > libs > data-schema
comparison data_schema/__init__.py @ 29:68286d27f27d
FIX: Allow customization of the data stream loader (get_data_stream())
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Sat, 08 Jul 2023 16:10:36 +0200 |
| parents | db3491e1b590 |
| children | 2e7c08c356ee |
comparison
equal
deleted
inserted
replaced
| 28:db3491e1b590 | 29:68286d27f27d |
|---|---|
| 234 pass | 234 pass |
| 235 | 235 |
| 236 | 236 |
| 237 ValidationSettings = collections.namedtuple( | 237 ValidationSettings = collections.namedtuple( |
| 238 "ValidationSettings", | 238 "ValidationSettings", |
| 239 ["skip_keys", "break_on_keynames_problems", "schema_loader"]) | 239 ["skip_keys", "break_on_keynames_problems", |
| 240 "data_stream_loader", "schema_loader"]) | |
| 240 | 241 |
| 241 | 242 |
| 242 class _Schema(dict): | 243 class _Schema(dict): |
| 243 | 244 |
| 244 __slots__ = ("parent", "is_sub_root", "_schema_cache") | 245 __slots__ = ("parent", "is_sub_root", "_schema_cache") |
| 326 return "<_Schema " + super().__str__() + ">" | 327 return "<_Schema " + super().__str__() + ">" |
| 327 | 328 |
| 328 def __repr__(self): | 329 def __repr__(self): |
| 329 return "<_Schema " + super().__repr__() + ">" | 330 return "<_Schema " + super().__repr__() + ">" |
| 330 | 331 |
| 331 def get_cached_schema(self, key, load_if_needed=True, schema_loader=None): | 332 def get_cached_schema(self, key, load_if_needed=True, |
| 333 data_stream_loader=None, | |
| 334 schema_loader=None): | |
| 332 root = self.ROOT | 335 root = self.ROOT |
| 333 s = root._schema_cache.get(key, None) | 336 s = root._schema_cache.get(key, None) |
| 334 if s is None and load_if_needed: | 337 if s is None and load_if_needed: |
| 335 if schema_loader is None: | 338 if schema_loader is None: |
| 336 raise SchemaError("no schema loader available") | 339 raise SchemaError("no schema loader available") |
| 337 with get_data_stream(key) as schemastream: | 340 dsl = data_stream_loader or get_data_stream |
| 341 with dsl(key) as schemastream: | |
| 338 # load schema a new `$self' (i.e. sub-root is True) | 342 # load schema a new `$self' (i.e. sub-root is True) |
| 339 s = _Schema(self, True, schema_loader(schemastream)) | 343 s = _Schema(self, True, schema_loader(schemastream)) |
| 340 root._schema_cache[key] = s | 344 root._schema_cache[key] = s |
| 341 return s | 345 return s |
| 342 | 346 |
| 499 | 503 |
| 500 """ | 504 """ |
| 501 settings = { | 505 settings = { |
| 502 "skip_keys": None, | 506 "skip_keys": None, |
| 503 "break_on_keynames_problems": True, | 507 "break_on_keynames_problems": True, |
| 508 "data_stream_loader": get_data_stream, | |
| 504 "schema_loader": default_schema_loader | 509 "schema_loader": default_schema_loader |
| 505 } | 510 } |
| 506 settings.update(kwds) | 511 settings.update(kwds) |
| 507 if not isinstance(schema, _Schema): | 512 if not isinstance(schema, _Schema): |
| 508 if not isinstance(schema, dict): | 513 if not isinstance(schema, dict): |
| 1406 s = schema.ROOT | 1411 s = schema.ROOT |
| 1407 else: | 1412 else: |
| 1408 s = schema.get_cached_schema( | 1413 s = schema.get_cached_schema( |
| 1409 uri.path, | 1414 uri.path, |
| 1410 load_if_needed=True, | 1415 load_if_needed=True, |
| 1416 data_stream_loader=context.settings.data_stream_loader, | |
| 1411 schema_loader=context.settings.schema_loader) | 1417 schema_loader=context.settings.schema_loader) |
| 1412 if uri.fragment is None: | 1418 if uri.fragment is None: |
| 1413 raise SchemaError("fragment required in reference") | 1419 raise SchemaError("fragment required in reference") |
| 1414 | 1420 |
| 1415 if not uri.fragment.startswith('/'): | 1421 if not uri.fragment.startswith('/'): |
