1010from collections import OrderedDict
1111from copy import deepcopy
1212
13- __all__ = [' JsonStore' ]
13+ __all__ = [" JsonStore" ]
1414
1515
1616class JsonStore (object ):
1717 """A class to provide object based access to a JSON file"""
18+
1819 def __enter__ (self ):
19- current_state = self .__dict__ [' _data' ]
20- self .__dict__ [' _states' ].append (current_state )
21- self .__dict__ [' _data' ] = deepcopy (current_state )
20+ current_state = self .__dict__ [" _data" ]
21+ self .__dict__ [" _states" ].append (current_state )
22+ self .__dict__ [" _data" ] = deepcopy (current_state )
2223 return self
2324
2425 def __exit__ (self , * args ):
25- previous_state = self .__dict__ [' _states' ].pop ()
26+ previous_state = self .__dict__ [" _states" ].pop ()
2627 if any (args ):
27- self .__dict__ [' _data' ] = previous_state
28- elif not self .__dict__ [' _states' ]:
28+ self .__dict__ [" _data" ] = previous_state
29+ elif not self .__dict__ [" _states" ]:
2930 self ._save ()
3031
3132 def _do_auto_commit (self ):
32- if self ._auto_commit and not self .__dict__ [' _states' ]:
33+ if self ._auto_commit and not self .__dict__ [" _states" ]:
3334 self ._save ()
3435
3536 def _load (self ):
3637 if not os .path .exists (self ._path ):
37- with open (self ._path , ' w+b' ) as store :
38- store .write ('{}' .encode (' utf-8' ))
39- with open (self ._path , ' r+b' ) as store :
40- raw_data = store .read ().decode (' utf-8' )
38+ with open (self ._path , " w+b" ) as store :
39+ store .write ("{}" .encode (" utf-8" ))
40+ with open (self ._path , " r+b" ) as store :
41+ raw_data = store .read ().decode (" utf-8" )
4142 if not raw_data :
4243 data = OrderedDict ()
4344 else :
4445 data = json .loads (raw_data , object_pairs_hook = OrderedDict )
4546
4647 if not isinstance (data , dict ):
4748 raise ValueError ("Root element is not an object" )
48- self .__dict__ [' _data' ] = data
49+ self .__dict__ [" _data" ] = data
4950
5051 def _save (self ):
51- temp = self ._path + '~'
52- with open (temp , 'wb' ) as store :
53- output = json .dumps (
54- self ._data ,
55- indent = self ._indent ,
56- )
57- store .write (output .encode ('utf-8' ))
52+ temp = self ._path + "~"
53+ with open (temp , "wb" ) as store :
54+ output = json .dumps (self ._data , indent = self ._indent )
55+ store .write (output .encode ("utf-8" ))
5856 os .rename (temp , self ._path )
5957
6058 def __init__ (self , path , indent = 2 , auto_commit = True ):
61- self .__dict__ .update ({
62- '_auto_commit' : auto_commit ,
63- '_data' : None ,
64- '_path' : path ,
65- '_indent' : indent ,
66- '_states' : [],
67- })
59+ self .__dict__ .update (
60+ {
61+ "_auto_commit" : auto_commit ,
62+ "_data" : None ,
63+ "_path" : path ,
64+ "_indent" : indent ,
65+ "_states" : [],
66+ }
67+ )
6868 self ._load ()
6969
7070 def __getattr__ (self , key ):
@@ -90,7 +90,7 @@ def _valid_object(cls, obj, parents=None):
9090 return all (
9191 cls ._valid_string (k ) and cls ._valid_object (v , parents )
9292 for k , v in obj .items ()
93- )
93+ )
9494 elif isinstance (obj , (list , tuple )):
9595 return all (cls ._valid_object (o , parents ) for o in obj )
9696 else :
@@ -100,7 +100,7 @@ def _valid_object(cls, obj, parents=None):
100100 def _valid_value (cls , value ):
101101 if isinstance (value , (bool , int , float , type (None ))):
102102 return True
103- elif sys .version_info < (3 , ) and isinstance (value , long ):
103+ elif sys .version_info < (3 ,) and isinstance (value , long ):
104104 return True
105105 else :
106106 return cls ._valid_string (value )
@@ -109,7 +109,7 @@ def _valid_value(cls, value):
109109 def _valid_string (cls , value ):
110110 if isinstance (value , str ):
111111 return True
112- elif sys .version_info < (3 , ):
112+ elif sys .version_info < (3 ,):
113113 return isinstance (value , unicode )
114114 else :
115115 return False
@@ -127,7 +127,7 @@ def __get_obj(self, full_path):
127127 """
128128 Returns the object which is under the given path
129129 """
130- steps = full_path .split ('.' )
130+ steps = full_path .split ("." )
131131 path = []
132132 obj = self ._data
133133 if not full_path :
@@ -137,11 +137,11 @@ def __get_obj(self, full_path):
137137 try :
138138 obj = obj [step ]
139139 except KeyError :
140- raise KeyError ('.' .join (path ))
140+ raise KeyError ("." .join (path ))
141141 return obj
142142
143143 def __setitem__ (self , name , value ):
144- path , _ , key = name .rpartition ('.' )
144+ path , _ , key = name .rpartition ("." )
145145 if self ._valid_object (value ):
146146 dictionary = self .__get_obj (path )
147147 dictionary [key ] = deepcopy (value )
@@ -156,7 +156,7 @@ def __getitem__(self, key):
156156 return deepcopy (obj )
157157
158158 def __delitem__ (self , name ):
159- path , _ , key = name .rpartition ('.' )
159+ path , _ , key = name .rpartition ("." )
160160 obj = self .__get_obj (path )
161161 del obj [key ]
162162
0 commit comments