Robot Framework Integrated Development Environment (RIDE)
unic.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 import sys
17 from pprint import PrettyPrinter
18 from unicodedata import normalize
19 
20 from .platform import PY2, PY3
21 from .robottypes import is_bytes, is_unicode, unicode
22 
23 
24 def unic(item):
25  item = _unic(item)
26  try:
27  return normalize('NFC', item)
28  except ValueError:
29  # https://github.com/IronLanguages/ironpython2/issues/628
30  return item
31 
32 
33 if PY2:
34 
35  def _unic(item):
36  if isinstance(item, unicode):
37  return item
38  if isinstance(item, (bytes, bytearray)):
39  try:
40  return item.decode('ASCII')
41  except UnicodeError:
42  return u''.join(chr(b) if b < 128 else '\\x%x' % b
43  for b in bytearray(item))
44  try:
45  try:
46  return unicode(item)
47  except UnicodeError:
48  return unic(str(item))
49  except:
50  return _unrepresentable_object(item)
51 
52 else:
53 
54  def _unic(item):
55  if isinstance(item, str):
56  return item
57  if isinstance(item, (bytes, bytearray)):
58  try:
59  return item.decode('ASCII')
60  except UnicodeError:
61  return ''.join(chr(b) if b < 128 else '\\x%x' % b
62  for b in item)
63  try:
64  return str(item)
65  except:
66  return _unrepresentable_object(item)
67 
68 
69 def prepr(item, width=80):
70  return unic(PrettyRepr(width=width).pformat(item))
71 
72 
73 class PrettyRepr(PrettyPrinter):
74 
75  def format(self, object, context, maxlevels, level):
76  try:
77  if is_unicode(object):
78  return repr(object).lstrip('u'), True, False
79  if is_bytes(object):
80  return 'b' + repr(object).lstrip('b'), True, False
81  return PrettyPrinter.format(self, object, context, maxlevels, level)
82  except:
83  return _unrepresentable_object(object), True, False
84 
85  if PY3:
86 
87  # Don't split strings: https://stackoverflow.com/questions/31485402
88  def _format(self, object, *args, **kwargs):
89  if isinstance(object, (str, bytes, bytearray)):
90  width = self._width_width
91  self._width_width = sys.maxsize
92  try:
93  super()._format(object, *args, **kwargs)
94  finally:
95  self._width_width = width
96  else:
97  super()._format(object, *args, **kwargs)
98 
99 
101  from .error import get_error_message
102  return u"<Unrepresentable object %s. Error: %s>" \
103  % (item.__class__.__name__, get_error_message())
def format(self, object, context, maxlevels, level)
Definition: unic.py:75
def _format(self, object, *args, **kwargs)
Definition: unic.py:88
unicode
Exceptions and return codes used internally.
Definition: errors.py:24
def get_error_message()
Returns error message of the last occurred exception.
Definition: error.py:41
def normalize(string, ignore=(), caseless=True, spaceless=True)
Normalizes given string according to given spec.
Definition: normalizing.py:30
def _unrepresentable_object(item)
Definition: unic.py:100
def prepr(item, width=80)
Definition: unic.py:69