Skip to content

Commit 5ed6fb0

Browse files
committed
Rework work with unicode in exceptions and data model
1 parent ee80368 commit 5ed6fb0

File tree

5 files changed

+39
-31
lines changed

5 files changed

+39
-31
lines changed

exec_helpers/_ssh_client_base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -350,19 +350,19 @@ def is_alive(self): # type: () -> bool
350350

351351
def __repr__(self): # type: () -> str
352352
"""Representation for debug purposes."""
353-
return "{cls}(host={self.hostname}, port={self.port}, auth={self.auth!r})".format(
353+
return "{cls}(host={self.hostname}, port={self.port}, auth={self.auth!r})".format( # type: ignore
354354
cls=self.__class__.__name__, self=self
355-
)
355+
).encode('utf-8')
356356

357357
def __unicode__(self): # type: () -> typing.Text # pragma: no cover
358358
"""Representation for debug purposes."""
359359
return "{cls}(host={self.hostname}, port={self.port}) for user {username}".format(
360360
cls=self.__class__.__name__, self=self, username=self.auth.username
361361
)
362362

363-
def __str__(self): # type: ignore # pragma: no cover
363+
def __str__(self): # type: () -> str # pragma: no cover
364364
"""Representation for debug purposes."""
365-
return self.__unicode__().encode("utf-8")
365+
return self.__unicode__().encode("utf-8") # type: ignore
366366

367367
@property
368368
def _ssh(self): # type: () -> paramiko.SSHClient

exec_helpers/exceptions.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,13 @@ def __init__(self, result, timeout): # type: (exec_result.ExecResult, typing.Un
109109
:param timeout: timeout for command
110110
:type timeout: typing.Union[int, float]
111111
"""
112-
stdout_brief = result.stdout_brief.encode(encoding="utf-8", errors="backslashreplace").decode("utf-8")
113-
stderr_brief = result.stderr_brief.encode(encoding="utf-8", errors="backslashreplace").decode("utf-8")
114112
message = (
115113
"Wait for {result.cmd!r} during {timeout!s}s: no return code and no response on SIGTERM + SIGKILL signals!"
116114
"\n"
117115
"\tSTDOUT:\n"
118-
"{stdout_brief}\n"
116+
"{result.stdout_brief!r}\n"
119117
"\tSTDERR:\n"
120-
"{stderr_brief}".format(
121-
result=result, timeout=timeout, stdout_brief=stdout_brief, stderr_brief=stderr_brief
122-
)
118+
"{result.stderr_brief!r}".format(result=result, timeout=timeout)
123119
)
124120
super(ExecHelperNoKillError, self).__init__(message, result=result, timeout=timeout)
125121

@@ -128,14 +124,12 @@ def make_timeout_error_message(
128124
result, timeout
129125
): # type: (exec_result.ExecResult, typing.Union[int, float]) -> typing.Text
130126
"""Make timeout failed message."""
131-
stdout_brief = result.stdout_brief.encode(encoding="utf-8", errors="backslashreplace").decode("utf-8")
132-
stderr_brief = result.stderr_brief.encode(encoding="utf-8", errors="backslashreplace").decode("utf-8")
133127
return (
134128
"Wait for {result.cmd!r} during {timeout!s}s: no return code!\n"
135129
"\tSTDOUT:\n"
136-
"{stdout_brief}\n"
130+
"{result.stdout_brief!r}\n"
137131
"\tSTDERR:\n"
138-
"{stderr_brief}".format(result=result, timeout=timeout, stdout_brief=stdout_brief, stderr_brief=stderr_brief)
132+
"{result.stderr_brief!r}".format(result=result, timeout=timeout)
139133
)
140134

141135

@@ -183,17 +177,13 @@ def __init__(
183177
"""
184178
self.result = result
185179
self.expected = proc_enums.exit_codes_to_enums(expected)
186-
stdout_brief = result.stdout_brief.encode(encoding="utf-8", errors="backslashreplace").decode("utf-8")
187-
stderr_brief = result.stderr_brief.encode(encoding="utf-8", errors="backslashreplace").decode("utf-8")
188180
message = (
189181
"Command {result.cmd!r} returned exit code {result.exit_code} "
190182
"while expected {expected}\n"
191183
"\tSTDOUT:\n"
192-
"{stdout_brief}\n"
184+
"{result.stdout_brief!r}\n"
193185
"\tSTDERR:\n"
194-
"{stderr_brief}".format(
195-
result=self.result, expected=self.expected, stdout_brief=stdout_brief, stderr_brief=stderr_brief
196-
)
186+
"{result.stderr_brief!r}".format(result=result, expected=self.expected)
197187
)
198188
super(CalledProcessError, self).__init__(message)
199189

exec_helpers/exec_result.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,11 @@ def __unicode__(self): # type: () -> typing.Text # pragma: no cover
122122
"""Get string for debug purposes."""
123123
return self[:]
124124

