Esempio n. 1
0
 def string(self, string, escape=True, attr=False):
     if escape and string:
         if not is_unicode(string):
             string = unicode(string)
         escaper = html_escape if not attr else html_attr_escape
         string = escaper(string)
     return self._strings.add(string)
Esempio n. 2
0
 def _normalize_message(self, msg):
     if callable(msg):
         return msg
     if not is_unicode(msg):
         msg = unic(msg)
     if '\r\n' in msg:
         msg = msg.replace('\r\n', '\n')
     return msg
Esempio n. 3
0
 def _get_type(self, name, value):
     if self._argspec.types is None or not is_unicode(value):
         return None, None
     if name in self._argspec.types:
         return self._argspec.types[name], True
     if name in self._argspec.defaults:
         return type(self._argspec.defaults[name]), False
     return None, None
 def _get_type(self, name, value):
     if self._argspec.types is None or not is_unicode(value):
         return None, None
     if name in self._argspec.types:
         return self._argspec.types[name], True
     if name in self._argspec.defaults:
         return type(self._argspec.defaults[name]), False
     return None, None
Esempio n. 5
0
 def _to_string(self, value):
     if is_unicode(value):
         return value
     if is_bytes(value):
         return value.decode('UTF-8')
     if is_tuple(value):
         return f"{value[0]}={value[1]}"
     raise DataError('Return value must be string.')
Esempio n. 6
0
    def should_be_unicode_string(self, item, msg=None):
        """Fails if the given ``item`` is not a Unicode string.

        Use `Should Be Byte String` if you want to verify the ``item`` is a
        byte string, or `Should Be String` if both Unicode and byte strings
        are fine. See `Should Be String` for more details about Unicode
        strings and byte strings.

        The default error message can be overridden with the optional
        ``msg`` argument.
        """
        if not is_unicode(item):
            self._fail(msg, "'%s' is not a Unicode string.", item)
Esempio n. 7
0
    def should_be_unicode_string(self, item, msg=None):
        """Fails if the given ``item`` is not a Unicode string.

        Use `Should Be Byte String` if you want to verify the ``item`` is a
        byte string, or `Should Be String` if both Unicode and byte strings
        are fine. See `Should Be String` for more details about Unicode
        strings and byte strings.

        The default error message can be overridden with the optional
        ``msg`` argument.
        """
        if not is_unicode(item):
            self._fail(msg, "'%s' is not a Unicode string.", item)
Esempio n. 8
0
    def decode_bytes_to_string(self, bytes, encoding, errors='strict'):
        """Decodes the given ``bytes`` to a Unicode string using the given ``encoding``.

        ``errors`` argument controls what to do if decoding some bytes fails.
        All values accepted by ``decode`` method in Python are valid, but in
        practice the following values are most useful:

        - ``strict``: fail if characters cannot be decoded (default)
        - ``ignore``: ignore characters that cannot be decoded
        - ``replace``: replace characters that cannot be decoded with
          a replacement character

        Examples:
        | ${string} = | Decode Bytes To String | ${bytes} | UTF-8 |
        | ${string} = | Decode Bytes To String | ${bytes} | ASCII | errors=ignore |

        Use `Encode String To Bytes` if you need to convert Unicode strings to
        byte strings, and `Convert To String` in ``BuiltIn`` if you need to
        convert arbitrary objects to Unicode strings.
        """
        if PY3 and is_unicode(bytes):
            raise TypeError('Can not decode strings on Python 3.')
        return bytes.decode(encoding, errors)
Esempio n. 9
0
    def decode_bytes_to_string(self, bytes, encoding, errors='strict'):
        """Decodes the given ``bytes`` to a Unicode string using the given ``encoding``.

        ``errors`` argument controls what to do if decoding some bytes fails.
        All values accepted by ``decode`` method in Python are valid, but in
        practice the following values are most useful:

        - ``strict``: fail if characters cannot be decoded (default)
        - ``ignore``: ignore characters that cannot be decoded
        - ``replace``: replace characters that cannot be decoded with
          a replacement character

        Examples:
        | ${string} = | Decode Bytes To String | ${bytes} | UTF-8 |
        | ${string} = | Decode Bytes To String | ${bytes} | ASCII | errors=ignore |

        Use `Encode String To Bytes` if you need to convert Unicode strings to
        byte strings, and `Convert To String` in ``BuiltIn`` if you need to
        convert arbitrary objects to Unicode strings.
        """
        if PY3 and is_unicode(bytes):
            raise TypeError('Can not decode strings on Python 3.')
        return bytes.decode(encoding, errors)
Esempio n. 10
0
 def _to_string(self, value):
     if not is_string(value):
         raise DataError('Return value must be string.')
     return value if is_unicode(value) else unic(value, 'UTF-8')
Esempio n. 11
0
 def _to_string(self, value):
     if is_unicode(value):
         return value
     if is_bytes(value):
         return value.decode('UTF-8')
     raise DataError('Return value must be string.')
Esempio n. 12
0
 def _to_string(self, value):
     if not is_string(value):
         raise DataError('Return value must be string.')
     return value if is_unicode(value) else unic(value, 'UTF-8')
 def string(self, string, escape=True, attr=False):
     if escape and string:
         if not is_unicode(string):
             string = unic(string)
         string = (html_escape if not attr else attribute_escape)(string)
     return self._strings.add(string)
Esempio n. 14
0
 def _to_string(self, value):
     if is_unicode(value):
         return value
     if is_bytes(value):
         return value.decode('UTF-8')
     raise DataError('Return value must be string.')
Esempio n. 15
0
 def string(self, string, escape=True, attr=False):
     if escape and string:
         if not is_unicode(string):
             string = unic(string)
         string = (html_escape if not attr else attribute_escape)(string)
     return self._strings.add(string)