comparison hgsubversion/util.py @ 1134:a9b6e38d6dc9

util: add visitor pattern for scrubbing json These functions are for future patches that will add safer serialization via json. '_convert' is a visitor pattern that will be used for lists, dictionaries, and strings for helping convert None to the empty string since json forbids 'null' as a key for a dictionary. None -> '' is a safe mapping because this is for the 'branch_info' variable which already maps the empty string to None. Note, also, that json is chosen instead of, say, csv because json has a concept of 'null' and will better handle utf8 strings (which subversion supports). Important: this changes the requirement of hgsubversion to python 2.6+.
author Sean Farley <sean.michael.farley@gmail.com>
date Mon, 17 Feb 2014 11:10:38 -0600
parents 2922f4881a04
children 5a0da3bab8a2
comparison
equal deleted inserted replaced
1133:ff4e102932ed 1134:a9b6e38d6dc9
140 string = "" 140 string = ""
141 f = open(file_path, 'wb') 141 f = open(file_path, 'wb')
142 f.write(str(string)) 142 f.write(str(string))
143 f.close() 143 f.close()
144 144
145 def _scrub(data):
146 if not data and not isinstance(data, list):
147 return ''
148 return data
149
150 def _descrub(data):
151 if isinstance(data, list):
152 return tuple(data)
153 if data == '':
154 return None
155 return data
156
157 def _convert(input, visitor):
158 if isinstance(input, dict):
159 scrubbed = {}
160 d = dict([(_convert(key, visitor), _convert(value, visitor))
161 for key, value in input.iteritems()])
162 for key, val in d.iteritems():
163 scrubbed[visitor(key)] = visitor(val)
164 return scrubbed
165 elif isinstance(input, list):
166 return [_convert(element, visitor) for element in input]
167 elif isinstance(input, unicode):
168 return input.encode('utf-8')
169 return input
170
145 def dump(data, file_path): 171 def dump(data, file_path):
146 """pickle some data to a path atomically. 172 """pickle some data to a path atomically.
147 173
148 This is present because I kept corrupting my revmap by managing to hit ^C 174 This is present because I kept corrupting my revmap by managing to hit ^C
149 during the pickle of that file. 175 during the pickle of that file.