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.
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