|
| 1 | +import os |
1 | 2 | import gpt |
| 3 | +import jsonschema |
2 | 4 |
|
3 | | -_component = __file__.split('.')[0] |
4 | | -_schemafile = '.'.join([_component, 'schema', 'json']) |
| 5 | +_curdir = os.path.dirname(os.path.abspath(__file__)) |
5 | 6 |
|
6 | | -with open(_schemafile) as f: |
| 7 | + |
| 8 | +def read_schemas(basedir, pattern='*.schema.json'): |
| 9 | + """ |
| 10 | + Return a dictionary with all schemas from 'basedir' matching 'pattern' |
| 11 | + """ |
7 | 12 | import json |
8 | | - schema = json.load(f) |
| 13 | + from glob import glob |
9 | 14 |
|
10 | | -def validate(gpkg_path): |
11 | | - # import fiona |
12 | | - # data = fiona.listlayers(gpkg_path) |
13 | | - gpkg = gpt.read_file(gpkg_path) |
| 15 | + schemas = {} |
| 16 | + for fn in glob(os.path.join(basedir, pattern)): |
| 17 | + try: |
| 18 | + with open(fn) as f: |
| 19 | + js = json.load(f) |
| 20 | + except: |
| 21 | + print(f"Error loading JSON: '{fn}'") |
| 22 | + raise |
| 23 | + else: |
| 24 | + # If the schema has no "$id" key, we adhoc push one: the filename, |
| 25 | + # which is reasonable since this is the simplest, valid, and |
| 26 | + # normally the way schemas will cross-reference each other. |
| 27 | + _fn = os.path.basename(fn) |
| 28 | + if "$id" not in js: |
| 29 | + js["$id"] = _fn # +"#" |
| 30 | + schemas[js["$id"]] = js |
| 31 | + |
| 32 | + return schemas |
| 33 | + |
| 34 | + |
| 35 | +schema_store = read_schemas(_curdir) |
14 | 36 |
|
15 | | - # print("\nGeoPackage layers/columns:") |
16 | | - # for name,table in gpkg.layers: |
17 | | - # print(name) |
18 | | - # print(table.columns) |
19 | | - # print() |
20 | 37 |
|
21 | | - # This is a workaround during devel, until I understand how to tell jsonschema about the DataFrames |
22 | | - # data = {ln:{} for ln in gpkg.list()} |
23 | | - # data['layer_styles']['geometry'] = list(gpkg['layer_styles']['geometry'].values) |
24 | | - # data['layer_styles']['geometry'] = [] |
25 | | - # print(data) |
26 | | - data = gpkg.to_json() |
| 38 | +def validate(gpkg_path, schema='geopackage_layers.schema.json'): |
| 39 | + # from jsonschema import Draft7Validator as Validator |
| 40 | + from jsonschema import RefResolver |
| 41 | + from jsonschema.validators import validator_for |
27 | 42 |
|
28 | | - import jsonschema |
29 | | - res = jsonschema.validate(data, schema) |
| 43 | + # If we had a simple schmea we could use jsonschema's 'validate' function |
| 44 | + # (as we did as first): |
| 45 | + # > import jsonschema |
| 46 | + # > res = jsonschema.validate(data, schema) |
| 47 | + # |
| 48 | + # But since the schema tree got a bit more complex, |
| 49 | + # > https://json-schema.org/understanding-json-schema/structuring.html, |
| 50 | + # we now have to creack open the components a little bit. |
| 51 | + # |
| 52 | + # One of the steps taken was to create a very simple "base" schema. |
| 53 | + # The "base" schema has two purposes: (1) to be used as the base |
| 54 | + # schema for jsonschema's RefResolver object, and (2) to define the |
| 55 | + # version of json-schema (currently draft-07) we're using in one single place. |
| 56 | + |
| 57 | + # Since we have "refs" in our schemas, we need a resolver to link them |
| 58 | + resolver = RefResolver.from_schema(schema_store['base.schema.json'], store=schema_store) |
| 59 | + |
| 60 | + # Get the correct (or best) validator for our schema's version |
| 61 | + Validator = validator_for(schema_store['base.schema.json']) |
| 62 | + |
| 63 | + # Put them all together to define the validator/schema set to use |
| 64 | + validator = Validator(schema_store[schema], resolver=resolver) |
| 65 | + |
| 66 | + gpkg = gpt.read_file(gpkg_path) |
| 67 | + data = gpkg.to_dict() |
30 | 68 |
|
| 69 | + res = validator.validate(data) |
31 | 70 | return res |
0 commit comments