How to use the fire.console.encoding.Decode function in fire

To help you get started, we’ve selected a few fire examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github google / python-fire / fire / console / console_attr_os.py View on Github external
def _GetKeyChar():
      return encoding.Decode(os.read(fd, 1))
github google / python-fire / fire / console / console_attr_os.py View on Github external
def _GetKeyChar():
      return encoding.Decode(msvcrt.getch())
github google / python-fire / fire / console / console_attr.py View on Github external
def Decode(data, encoding=None):
  """Converts the given string, bytes, or object to a text string.

  Args:
    data: Any bytes, string, or object that has str() or unicode() methods.
    encoding: A suggesting encoding used to decode. If this encoding doesn't
      work, other defaults are tried. Defaults to
      GetConsoleAttr().GetEncoding().

  Returns:
    A text string representation of the data.
  """
  encoding = encoding or GetConsoleAttr().GetEncoding()
  return encoding_util.Decode(data, encoding=encoding)
github google / python-fire / fire / console / console_attr.py View on Github external
GetConsoleAttr().GetEncoding().
    escape: Replace unencodable characters with a \uXXXX or \xXX equivalent if
      True. Otherwise replace unencodable characters with an appropriate unknown
      character, '?' for ASCII, and the unicode unknown replacement character
      \uFFFE for unicode.

  Returns:
    A text string representation of the data, but modified to remove any
    characters that would result in an encoding exception with the target
    encoding. In the worst case, with escape=False, it will contain only ?
    characters.
  """
  if data is None:
    return 'None'
  encoding = encoding or GetConsoleAttr().GetEncoding()
  string = encoding_util.Decode(data, encoding=encoding)

  try:
    # No change needed if the string encodes to the output encoding.
    string.encode(encoding)
    return string
  except UnicodeError:
    # The string does not encode to the output encoding. Encode it with error
    # handling then convert it back into a text string (which will be
    # guaranteed to only contain characters that can be encoded later.
    return (string
            .encode(encoding, 'backslashreplace' if escape else 'replace')
            .decode(encoding))