lib.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import sys
  2. import locale
  3. def to_utf8(s):
  4. """Re-encode string from the default system encoding to UTF-8."""
  5. current = locale.getpreferredencoding()
  6. return s.decode(current).encode("UTF-8") if s and current != "UTF-8" else s
  7. def debug(obj, fd=sys.stderr):
  8. """Write obj to standard error."""
  9. string = str(obj.encode(get_encoding(fd), "backslashreplace")
  10. if isinstance(obj, unicode) else obj)
  11. fd.write(string + "\n")
  12. def catch_exceptions(exit_codes, fun, *args, **kwargs):
  13. """
  14. Catch exceptions on fun(*args, **kwargs) and return the exit code specified
  15. in the exit_codes dictionary. Return 0 if no exception is raised.
  16. """
  17. try:
  18. fun(*args, **kwargs)
  19. return 0
  20. except tuple(exit_codes.keys()) as exc:
  21. debug("[%s] %s" % (exc.__class__.__name__, exc))
  22. return exit_codes[exc.__class__]
  23. def get_encoding(fd):
  24. """Guess terminal encoding."""
  25. return fd.encoding or locale.getpreferredencoding()
  26. def first(it):
  27. """Return first element in iterable."""
  28. return it.next()
  29. def string_to_dict(string):
  30. """Return dictionary from string "key1=value1, key2=value2"."""
  31. pairs = [s.strip() for s in (string or "").split(",")]
  32. return dict(pair.split("=") for pair in pairs)