125-
def __repr__(self): # type: () -> typing.Text
125+
def __repr__(self): # type: () -> str
126126
"""Repr for debug purposes."""
127-
return "{cls}(data={self._data!r})".format(cls=self.__class__.__name__, self=self)
127+
return (
128+
"{cls}(data={self._data!r})".format(cls=self.__class__.__name__, self=self).encode('utf-8') # type: ignore
129+
)
128130

129131

130132
class ExecResult(object):
@@ -600,11 +602,11 @@ def __repr__(self): # type: () -> str
600602
else:
601603
started = ""
602604
return (
603-
"{cls}(cmd={self.cmd!r}, stdout={self.stdout}, stderr={self.stderr}, "
605+
"{cls}(cmd={self.cmd!r}, stdout={self.stdout}, stderr={self.stderr}, " # type: ignore
604606
"exit_code={self.exit_code!s}{started},)".format(cls=self.__class__.__name__, self=self, started=started)
605-
)
607+
).encode('utf-8')
606608

607-
def __str__(self): # type: () -> str
609+
def __unicode__(self): # type: () -> typing.Text
608610
"""Representation for logging."""
609611
if self.started:
610612
started = "\tstarted={started},\n".format(started=self.started.strftime("%Y-%m-%d %H:%M:%S"))
@@ -634,6 +636,10 @@ def __str__(self): # type: () -> str
634636
)
635637
)
636638

639+
def __str__(self): # type: () -> str # pragma: no cover
640+
"""Representation for debug purposes."""
641+
return self.__unicode__().encode("utf-8") # type: ignore
642+
637643
def __eq__(self, other): # type: (typing.Any) -> bool
638644
"""Comparision."""
639645
return (

exec_helpers/proc_enums.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,16 @@ class SigNum(int, enum.Enum):
7070
SIGPWR = 30 # Power failure restart (System V).
7171
SIGSYS = 31 # Bad system call.
7272

73-
def __str__(self): # type: () -> str # pragma: no cover
73+
def __unicode__(self): # type: () -> typing.Text # pragma: no cover
7474
"""Representation for logs."""
7575
return "{self.name}<{self.value:d}(0x{self.value:02X})>".format( # pylint: disable=missing-format-attribute
7676
self=self
7777
)
7878

79+
def __str__(self): # type: () -> str # pragma: no cover
80+
"""Representation for debug purposes."""
81+
return self.__unicode__().encode("utf-8") # type: ignore
82+
7983

8084
if six.PY3: # pragma: no cover
8185
digit_type = int
@@ -181,12 +185,16 @@ class ExitCodes(digit_type, enum.Enum):
181185
SH_EX_SIGPWR = 128 + SigNum.SIGPWR
182186
SH_EX_SIGSYS = 128 + SigNum.SIGSYS
183187

184-
def __str__(self): # type: () -> str
188+
def __unicode__(self): # type: () -> typing.Text
185189
"""Representation for logs."""
186190
return "{self.name}<{self.value:d}(0x{self.value:02X})>".format( # pylint: disable=missing-format-attribute
187191
self=self
188192
)
189193

194+
def __str__(self): # type: () -> str # pragma: no cover
195+
"""Representation for debug purposes."""
196+
return self.__unicode__().encode("utf-8") # type: ignore
197+
190198

191199
EXPECTED = 0 if "win32" == sys.platform else ExitCodes.EX_OK
192200
INVALID = 0xDEADBEEF if "win32" == sys.platform else ExitCodes.EX_INVALID

exec_helpers/ssh_auth.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def __copy__(self): # type: () -> SSHAuth
219219
"""Copy self."""
220220
return self.__class__(username=self.username, password=self.__password, key=self.__key, keys=self.__keys)
221221

222-
def __repr__(self): # type: (...) -> str
222+
def __repr__(self): # type: () -> str
223223
"""Representation for debug purposes."""
224224
_key = None if self.__key is None else "<private for pub: {}>".format(self.public_key)
225225
_keys = [] # type: typing.List[typing.Union[str, None]]
@@ -230,16 +230,20 @@ def __repr__(self): # type: (...) -> str
230230
_keys.append("<private for pub: {}>".format(self.__get_public_key(key=k)) if k is not None else None)
231231

232232
return (
233-
"{cls}("
233+
"{cls}(" # type: ignore
234234
"username={self.username!r}, "
235235
"password=<*masked*>, "
236236
"key={key}, "
237237
"keys={keys}, "
238238
"key_filename={self.key_filename!r}, "
239239
"passphrase=<*masked*>,"
240240
")".format(cls=self.__class__.__name__, self=self, key=_key, keys=_keys)
241-
)
241+
).encode('utf-8')
242242

243-
def __str__(self): # type: (...) -> str
243+
def __unicode__(self): # type: () -> typing.Text
244244
"""Representation for debug purposes."""
245245
return "{cls} for {self.username}".format(cls=self.__class__.__name__, self=self)
246+
247+
def __str__(self): # type: () -> str
248+
"""Representation for debug purposes."""
249+
return self.__unicode__().encode('utf-8') # type: ignore

0 commit comments

Comments
 (0)