json-schema-utils 0.8__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_schema_utils-0.8.dist-info/METADATA +119 -0
- json_schema_utils-0.8.dist-info/RECORD +15 -0
- json_schema_utils-0.8.dist-info/WHEEL +5 -0
- json_schema_utils-0.8.dist-info/entry_points.txt +7 -0
- json_schema_utils-0.8.dist-info/licenses/LICENSE +1 -0
- json_schema_utils-0.8.dist-info/top_level.txt +1 -0
- jsutils/__init__.py +5 -0
- jsutils/convert.py +934 -0
- jsutils/inline.py +206 -0
- jsutils/recurse.py +90 -0
- jsutils/schemas.py +151 -0
- jsutils/scripts.py +396 -0
- jsutils/simplify.py +580 -0
- jsutils/stats.py +1310 -0
- jsutils/utils.py +44 -0
jsutils/utils.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
from typing import Callable
|
|
2
|
+
import logging
|
|
3
|
+
import sys
|
|
4
|
+
|
|
5
|
+
log = logging.getLogger("JSU")
|
|
6
|
+
|
|
7
|
+
# simplistic typing
|
|
8
|
+
type Jsonable = None|bool|int|float|str|list[Jsonable]|dict[str, Jsonable]
|
|
9
|
+
JsonSchema = dict[str, Jsonable]|bool
|
|
10
|
+
FilterFun = Callable[[JsonSchema, list[str]], bool]
|
|
11
|
+
RewriteFun = Callable[[JsonSchema, list[str]], JsonSchema]
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class JSUError(BaseException):
|
|
15
|
+
pass
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def only(schema, *props):
|
|
19
|
+
"""Tell whether schema only contains these props."""
|
|
20
|
+
assert isinstance(schema, dict)
|
|
21
|
+
ok = set(schema.keys()).issubset(set(props))
|
|
22
|
+
if not ok:
|
|
23
|
+
ttype = schema.get("type", "<>")
|
|
24
|
+
log.debug(f"BAD SCHEMA {ttype}: {list(schema.keys())} {props}")
|
|
25
|
+
return ok
|
|
26
|
+
|
|
27
|
+
def has(schema, *props):
|
|
28
|
+
"""Tell whether schema has any of these props."""
|
|
29
|
+
assert isinstance(schema, dict)
|
|
30
|
+
for p in schema.keys():
|
|
31
|
+
if p not in props:
|
|
32
|
+
return False
|
|
33
|
+
return True
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def openfiles(args: list[str] = []):
|
|
37
|
+
if not args: # empty list is same as stdin
|
|
38
|
+
args = ["-"]
|
|
39
|
+
for fn in args:
|
|
40
|
+
if fn == "-":
|
|
41
|
+
yield fn, sys.stdin
|
|
42
|
+
else:
|
|
43
|
+
with open(fn) as fh:
|
|
44
|
+
yield fn, fh
|