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
=== modified file 'utah/client/common.py'
--- utah/client/common.py 2012-11-07 21:54:57 +0000
+++ utah/client/common.py 2012-12-03 14:21:27 +0000
@@ -97,6 +97,9 @@
9797
98 try:98 try:
99 stdout, stderr = p.communicate()99 stdout, stderr = p.communicate()
100 stdout = normalize_encoding(stdout)
101 stderr = normalize_encoding(stderr)
102
100 if timeout != 0:103 if timeout != 0:
101 signal.alarm(0)104 signal.alarm(0)
102 except TimeoutAlarm:105 except TimeoutAlarm:
@@ -135,6 +138,22 @@
135 )138 )
136139
137140
141def normalize_encoding(value, encoding='utf-8'):
142 """Normalize string encoding.
143
144 Make sure that byte strings are used only for ascii data and unicode
145 strings for strings using any other encoding.
146
147 """
148 unicode_value = value.decode(encoding)
149 try:
150 output = unicode_value.encode('ascii')
151 except UnicodeEncodeError:
152 output = unicode_value
153
154 return output
155
156
138# TODO: it might make sense to have a result object that keeps track of157# TODO: it might make sense to have a result object that keeps track of
139# test pass, fail, error and serializes it's data.158# test pass, fail, error and serializes it's data.
140def make_result(command, retcode, stdout='', stderr='', start_time='',159def make_result(command, retcode, stdout='', stderr='', start_time='',
141160
=== modified file 'utah/client/result.py'
--- utah/client/result.py 2012-11-07 18:01:26 +0000
+++ utah/client/result.py 2012-12-03 14:21:27 +0000
@@ -197,12 +197,14 @@
197197
198# Trick to get strings printed as literal blocks198# Trick to get strings printed as literal blocks
199# inspired by: http://stackoverflow.com/a/7445560/183066199# inspired by: http://stackoverflow.com/a/7445560/183066
200class LiteralString(str):200class LiteralString(object):
201 pass201 def __init__(self, str_data):
202 self.str_data = str_data
202203
203204
204def literal_block(dumper, data):205def literal_block(dumper, data):
205 return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|')206 return dumper.represent_scalar('tag:yaml.org,2002:str',
207 data.str_data, style='|')
206yaml.add_representer(LiteralString, literal_block)208yaml.add_representer(LiteralString, literal_block)
207209
208210
@@ -211,7 +213,7 @@
211 """213 """
212 Transform long strings into literal blocks214 Transform long strings into literal blocks
213 """215 """
214 if isinstance(data, str) and '\n' in data:216 if isinstance(data, basestring) and '\n' in data:
215 # Remove trailing whitespace to serialize in yaml217 # Remove trailing whitespace to serialize in yaml
216 # as a literal string218 # as a literal string
217 lines = [line.rstrip() for line in data.splitlines()]219 lines = [line.rstrip() for line in data.splitlines()]
@@ -228,8 +230,10 @@
228 def result(self, verbose=False):230 def result(self, verbose=False):
229 if self.results:231 if self.results:
230 data = self._literalize(self.payload())232 data = self._literalize(self.payload())
231 yaml.dump(data, sys.stdout, explicit_start='---',233 yaml.dump(data, sys.stdout,
232 default_flow_style=False)234 explicit_start='---',
235 default_flow_style=False,
236 allow_unicode=True)
233237
234 status = self.status238 status = self.status
235 self.clear_results()239 self.clear_results()

Subscribers

People subscribed via source and target branches