json-model-compiler 2.0b0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- json_model/README.md +32 -0
- json_model/__init__.py +18 -0
- json_model/analyze.py +409 -0
- json_model/clang.py +563 -0
- json_model/convert.py +430 -0
- json_model/data/clang_entry.c +21 -0
- json_model/data/clang_free.c +10 -0
- json_model/data/clang_init.c +11 -0
- json_model/data/clang_pcre2_fun.c +6 -0
- json_model/data/clang_re2_fun.c +5 -0
- json_model/data/javascript_exe.js +4 -0
- json_model/data/javascript_foot.js +9 -0
- json_model/data/javascript_free.js +9 -0
- json_model/data/javascript_head.js +1 -0
- json_model/data/javascript_init.js +11 -0
- json_model/data/javascript_main.js +5 -0
- json_model/data/plpgsql_entry.sql +16 -0
- json_model/data/python_entry.py +11 -0
- json_model/data/python_exe.py +1 -0
- json_model/data/python_free.py +6 -0
- json_model/data/python_init.py +9 -0
- json_model/data/python_main.py +4 -0
- json_model/javascript.py +310 -0
- json_model/language.py +748 -0
- json_model/model.py +1084 -0
- json_model/mtypes.py +42 -0
- json_model/objmerge.py +165 -0
- json_model/optim.py +486 -0
- json_model/plpgsql.py +422 -0
- json_model/python.py +269 -0
- json_model/recurse.py +127 -0
- json_model/resolver.py +106 -0
- json_model/runtime/META.json +30 -0
- json_model/runtime/Makefile +21 -0
- json_model/runtime/__init__.py +32 -0
- json_model/runtime/json-model.c +1054 -0
- json_model/runtime/json-model.h +183 -0
- json_model/runtime/json_model--2.0.sql +261 -0
- json_model/runtime/json_model.control +2 -0
- json_model/runtime/main.c +473 -0
- json_model/runtime/support.py +485 -0
- json_model/runtime/types.py +16 -0
- json_model/script.py +585 -0
- json_model/url_cache.py +82 -0
- json_model/utils.py +623 -0
- json_model/xstatic.py +1444 -0
- json_model_compiler-2.0b0.dist-info/METADATA +201 -0
- json_model_compiler-2.0b0.dist-info/RECORD +51 -0
- json_model_compiler-2.0b0.dist-info/WHEEL +5 -0
- json_model_compiler-2.0b0.dist-info/entry_points.txt +2 -0
- json_model_compiler-2.0b0.dist-info/top_level.txt +1 -0
json_model/recurse.py
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
#
|
|
2
|
+
# Generic recursion on models in a JSON Model
|
|
3
|
+
#
|
|
4
|
+
# TODO multi recurse?
|
|
5
|
+
# TODO allow extending the parameterization
|
|
6
|
+
#
|
|
7
|
+
from .mtypes import ModelType, ModelPath, ModelFilter, ModelRewrite, ModelError
|
|
8
|
+
from .utils import log
|
|
9
|
+
|
|
10
|
+
ROOT_KEYWORDS = {"~", "$", "%"}
|
|
11
|
+
# .mo and .in are extensions
|
|
12
|
+
ANYWHERE_KEYWORDS = {"#", ".schema"}
|
|
13
|
+
CONSTRAINT_KEYWORDS = {"=", "!=", "<", "<=", ">", ">=", "!", ".mo", ".in"}
|
|
14
|
+
NO_MODEL_KEYWORDS = ANYWHERE_KEYWORDS | CONSTRAINT_KEYWORDS | {"~", "/"}
|
|
15
|
+
MODEL_KEYWORD = {"@"}
|
|
16
|
+
# "/" expects a list, but we do not want to recurse there
|
|
17
|
+
MODEL_LIST_KEYWORDS = {"|", "&", "^", "+"}
|
|
18
|
+
MODEL_VALUE_KEYWORDS = {"$", "%", "*"}
|
|
19
|
+
|
|
20
|
+
ALL_KEYWORDS = NO_MODEL_KEYWORDS | MODEL_KEYWORD | MODEL_LIST_KEYWORDS | MODEL_VALUE_KEYWORDS
|
|
21
|
+
|
|
22
|
+
def _recModel(
|
|
23
|
+
model: ModelType,
|
|
24
|
+
path: ModelPath,
|
|
25
|
+
flt: ModelFilter,
|
|
26
|
+
rwt: ModelRewrite,
|
|
27
|
+
keys: bool,
|
|
28
|
+
root: bool,
|
|
29
|
+
) -> ModelType:
|
|
30
|
+
|
|
31
|
+
if not flt(model, path):
|
|
32
|
+
return model
|
|
33
|
+
|
|
34
|
+
# actual recursion
|
|
35
|
+
if isinstance(model, list):
|
|
36
|
+
return [_recModel(m, path + [i], flt, rwt, keys, False) for i, m in enumerate(model)]
|
|
37
|
+
elif isinstance(model, dict):
|
|
38
|
+
mkeys: list[str] = list(model.keys())
|
|
39
|
+
for prop in mkeys:
|
|
40
|
+
val = model[prop]
|
|
41
|
+
lpath = path + [prop]
|
|
42
|
+
assert isinstance(prop, str), f"properties are strings {lpath}"
|
|
43
|
+
if prop in MODEL_KEYWORD:
|
|
44
|
+
okprops = {prop} | ANYWHERE_KEYWORDS | CONSTRAINT_KEYWORDS
|
|
45
|
+
if root:
|
|
46
|
+
okprops.update(ROOT_KEYWORDS)
|
|
47
|
+
assert not (set(mkeys) - okprops), f"@ restricts other keywords {lpath}"
|
|
48
|
+
model[prop] = _recModel(val, lpath, flt, rwt, keys, False)
|
|
49
|
+
elif prop in NO_MODEL_KEYWORDS:
|
|
50
|
+
# some sanity checks in passing
|
|
51
|
+
if prop == "#":
|
|
52
|
+
assert isinstance(val, str), f"# is a string {lpath}"
|
|
53
|
+
elif prop == "~":
|
|
54
|
+
assert root and isinstance(val, str), f"~ is a string at root {lpath}"
|
|
55
|
+
elif prop == "!":
|
|
56
|
+
assert isinstance(val, bool), f"! is a bool {lpath}"
|
|
57
|
+
elif prop == "/":
|
|
58
|
+
assert isinstance(val, list), f"/ is a list {lpath}"
|
|
59
|
+
continue
|
|
60
|
+
elif prop in MODEL_LIST_KEYWORDS:
|
|
61
|
+
assert isinstance(val, list), f"{prop} is a list {lpath}"
|
|
62
|
+
model[prop] = _recModel(val, lpath, flt, rwt, keys, False)
|
|
63
|
+
okprops = {prop, "#"}
|
|
64
|
+
if root:
|
|
65
|
+
okprops.update(ROOT_KEYWORDS)
|
|
66
|
+
assert not (set(mkeys) - okprops), f"{prop} restricts other keywords {lpath}"
|
|
67
|
+
elif prop == "%": # renames and rewrites
|
|
68
|
+
assert root and isinstance(val, dict), f"% transformations at root {lpath}"
|
|
69
|
+
for k, v in val.items():
|
|
70
|
+
assert isinstance(k, str), f"% props are strings {lpath}"
|
|
71
|
+
if k == "#":
|
|
72
|
+
assert isinstance(v, str), f"# is a string {lpath}"
|
|
73
|
+
elif k == "<":
|
|
74
|
+
is_ref = lambda s: isinstance(s, str) and s and s[0] == "$"
|
|
75
|
+
assert is_ref(v) or isinstance(v, list) and all(map(is_ref, v)), \
|
|
76
|
+
"%.< is a reference or list of references"
|
|
77
|
+
elif k.startswith("."): # rename
|
|
78
|
+
assert isinstance(v, str), f"rename to a string {lpath + [k]}"
|
|
79
|
+
assert v in ALL_KEYWORDS, f"rename to a valid keyword {lpath + [k]}"
|
|
80
|
+
else: # rewrite
|
|
81
|
+
# FIXME recurse or not?
|
|
82
|
+
# model[k] = _recModel(v, path + ["%", k], flt, rwt)
|
|
83
|
+
continue
|
|
84
|
+
elif prop in MODEL_VALUE_KEYWORDS:
|
|
85
|
+
if prop == "$":
|
|
86
|
+
assert root and isinstance(val, dict), f"$ definitions at root {lpath}"
|
|
87
|
+
model[prop] = _recModel(val, lpath, flt, rwt, keys, False)
|
|
88
|
+
else: # assume properties
|
|
89
|
+
model[prop] = _recModel(val, lpath, flt, rwt, keys, False)
|
|
90
|
+
if keys: # possibly rewrite key references
|
|
91
|
+
if prop != "" and prop[0] == "$":
|
|
92
|
+
log.debug(f"### considering key ref {prop}")
|
|
93
|
+
nprop = _recModel(prop, lpath, flt, rwt, keys, False)
|
|
94
|
+
log.debug(f"nprop={nprop}")
|
|
95
|
+
assert isinstance(nprop, str)
|
|
96
|
+
# substitution to an optional string
|
|
97
|
+
if nprop and nprop[0] not in ("$", "/", "="):
|
|
98
|
+
nprop = "?" + (nprop[1:] if nprop[0] == "_" else nprop)
|
|
99
|
+
if nprop != prop:
|
|
100
|
+
if nprop in model:
|
|
101
|
+
raise ModelError(f"cannot override key {prop}: {nprop} {lpath}")
|
|
102
|
+
model[nprop] = model[prop]
|
|
103
|
+
del model[prop]
|
|
104
|
+
else: # sanity check
|
|
105
|
+
# FIXME could/should recurse on str?
|
|
106
|
+
assert model is None or isinstance(model, (bool, int, float, str))
|
|
107
|
+
|
|
108
|
+
return rwt(model, path)
|
|
109
|
+
|
|
110
|
+
def allFlt(_m: ModelType, _p: ModelPath) -> bool:
|
|
111
|
+
return True
|
|
112
|
+
|
|
113
|
+
def builtFlt(m: ModelType, _p: ModelPath) -> bool:
|
|
114
|
+
return isinstance(m, (list, dict))
|
|
115
|
+
|
|
116
|
+
def noRwt(m: ModelType, _p: ModelPath) -> ModelType:
|
|
117
|
+
return m
|
|
118
|
+
|
|
119
|
+
def recModel(
|
|
120
|
+
model: ModelType,
|
|
121
|
+
flt: ModelFilter = lambda _m, _p: True,
|
|
122
|
+
rwt: ModelRewrite = lambda m, _p: m,
|
|
123
|
+
keys: bool = False,
|
|
124
|
+
root: bool = True,
|
|
125
|
+
) -> ModelType:
|
|
126
|
+
|
|
127
|
+
return _recModel(model, [], flt, rwt, keys, root)
|
json_model/resolver.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
import re
|
|
3
|
+
import json
|
|
4
|
+
import yaml
|
|
5
|
+
|
|
6
|
+
from .mtypes import ModelError, ModelPath, Jsonable
|
|
7
|
+
from .url_cache import JsonURLCache
|
|
8
|
+
from .utils import log
|
|
9
|
+
|
|
10
|
+
JSON_SUFFIX = ["", ".json", ".model", ".model.json"]
|
|
11
|
+
YAML_SUFFIX = [".yaml", ".model.yaml", ".yml", ".model.yml"]
|
|
12
|
+
JS_SUFFIX = [".js", ".model.js"]
|
|
13
|
+
|
|
14
|
+
# guess type based on file contents
|
|
15
|
+
JS_RE = r"(?m)^\s*//\s"
|
|
16
|
+
JSON_RE = r"(?s)\s*(\[.*\]|\{.*\}|\".*\"|-?\d+(\.\d*)?([eE]-?\d+)?|null|true|false)\s*$"
|
|
17
|
+
|
|
18
|
+
# numbers, identifiers, "quoted", 'quoted', blanks, whatever (1 char, including separators)
|
|
19
|
+
JS_TOKEN = r"(?s)(-?\d+(\.\d*)?([eE]-?\d+)?|\w+|" + \
|
|
20
|
+
r'"([^"]*(\\")?)*"|' + r"'([^']*(\\')?)*'|\s+|\W)"
|
|
21
|
+
|
|
22
|
+
def jsob2json(string: str) -> str:
|
|
23
|
+
tokens: list[str] = []
|
|
24
|
+
for mtoken in re.finditer(JS_TOKEN, string):
|
|
25
|
+
token = mtoken.group(0)
|
|
26
|
+
if (re.match(r"\w", token) and token[0] not in "0123456789" and
|
|
27
|
+
token not in ("null", "false", "true")):
|
|
28
|
+
tokens.append(f"\"{token}\"")
|
|
29
|
+
else:
|
|
30
|
+
tokens.append(token)
|
|
31
|
+
return "".join(tokens)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class Resolver:
|
|
35
|
+
"""Resolve external references to jsonable data.
|
|
36
|
+
|
|
37
|
+
- `cache_dir`: where to store downloaded jsons.
|
|
38
|
+
- `maps`: url to directory mapping for testing.
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
def __init__(self, cache_dir: str|None = None, maps: dict[str, str]|None = None):
|
|
42
|
+
self._cache = JsonURLCache(cache_dir)
|
|
43
|
+
self._maps: dict[str, str] = maps if maps else {}
|
|
44
|
+
self._jsons: dict[str, Jsonable] = {}
|
|
45
|
+
|
|
46
|
+
def __call__(self, url: str, *, path: ModelPath = [], follow: bool = True):
|
|
47
|
+
"""Resolve an external reference."""
|
|
48
|
+
|
|
49
|
+
assert "#" not in url, f"no fragment in {url}"
|
|
50
|
+
|
|
51
|
+
if url == "-": # no caching, cannot read same input twice?
|
|
52
|
+
log.info("reading: stdin")
|
|
53
|
+
return json.load(sys.stdin)
|
|
54
|
+
|
|
55
|
+
# possibly follow mappings
|
|
56
|
+
if follow:
|
|
57
|
+
changes, previous, skip = 0, -1, set()
|
|
58
|
+
while changes != previous:
|
|
59
|
+
previous = changes
|
|
60
|
+
for s, d in self._maps.items():
|
|
61
|
+
if s not in skip and url.startswith(s):
|
|
62
|
+
url = d + url[len(s):]
|
|
63
|
+
skip.add(s) # do not apply same rule twice
|
|
64
|
+
changes += 1
|
|
65
|
+
|
|
66
|
+
if url in self._jsons:
|
|
67
|
+
return self._jsons[url]
|
|
68
|
+
|
|
69
|
+
# handle local files
|
|
70
|
+
if url.startswith("./") or url.startswith("../") or url.startswith("/"):
|
|
71
|
+
file = url
|
|
72
|
+
elif url.startswith("file://"):
|
|
73
|
+
file = url[7:]
|
|
74
|
+
elif ":" not in url:
|
|
75
|
+
file = "./" + url
|
|
76
|
+
else:
|
|
77
|
+
file = None
|
|
78
|
+
|
|
79
|
+
if file:
|
|
80
|
+
for suffix in JSON_SUFFIX + JS_SUFFIX + YAML_SUFFIX:
|
|
81
|
+
fn = file + suffix
|
|
82
|
+
try:
|
|
83
|
+
with open(fn) as f:
|
|
84
|
+
log.info(f"loading: {fn}")
|
|
85
|
+
content = f.read()
|
|
86
|
+
if fn.endswith(".js") or re.match(JS_RE, content):
|
|
87
|
+
# possibly remove js comments
|
|
88
|
+
content = re.sub(r"(?s)/\*.*?\*/", "", content)
|
|
89
|
+
content = re.sub(r"\s*//\s.*", "", content)
|
|
90
|
+
# quote bare identifiers
|
|
91
|
+
content = jsob2json(content)
|
|
92
|
+
# guess content type
|
|
93
|
+
if fn.endswith(".json") or re.match(JSON_RE, content):
|
|
94
|
+
# try JSON
|
|
95
|
+
self._jsons[url] = j = json.loads(content)
|
|
96
|
+
else: # try yaml
|
|
97
|
+
self._jsons[url] = j = yaml.full_load(content)
|
|
98
|
+
return j
|
|
99
|
+
except FileNotFoundError:
|
|
100
|
+
log.debug(f"no such file: {fn}")
|
|
101
|
+
continue
|
|
102
|
+
raise ModelError(f"cannot resolve: {file}")
|
|
103
|
+
else: # other actual URLs will download
|
|
104
|
+
log.info(f"downloading: {url}")
|
|
105
|
+
self._jsons[url] = j = self._cache.load(url)
|
|
106
|
+
return j
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "json_model",
|
|
3
|
+
"abstract": "PL/pgSQL Runtime for JSON Model",
|
|
4
|
+
"description": "This runtime is needed by JSON Model generated PL/pgSQL code to validate JSON values.",
|
|
5
|
+
"version": "2.0.0-alpha3",
|
|
6
|
+
"maintainer": "Fabien et Claire <maintainers@json-model.org>",
|
|
7
|
+
"license": "unrestricted",
|
|
8
|
+
"provides": {
|
|
9
|
+
"json_model": {
|
|
10
|
+
"file": "json_model--1.0.sql",
|
|
11
|
+
"version": "2.0.0-alpha3"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"resources": {
|
|
15
|
+
"bugtracker": {
|
|
16
|
+
"web": "https://github.com/clairey-zx81/json-model/issues"
|
|
17
|
+
},
|
|
18
|
+
"repository": {
|
|
19
|
+
"url": "https://github.com/clairey-zx81/json-model.git",
|
|
20
|
+
"web": "https://github.com/clairey-zx81/json-model",
|
|
21
|
+
"type": "git"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"generated_by": "Fabien",
|
|
25
|
+
"meta-spec": {
|
|
26
|
+
"version": "1.0.0",
|
|
27
|
+
"url": "https://pgxn.org/meta/spec.txt"
|
|
28
|
+
},
|
|
29
|
+
"tags": [ "JSON", "JSON Model", "data validation", "data type" ]
|
|
30
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
#
|
|
2
|
+
# PostGres eXtenSion makefile
|
|
3
|
+
#
|
|
4
|
+
|
|
5
|
+
EXTENSION = json_model
|
|
6
|
+
DATA = json_model--2.0.sql
|
|
7
|
+
|
|
8
|
+
PG_CONFIG = pg_config
|
|
9
|
+
PGXS := $(shell $(PG_CONFIG) --pgxs)
|
|
10
|
+
include $(PGXS)
|
|
11
|
+
|
|
12
|
+
# pgxn
|
|
13
|
+
|
|
14
|
+
clean: pgxn.clean
|
|
15
|
+
.PHONY: pgxn.clean
|
|
16
|
+
pgxn.clean:
|
|
17
|
+
$(RM) json_model.tgz
|
|
18
|
+
|
|
19
|
+
pgxn: json_model.tgz
|
|
20
|
+
json_model.tgz:
|
|
21
|
+
tar czf $@ json_model--2.0.sql json_model.control META.json Makefile
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
from .types import (
|
|
2
|
+
Jsonable,
|
|
3
|
+
Path,
|
|
4
|
+
Report,
|
|
5
|
+
PropMap,
|
|
6
|
+
CheckFun,
|
|
7
|
+
RegexFun,
|
|
8
|
+
EntryCheckFun,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
from .support import (
|
|
12
|
+
# TODO remove _
|
|
13
|
+
UNDEFINED,
|
|
14
|
+
_tname,
|
|
15
|
+
_path,
|
|
16
|
+
_rep,
|
|
17
|
+
Const,
|
|
18
|
+
ConstSet,
|
|
19
|
+
ConstMap,
|
|
20
|
+
is_valid_date,
|
|
21
|
+
is_valid_time,
|
|
22
|
+
is_valid_datetime,
|
|
23
|
+
is_valid_regex,
|
|
24
|
+
is_valid_exreg,
|
|
25
|
+
is_valid_url,
|
|
26
|
+
is_valid_email,
|
|
27
|
+
is_valid_uuid,
|
|
28
|
+
is_unique_array,
|
|
29
|
+
check_constraint,
|
|
30
|
+
value_len,
|
|
31
|
+
main,
|
|
32
|
+
)
|