| OLD | NEW |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import difflib | 5 import difflib |
| 6 import json | 6 import json |
| 7 import os | 7 import os |
| 8 import pprint | 8 import pprint |
| 9 | 9 |
| 10 from collections import OrderedDict | 10 from collections import OrderedDict |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 SUPPORTED_SERIALIZERS = {'json', 'yaml'} | 21 SUPPORTED_SERIALIZERS = {'json', 'yaml'} |
| 22 SERIALIZERS = {} | 22 SERIALIZERS = {} |
| 23 | 23 |
| 24 | 24 |
| 25 # JSON support | 25 # JSON support |
| 26 def re_encode(obj): | 26 def re_encode(obj): |
| 27 if isinstance(obj, dict): | 27 if isinstance(obj, dict): |
| 28 return {re_encode(k): re_encode(v) for k, v in obj.iteritems()} | 28 return {re_encode(k): re_encode(v) for k, v in obj.iteritems()} |
| 29 elif isinstance(obj, list): | 29 elif isinstance(obj, list): |
| 30 return [re_encode(i) for i in obj] | 30 return [re_encode(i) for i in obj] |
| 31 elif isinstance(obj, unicode): | 31 elif isinstance(obj, str): |
| 32 return obj.encode('utf-8') | 32 return obj.decode('utf-8') |
| 33 else: | 33 else: |
| 34 return obj | 34 return obj |
| 35 | 35 |
| 36 | 36 |
| 37 SERIALIZERS['json'] = ( | 37 SERIALIZERS['json'] = ( |
| 38 lambda s: re_encode(json.load(s)), | 38 lambda s: re_encode(json.load(s)), |
| 39 lambda data, stream: json.dump( | 39 lambda data, stream: json.dump( |
| 40 data, stream, sort_keys=True, indent=2, separators=(',', ': '))) | 40 data, stream, sort_keys=True, indent=2, separators=(',', ': '))) |
| 41 | 41 |
| 42 | 42 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 return new | 118 return new |
| 119 if old == new: | 119 if old == new: |
| 120 return None | 120 return None |
| 121 else: | 121 else: |
| 122 return list(difflib.unified_diff( | 122 return list(difflib.unified_diff( |
| 123 pprint.pformat(old).splitlines(), | 123 pprint.pformat(old).splitlines(), |
| 124 pprint.pformat(new).splitlines(), | 124 pprint.pformat(new).splitlines(), |
| 125 fromfile='expected', tofile='current', | 125 fromfile='expected', tofile='current', |
| 126 n=4, lineterm='' | 126 n=4, lineterm='' |
| 127 )) | 127 )) |
| OLD | NEW |