1- from __future__ import print_function , absolute_import , division , generators , nested_scopes
1+ from __future__ import (
2+ print_function ,
3+ absolute_import ,
4+ division ,
5+ generators ,
6+ nested_scopes ,
7+ )
28import sys
39import os .path
4- import logging
510
611import ply .yacc
712
13+ from jsonpath_ng .exceptions import JsonPathParserError
814from jsonpath_ng .jsonpath import *
915from jsonpath_ng .lexer import JsonPathLexer
1016
1117logger = logging .getLogger (__name__ )
1218
19+
1320def parse (string ):
1421 return JsonPathParser ().parse (string )
1522
23+
1624class JsonPathParser (object ):
1725 '''
1826 An LALR-parser for JsonPath
@@ -21,8 +29,12 @@ class JsonPathParser(object):
2129 tokens = JsonPathLexer .tokens
2230
2331 def __init__ (self , debug = False , lexer_class = None ):
24- if self .__doc__ == None :
25- raise Exception ('Docstrings have been removed! By design of PLY, jsonpath-rw requires docstrings. You must not use PYTHONOPTIMIZE=2 or python -OO.' )
32+ if self .__doc__ is None :
33+ raise JsonPathParserError (
34+ 'Docstrings have been removed! By design of PLY, '
35+ 'jsonpath-rw requires docstrings. You must not use '
36+ 'PYTHONOPTIMIZE=2 or python -OO.'
37+ )
2638
2739 self .debug = debug
2840 self .lexer_class = lexer_class or JsonPathLexer # Crufty but works around statefulness in PLY
@@ -66,7 +78,8 @@ def parse_token_stream(self, token_iterator, start_symbol='jsonpath'):
6678 ]
6779
6880 def p_error (self , t ):
69- raise Exception ('Parse error at %s:%s near token %s (%s)' % (t .lineno , t .col , t .value , t .type ))
81+ raise JsonPathParserError ('Parse error at %s:%s near token %s (%s)'
82+ % (t .lineno , t .col , t .value , t .type ))
7083
7184 def p_jsonpath_binop (self , p ):
7285 """jsonpath : jsonpath '.' jsonpath
@@ -98,7 +111,8 @@ def p_jsonpath_named_operator(self, p):
98111 elif p [1 ] == 'parent' :
99112 p [0 ] = Parent ()
100113 else :
101- raise Exception ('Unknown named operator `%s` at %s:%s' % (p [1 ], p .lineno (1 ), p .lexpos (1 )))
114+ raise JsonPathParserError ('Unknown named operator `%s` at %s:%s'
115+ % (p [1 ], p .lineno (1 ), p .lexpos (1 )))
102116
103117 def p_jsonpath_root (self , p ):
104118 "jsonpath : '$'"
0 commit comments