11from collections .abc import MutableMapping , MutableSequence
2- from enum import Flag , IntFlag , auto
2+ from enum import IntFlag , auto
33from typing import Union , List , Any , Dict
44
5- import dpath .segments
6- from dpath import options
7- from dpath .exceptions import InvalidKeyName
5+ from dpath import options , segments
6+ from dpath .exceptions import InvalidKeyName , PathNotFound
87
98_DEFAULT_SENTINEL = object ()
109
@@ -29,28 +28,28 @@ def _split_path(path: str, separator: str) -> Union[List[IntAwareSegment], IntAw
2928 ignored, and is assumed to be part of each key glob. It will not be
3029 stripped.
3130 """
32- if not dpath . segments .leaf (path ):
33- segments = path
31+ if not segments .leaf (path ):
32+ split_segments = path
3433 else :
35- segments = path .lstrip (separator ).split (separator )
34+ split_segments = path .lstrip (separator ).split (separator )
3635
3736 # FIXME: This check was in the old internal library, but I can't
3837 # see a way it could fail...
39- for i , segment in enumerate (segments ):
38+ for i , segment in enumerate (split_segments ):
4039 if (separator and (separator in segment )):
4140 raise InvalidKeyName ("{} at {}[{}] contains the separator '{}'"
42- "" .format (segment , segments , i , separator ))
41+ "" .format (segment , split_segments , i , separator ))
4342
4443 # Attempt to convert integer segments into actual integers.
4544 final = []
46- for segment in segments :
45+ for segment in split_segments :
4746 try :
4847 final .append (int (segment ))
4948 except :
5049 final .append (segment )
51- segments = final
50+ split_segments = final
5251
53- return segments
52+ return split_segments
5453
5554
5655def new (obj , path , value , separator = '/' , creator = None ):
@@ -67,10 +66,10 @@ def new(obj, path, value, separator='/', creator=None):
6766 responsible for creating missing keys at arbitrary levels of
6867 the path (see the help for dpath.path.set)
6968 """
70- segments = _split_path (path , separator )
69+ split_segments = _split_path (path , separator )
7170 if creator :
72- return dpath . segments .set (obj , segments , value , creator = creator )
73- return dpath . segments .set (obj , segments , value )
71+ return segments .set (obj , split_segments , value , creator = creator )
72+ return segments .set (obj , split_segments , value )
7473
7574
7675def delete (obj , glob , separator = '/' , afilter = None ):
@@ -83,18 +82,18 @@ def delete(obj, glob, separator='/', afilter=None):
8382 globlist = _split_path (glob , separator )
8483
8584 def f (obj , pair , counter ):
86- (segments , value ) = pair
85+ (path_segments , value ) = pair
8786
8887 # Skip segments if they no longer exist in obj.
89- if not dpath . segments .has (obj , segments ):
88+ if not segments .has (obj , path_segments ):
9089 return
9190
92- matched = dpath . segments .match (segments , globlist )
93- selected = afilter and dpath . segments .leaf (value ) and afilter (value )
91+ matched = segments .match (path_segments , globlist )
92+ selected = afilter and segments .leaf (value ) and afilter (value )
9493
9594 if (matched and not afilter ) or selected :
96- key = segments [- 1 ]
97- parent = dpath . segments .get (obj , segments [:- 1 ])
95+ key = path_segments [- 1 ]
96+ parent = segments .get (obj , path_segments [:- 1 ])
9897
9998 try :
10099 # Attempt to treat parent like a sequence.
@@ -107,7 +106,7 @@ def f(obj, pair, counter):
107106 #
108107 # Note: In order to achieve proper behavior we are
109108 # relying on the reverse iteration of
110- # non-dictionaries from dpath. segments.kvs().
109+ # non-dictionaries from segments.kvs().
111110 # Otherwise we'd be unable to delete all the tails
112111 # of a list and end up with None values when we
113112 # don't need them.
@@ -123,9 +122,9 @@ def f(obj, pair, counter):
123122
124123 counter [0 ] += 1
125124
126- [deleted ] = dpath . segments .foldm (obj , f , [0 ])
125+ [deleted ] = segments .foldm (obj , f , [0 ])
127126 if not deleted :
128- raise dpath . exceptions . PathNotFound ("Could not find {0} to delete it" .format (glob ))
127+ raise PathNotFound ("Could not find {0} to delete it" .format (glob ))
129128
130129 return deleted
131130
@@ -138,20 +137,20 @@ def set(obj, glob, value, separator='/', afilter=None):
138137 globlist = _split_path (glob , separator )
139138
140139 def f (obj , pair , counter ):
141- (segments , found ) = pair
140+ (path_segments , found ) = pair
142141
143142 # Skip segments if they no longer exist in obj.
144- if not dpath . segments .has (obj , segments ):
143+ if not segments .has (obj , path_segments ):
145144 return
146145
147- matched = dpath . segments .match (segments , globlist )
148- selected = afilter and dpath . segments .leaf (found ) and afilter (found )
146+ matched = segments .match (path_segments , globlist )
147+ selected = afilter and segments .leaf (found ) and afilter (found )
149148
150149 if (matched and not afilter ) or (matched and selected ):
151- dpath . segments .set (obj , segments , value , creator = None )
150+ segments .set (obj , path_segments , value , creator = None )
152151 counter [0 ] += 1
153152
154- [changed ] = dpath . segments .foldm (obj , f , [0 ])
153+ [changed ] = segments .foldm (obj , f , [0 ])
155154 return changed
156155
157156
@@ -171,14 +170,14 @@ def get(obj: Dict, glob: str, separator="/", default: Any = _DEFAULT_SENTINEL) -
171170 globlist = _split_path (glob , separator )
172171
173172 def f (obj , pair , results ):
174- (segments , found ) = pair
173+ (path_segments , found ) = pair
175174
176- if dpath . segments .match (segments , globlist ):
175+ if segments .match (path_segments , globlist ):
177176 results .append (found )
178177 if len (results ) > 1 :
179178 return False
180179
181- results = dpath . segments .fold (obj , f , [])
180+ results = segments .fold (obj , f , [])
182181
183182 if len (results ) == 0 :
184183 if default is not _DEFAULT_SENTINEL :
@@ -213,33 +212,33 @@ def search(obj, glob, yielded=False, separator='/', afilter=None, dirs=True):
213212
214213 globlist = _split_path (glob , separator )
215214
216- def keeper (segments , found ):
215+ def keeper (path , found ):
217216 """
218217 Generalized test for use in both yielded and folded cases.
219218 Returns True if we want this result. Otherwise returns False.
220219 """
221- if not dirs and not dpath . segments .leaf (found ):
220+ if not dirs and not segments .leaf (found ):
222221 return False
223222
224- matched = dpath . segments .match (segments , globlist )
223+ matched = segments .match (path , globlist )
225224 selected = afilter and afilter (found )
226225
227226 return (matched and not afilter ) or (matched and selected )
228227
229228 if yielded :
230229 def yielder ():
231- for segments , found in dpath . segments .walk (obj ):
232- if keeper (segments , found ):
233- yield (separator .join (map (dpath . segments .int_str , segments )), found )
230+ for path , found in segments .walk (obj ):
231+ if keeper (path , found ):
232+ yield (separator .join (map (segments .int_str , path )), found )
234233 return yielder ()
235234 else :
236235 def f (obj , pair , result ):
237- (segments , found ) = pair
236+ (path , found ) = pair
238237
239- if keeper (segments , found ):
240- dpath . segments .set (result , segments , found , hints = dpath . segments .types (obj , segments ))
238+ if keeper (path , found ):
239+ segments .set (result , path , found , hints = segments .types (obj , path ))
241240
242- return dpath . segments .fold (obj , f , {})
241+ return segments .fold (obj , f , {})
243242
244243
245244def merge (dst , src , separator = '/' , afilter = None , flags = MergeType .ADDITIVE ):
@@ -293,43 +292,43 @@ def are_both_mutable(o1, o2):
293292 return False
294293
295294 def merger (dst , src , _segments = ()):
296- for key , found in dpath . segments .kvs (src ):
295+ for key , found in segments .kvs (src ):
297296 # Our current path in the source.
298- segments = _segments + (key ,)
297+ current_path = _segments + (key ,)
299298
300299 if len (key ) == 0 and not options .ALLOW_EMPTY_STRING_KEYS :
301300 raise InvalidKeyName ("Empty string keys not allowed without "
302301 "dpath.options.ALLOW_EMPTY_STRING_KEYS=True: "
303- "{}" .format (segments ))
302+ "{}" .format (current_path ))
304303
305304 # Validate src and dst types match.
306305 if flags & MergeType .TYPESAFE :
307- if dpath . segments .has (dst , segments ):
308- target = dpath . segments .get (dst , segments )
306+ if segments .has (dst , current_path ):
307+ target = segments .get (dst , current_path )
309308 tt = type (target )
310309 ft = type (found )
311310 if tt != ft :
312- path = separator .join (segments )
311+ path = separator .join (current_path )
313312 raise TypeError ("Cannot merge objects of type"
314313 "{0} and {1} at {2}"
315314 "" .format (tt , ft , path ))
316315
317316 # Path not present in destination, create it.
318- if not dpath . segments .has (dst , segments ):
319- dpath . segments .set (dst , segments , found )
317+ if not segments .has (dst , current_path ):
318+ segments .set (dst , current_path , found )
320319 continue
321320
322321 # Retrieve the value in the destination.
323- target = dpath . segments .get (dst , segments )
322+ target = segments .get (dst , current_path )
324323
325324 # If the types don't match, replace it.
326325 if ((type (found ) != type (target )) and (not are_both_mutable (found , target ))):
327- dpath . segments .set (dst , segments , found )
326+ segments .set (dst , current_path , found )
328327 continue
329328
330329 # If target is a leaf, the replace it.
331- if dpath . segments .leaf (target ):
332- dpath . segments .set (dst , segments , found )
330+ if segments .leaf (target ):
331+ segments .set (dst , current_path , found )
333332 continue
334333
335334 # At this point we know:
@@ -348,14 +347,14 @@ def merger(dst, src, _segments=()):
348347 try :
349348 target ['' ]
350349 except TypeError :
351- dpath . segments .set (dst , segments , found )
350+ segments .set (dst , current_path , found )
352351 continue
353352 except :
354353 raise
355354 except :
356355 # We have a dictionary like thing and we need to attempt to
357356 # recursively merge it.
358- merger (dst , found , segments )
357+ merger (dst , found , current_path )
359358
360359 merger (dst , filtered_src )
361360
0 commit comments