1- import re
1+ from nested_multipart_parser . lazy import lazy_regex_compile
22
33
44class InvalidFormat (Exception ):
@@ -16,7 +16,7 @@ def __new__(cls, cls_name, ns, childs):
1616 return super ().__new__ (cls , cls_name , ns , childs )
1717
1818
19- TOKEN_PARSER = ("[" , "]" , "." )
19+ INVALID_TOKEN_PARSER = ("[" , "]" , "." )
2020
2121
2222class NestedParserOptionsAbstract (metaclass = NestedParserOptionsType ):
@@ -25,7 +25,7 @@ def check(self, key, keys):
2525 raise InvalidFormat (key )
2626
2727 first = keys [0 ]
28- for token in TOKEN_PARSER :
28+ for token in INVALID_TOKEN_PARSER :
2929 if token in first :
3030 raise InvalidFormat (key )
3131
@@ -35,7 +35,7 @@ def check(self, key, keys):
3535 for c in key :
3636 if c .isspace ():
3737 raise InvalidFormat (key )
38-
38+
3939 def split (self , key ):
4040 contents = list (filter (None , self ._reg_spliter .split (key )))
4141 if not contents :
@@ -50,18 +50,10 @@ def split(self, key):
5050 return list (filter (None , lst ))
5151
5252
53- REGEX_SEPARATOR = {
54- "dot" : r"(\.[^\.]+)" ,
55- "bracket" : r"([^\[\]]+)" ,
56- "mixed" : r"(\[\d+\])|([^\[\]]+)" ,
57- "mixed-dot" : r"(\[\d+\])|(\.[^\[\]\.]+)" ,
58- }
59-
60-
6153class NestedParserOptionsDot (NestedParserOptionsAbstract ):
6254 def __init__ (self ):
63- self ._reg_spliter = re . compile (r"^([^\.]+)(.*?)(\.)?$" )
64- self ._reg_options = re . compile (r"(\.[^\.]+)" )
55+ self ._reg_spliter = lazy_regex_compile (r"^([^\.]+)(.*?)(\.)?$" )
56+ self ._reg_options = lazy_regex_compile (r"(\.[^\.]+)" )
6557
6658 def sanitize (self , key , value ):
6759 contents = self .split (key )
@@ -88,8 +80,8 @@ def sanitize(self, key, value):
8880
8981class NestedParserOptionsBracket (NestedParserOptionsAbstract ):
9082 def __init__ (self ):
91- self ._reg_spliter = re . compile (r"^([^\[\]]+)(.*?)(\[\])?$" )
92- self ._reg_options = re . compile (r"(\[[^\[\]]+\])" )
83+ self ._reg_spliter = lazy_regex_compile (r"^([^\[\]]+)(.*?)(\[\])?$" )
84+ self ._reg_options = lazy_regex_compile (r"(\[[^\[\]]+\])" )
9385
9486 def sanitize (self , key , value ):
9587 first , * lst = self .split (key )
@@ -117,8 +109,10 @@ def sanitize(self, key, value):
117109
118110class NestedParserOptionsMixedDot (NestedParserOptionsAbstract ):
119111 def __init__ (self ):
120- self ._reg_spliter = re .compile (r"^([^\[\]\.]+)(.*?)((?:\.)|(?:\[\]))?$" )
121- self ._reg_options = re .compile (r"(\[\d+\])|(\.[^\[\]\.]+)" )
112+ self ._reg_spliter = lazy_regex_compile (
113+ r"^([^\[\]\.]+)(.*?)((?:\.)|(?:\[\]))?$"
114+ )
115+ self ._reg_options = lazy_regex_compile (r"(\[\d+\])|(\.[^\[\]\.]+)" )
122116
123117 def sanitize (self , key , value ):
124118 first , * lst = self .split (key )
@@ -152,8 +146,10 @@ def sanitize(self, key, value):
152146
153147class NestedParserOptionsMixed (NestedParserOptionsMixedDot ):
154148 def __init__ (self ):
155- self ._reg_spliter = re .compile (r"^([^\[\]\.]+)(.*?)((?:\.)|(?:\[\]))?$" )
156- self ._reg_options = re .compile (r"(\[\d+\])|(\.?[^\[\]\.]+)" )
149+ self ._reg_spliter = lazy_regex_compile (
150+ r"^([^\[\]\.]+)(.*?)((?:\.)|(?:\[\]))?$"
151+ )
152+ self ._reg_options = lazy_regex_compile (r"(\[\d+\])|(\.?[^\[\]\.]+)" )
157153
158154 def sanitize (self , key , value ):
159155 first , * lst = self .split (key )
0 commit comments