Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(86)

Side by Side Diff: recipe_engine/third_party/expect_tests/serialize.py

Issue 2088343002: improve unicode support (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/recipes-py@master
Patch Set: Copyright Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « recipe_engine/stream.py ('k') | recipes/engine_tests/unicode.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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 ))
OLDNEW
« no previous file with comments | « recipe_engine/stream.py ('k') | recipes/engine_tests/unicode.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698