22Author : zhangxianbing1
33Date : 2020-12-27 09:22:14
44LastEditors : zhangxianbing1
5- LastEditTime : 2020-12-30 17:16:18
5+ LastEditTime : 2020-12-30 17:28:02
66Description : JSONPath
77"""
88__version__ = "1.0.0"
1313import os
1414import re
1515import sys
16- from typing import Any , Dict , Iterable , Union
16+ from typing import Any , Dict , Iterable
1717from collections import defaultdict
1818
1919RESULT_TYPE = {
3333REP_DOT = re .compile (r"(?<!\.)\.(?!\.)" )
3434REP_FILTER = re .compile (r"\[(\??\(.*?\))\]" )
3535REP_SORT = re .compile (r"\[[\\|/](.*?),\]" )
36- _DEBUG = os . getenv ( "PYTHONDEBUG" )
36+
3737LOG = logging .getLogger (__name__ )
38+ LOG .setLevel (logging .DEBUG )
39+
40+ # pylint: disable=invalid-name,missing-function-docstring,missing-class-docstring
3841
3942
4043def concat (x , y , con = SEP ):
@@ -46,16 +49,16 @@ class ExprSyntaxError(Exception):
4649
4750
4851class JSONPath :
49-
5052 # annotations
5153 result : Iterable
5254 result_type : str
5355 caller_globals : Dict [str , Any ]
5456 subx = defaultdict (list )
5557 ops : list
5658 oplen : int
59+ debug : int
5760
58- def __init__ (self , expr : str , * , result_type = "VALUE" ):
61+ def __init__ (self , expr : str , * , result_type = "VALUE" , debug = 0 ):
5962
6063 if result_type not in RESULT_TYPE :
6164 raise ValueError (f"result_type must be one of { tuple (RESULT_TYPE .keys ())} " )
@@ -67,25 +70,21 @@ def __init__(self, expr: str, *, result_type="VALUE"):
6770 self .ops = expr .split (SEP )
6871 self .oplen = len (self .ops )
6972
73+ self .debug = debug
74+
7075 def _parse_expr (self , expr ):
71- print ( f "before expr: { expr } " )
76+ LOG . debug ( "before expr: %s" , expr )
7277
7378 expr = REP_PICKUP_QUOTE .sub (self ._f_pickup_quote , expr )
74- # print(expr)
7579 expr = REP_PICKUP_BRACKET .sub (self ._f_pickup_bracket , expr )
76- # print(expr)
7780 expr = REP_DOUBLEDOTS .sub (f"{ SEP } ..{ SEP } " , expr )
78- # print(expr)
7981 expr = REP_DOT .sub (SEP , expr )
80- # print(expr)
8182 expr = REP_PUTBACK_BRACKET .sub (self ._f_putback_bracket , expr )
82- # print(expr)
8383 expr = REP_PUTBACK_QUOTE .sub (self ._f_putback_quote , expr )
84- # print(expr)
8584 if expr .startswith ("$;" ):
8685 expr = expr [2 :]
8786
88- print ( f"after expr: { expr } " )
87+ LOG . debug ( "before expr: %s" , expr )
8988 return expr
9089
9190 def _f_pickup_quote (self , m ):
0 commit comments