Merge lp:~javier.collado/utah/bug1071385 into lp:utah

Proposed by Javier Collado
Status: Merged
Approved by: Javier Collado
Approved revision: 768
Merged at revision: 767
Proposed branch: lp:~javier.collado/utah/bug1071385
Merge into: lp:utah
Diff against target: 80 lines (+29/-6)
2 files modified
utah/client/common.py (+19/-0)
utah/client/result.py (+10/-6)
To merge this branch: bzr merge lp:~javier.collado/utah/bug1071385
Reviewer Review Type Date Requested Status
Joe Talbott (community) Approve
Review via email: mp+137573@code.launchpad.net

Description of the change

This change makes sure that unicode is used for the output from shell commands
when it contains a non-ascii character.

Also, it takes care of representing long unicode strings as literal blocks (the
current behaviour for byte strings).

Using the same example as in the bug, I get the following the expected output
for different locales:

- German
stderr: |-
  fail://example.com: Nicht unterstütztes Schema »fail«.

- Spanish:
stderr: |-
  fail://example.com: Esquema “fail” no soportado.

- Chinese
stderr: |-
  fail://example.com: 不支持的协议类型 “fail”.

- English:
stderr: |-
  fail://example.com: Unsupported scheme `fail'.

To post a comment you must log in.
Revision history for this message
Joe Talbott (joetalbott) wrote :

Looks good, thanks.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'utah/client/common.py'
2--- utah/client/common.py 2012-11-07 21:54:57 +0000
3+++ utah/client/common.py 2012-12-03 14:21:27 +0000
4@@ -97,6 +97,9 @@
5
6 try:
7 stdout, stderr = p.communicate()
8+ stdout = normalize_encoding(stdout)
9+ stderr = normalize_encoding(stderr)
10+
11 if timeout != 0:
12 signal.alarm(0)
13 except TimeoutAlarm:
14@@ -135,6 +138,22 @@
15 )
16
17
18+def normalize_encoding(value, encoding='utf-8'):
19+ """Normalize string encoding.
20+
21+ Make sure that byte strings are used only for ascii data and unicode
22+ strings for strings using any other encoding.
23+
24+ """
25+ unicode_value = value.decode(encoding)
26+ try:
27+ output = unicode_value.encode('ascii')
28+ except UnicodeEncodeError:
29+ output = unicode_value
30+
31+ return output
32+
33+
34 # TODO: it might make sense to have a result object that keeps track of
35 # test pass, fail, error and serializes it's data.
36 def make_result(command, retcode, stdout='', stderr='', start_time='',
37
38=== modified file 'utah/client/result.py'
39--- utah/client/result.py 2012-11-07 18:01:26 +0000
40+++ utah/client/result.py 2012-12-03 14:21:27 +0000
41@@ -197,12 +197,14 @@
42
43 # Trick to get strings printed as literal blocks
44 # inspired by: http://stackoverflow.com/a/7445560/183066
45-class LiteralString(str):
46- pass
47+class LiteralString(object):
48+ def __init__(self, str_data):
49+ self.str_data = str_data
50
51
52 def literal_block(dumper, data):
53- return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|')
54+ return dumper.represent_scalar('tag:yaml.org,2002:str',
55+ data.str_data, style='|')
56 yaml.add_representer(LiteralString, literal_block)
57
58
59@@ -211,7 +213,7 @@
60 """
61 Transform long strings into literal blocks
62 """
63- if isinstance(data, str) and '\n' in data:
64+ if isinstance(data, basestring) and '\n' in data:
65 # Remove trailing whitespace to serialize in yaml
66 # as a literal string
67 lines = [line.rstrip() for line in data.splitlines()]
68@@ -228,8 +230,10 @@
69 def result(self, verbose=False):
70 if self.results:
71 data = self._literalize(self.payload())
72- yaml.dump(data, sys.stdout, explicit_start='---',
73- default_flow_style=False)
74+ yaml.dump(data, sys.stdout,
75+ explicit_start='---',
76+ default_flow_style=False,
77+ allow_unicode=True)
78
79 status = self.status
80 self.clear_results()

Subscribers

People subscribed via source and target branches