ALChacks.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # vim:set fileencoding=utf-8 et ts=4 sts=4 sw=4:
  2. #
  3. # apt-listchanges - Show changelog entries between the installed versions
  4. # of a set of packages and the versions contained in
  5. # corresponding .deb files
  6. #
  7. # Copyright (C) 2007 Pierre Habouzit <madcoder@debian.org>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public
  20. # License along with this program; if not, write to the Free
  21. # Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. # MA 02111-1307 USA
  23. #
  24. import gettext
  25. import locale
  26. import os
  27. import sys
  28. try:
  29. locale.setlocale(locale.LC_ALL, '')
  30. except locale.Error:
  31. sys.stderr.write("apt-listchanges: Can't set locale; make sure $LC_* and $LANG are correct!\n")
  32. os.environ['LC_ALL'] = 'C'
  33. locale.setlocale(locale.LC_ALL, 'C')
  34. # set the i18n dirs
  35. gettext.bindtextdomain("apt-listchanges", "/usr/share/locale")
  36. gettext.textdomain("apt-listchanges")
  37. def _(x):
  38. try:
  39. return gettext.gettext(x)
  40. except:
  41. return x
  42. # Utility classes for encoding conversions
  43. class _base_encoding(object):
  44. def __init__(self, encoding):
  45. self._encoding = encoding;
  46. def get_encoding(self):
  47. return self._encoding
  48. def to_bytes(self, text):
  49. return text.encode(self._encoding, 'replace')
  50. def from_bytes(self, bintext):
  51. return bintext.decode(self._encoding, 'replace')
  52. def as_string(self, text):
  53. if self._encoding == 'utf-8':
  54. return text
  55. return self.from_bytes(self.to_bytes(text))
  56. class utf8_encoding(_base_encoding):
  57. def __init__(self):
  58. _base_encoding.__init__(self, 'utf-8')
  59. class system_encoding(_base_encoding):
  60. _sysencoding = locale.getpreferredencoding(False).lower()
  61. def __init__(self):
  62. _base_encoding.__init__(self, self._sysencoding)
  63. # forcibly export _
  64. __all__ = ['_']