evalidate 2.1.5__tar.gz → 2.1.6__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: evalidate
3
- Version: 2.1.5
3
+ Version: 2.1.6
4
4
  Summary: Validation and secure evaluation of untrusted python expressions
5
5
  Project-URL: Homepage, https://github.com/yaroslaff/evalidate
6
6
  Project-URL: Issues, https://github.com/yaroslaff/evalidate/issues
@@ -6,7 +6,7 @@ import ast
6
6
  import dataclasses
7
7
  from typing import Callable
8
8
 
9
- __version__ = '2.1.5'
9
+ __version__ = '2.1.6'
10
10
 
11
11
 
12
12
  class EvalException(Exception):
@@ -5,6 +5,7 @@ import json
5
5
  import sys
6
6
  import argparse
7
7
  import time
8
+ import keyword
8
9
 
9
10
  def short_repr(data, limit=100):
10
11
  try:
@@ -22,7 +23,8 @@ def get_args():
22
23
  parser.add_argument('-l', '--jsonl', default=False, action='store_true', help='input is JSONL (not JSON list)')
23
24
  parser.add_argument('-k', '--keypath', type=str, help='key path to list separated by ::, e.g.: -k shop::products::onstock')
24
25
  parser.add_argument('-f', '--format', type=str, help='format (if not JSON), e.g. -f \'{sku} {price} ({stock}) {title!r}\'')
25
- parser.add_argument('expr', help='Python filter expression')
26
+ parser.add_argument('-F', '--fix', action='store_true', help='check each dict key and rename if it\'s a reserved word (e.g. "from" -> "_from")')
27
+ parser.add_argument('expr', nargs='?', help='Python filter expression')
26
28
  parser.add_argument('path', default=None, help='Path to JSON list (or stdin)', nargs='?')
27
29
 
28
30
  return parser.parse_args()
@@ -41,6 +43,9 @@ def main():
41
43
  args.verbose = True
42
44
  out = list()
43
45
 
46
+ if args.expr is None:
47
+ args.expr = 'True'
48
+
44
49
  vprint(f'# loading data from {args.path or "stdin"}')
45
50
 
46
51
  if args.path:
@@ -48,11 +53,25 @@ def main():
48
53
  else:
49
54
  f = sys.stdin
50
55
 
56
+
51
57
  with f:
52
58
  if args.jsonl:
53
59
  data = [json.loads(line) for line in f if line.strip()]
54
60
  else:
55
- data = json.load(f)
61
+ try:
62
+ data = json.load(f)
63
+ except json.JSONDecodeError as e:
64
+ if e.msg == 'Extra data':
65
+ print(f"Error decoding JSON: {e}", file=sys.stderr)
66
+ print("Hint: use --jsonl / -l for JSONL files (one JSON object per line)")
67
+ sys.exit(1)
68
+
69
+ print(f"Error decoding JSON: {e}", file=sys.stderr)
70
+ print(e.msg)
71
+ print(type(e))
72
+ print(dir(e))
73
+
74
+ sys.exit(1)
56
75
 
57
76
  # follow keypath if needed
58
77
  try:
@@ -63,6 +82,15 @@ def main():
63
82
  print(f"No such key: {e}", file=sys.stderr)
64
83
  sys.exit(1)
65
84
 
85
+
86
+ if args.fix:
87
+ print("# fixing reserved words in dict keys")
88
+ for d in data:
89
+ if isinstance(d, dict):
90
+ for k in list(d.keys()):
91
+ if keyword.iskeyword(k):
92
+ d[f'_{k}'] = d.pop(k)
93
+
66
94
  vprint(f'# loaded {len(data)} records from {args.path}')
67
95
 
68
96
 
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes