diff options
author | 2011-01-16 22:52:52 -0800 | |
---|---|---|
committer | 2011-01-16 22:52:52 -0800 | |
commit | 9ba50e35e8ff5f8d1afcfdf101d2eca3a899cdc7 (patch) | |
tree | bfbe06d707414cc24e6770146a1b52f599d61644 | |
parent | add a linefeed to the hint message for better output separation (diff) | |
download | overlord-9ba50e35e8ff5f8d1afcfdf101d2eca3a899cdc7.tar.gz overlord-9ba50e35e8ff5f8d1afcfdf101d2eca3a899cdc7.tar.bz2 overlord-9ba50e35e8ff5f8d1afcfdf101d2eca3a899cdc7.zip |
move utility functions out of the Overlay class
-rw-r--r-- | layman/overlays/overlay.py | 47 | ||||
-rw-r--r-- | layman/utils.py | 54 |
2 files changed, 60 insertions, 41 deletions
diff --git a/layman/overlays/overlay.py b/layman/overlays/overlay.py index 6f39ae8..9e41fe9 100644 --- a/layman/overlays/overlay.py +++ b/layman/overlays/overlay.py @@ -33,8 +33,8 @@ import codecs import locale import xml.etree.ElementTree as ET # Python 2.5 -from layman.utils import ensure_unicode - +from layman.utils import (pad, terminal_width, get_encoding, encoder, + ensure_unicode) #from layman.debug import OUT from layman.overlays.bzr import BzrOverlay @@ -103,7 +103,8 @@ class Overlay(object): ''' self.output = config['output'] - + self._encoding_ = get_encoding(self.output) + def strip_text(node): res = node.text if res is None: @@ -294,16 +295,6 @@ class Overlay(object): assert len(self.sources) == 1 return self.sources[0].delete(base) - def _get_encoding(self): - if hasattr(sys.stdout, 'encoding') \ - and sys.stdout.encoding != None: - return sys.stdout.encoding - else: - return locale.getpreferredencoding() - - def _encode(self, unicode_text): - return codecs.encode(unicode_text, self._get_encoding(), 'replace') - def __str__(self): ''' >>> here = os.path.dirname(os.path.realpath(__file__)) @@ -369,7 +360,7 @@ class Overlay(object): result += u'\n %s' % i result += u'\n' - return self._encode(result) + return encoder(result, self._encoding_) def short_list(self, width = 0): ''' @@ -382,32 +373,6 @@ class Overlay(object): wrobel [Subversion] (https://o.g.o/svn/dev/wrobel ) ''' - def pad(string, length): - '''Pad a string with spaces.''' - if len(string) <= length: - return string + ' ' * (length - len(string)) - else: - return string[:length - 3] + '...' - - def terminal_width(): - '''Determine width of terminal window.''' - try: - width = int(os.environ['COLUMNS']) - if width > 0: - return width - except: - pass - try: - import struct, fcntl, termios - query = struct.pack('HHHH', 0, 0, 0, 0) - response = fcntl.ioctl(1, termios.TIOCGWINSZ, query) - width = struct.unpack('HHHH', response)[1] - if width > 0: - return width - except: - pass - return 80 - name = pad(self.name, 25) if len(set(e.type for e in self.sources)) == 1: @@ -424,7 +389,7 @@ class Overlay(object): source = source.replace("overlays.gentoo.org", "o.g.o") source = ' (' + pad(source, srclen) + ')' - return self._encode(name + mtype + source) + return encoder(name + mtype + source, self._encoding_) def is_official(self): '''Is the overlay official?''' diff --git a/layman/utils.py b/layman/utils.py index 18bf442..b2b38f1 100644 --- a/layman/utils.py +++ b/layman/utils.py @@ -30,14 +30,68 @@ __version__ = '$Id: utils.py 236 2006-09-05 20:39:37Z wrobel $' #------------------------------------------------------------------------------- import types, re, os +import locale +import codecs + from layman.debug import OUT + #=============================================================================== # # Helper functions # #------------------------------------------------------------------------------- +def decode_selection(selection): + '''utility function to decode a list of strings + accoring to the filesystem encoding + ''' + enc = sys.getfilesystemencoding() + if enc: + return [i.decode(enc) for i in selection] + return selection + + +def encoder(unicode_text, _encoding_): + return codecs.encode(unicode_text, _encoding_, 'replace') + + +def get_encoding(output): + if hasattr(output, 'encoding') \ + and output.encoding != None: + return output.encoding + else: + return locale.getpreferredencoding() + + +def pad(string, length): + '''Pad a string with spaces.''' + if len(string) <= length: + return string + ' ' * (length - len(string)) + else: + return string[:length - 3] + '...' + + +def terminal_width(): + '''Determine width of terminal window.''' + try: + width = int(os.environ['COLUMNS']) + if width > 0: + return width + except: + pass + try: + import struct, fcntl, termios + query = struct.pack('HHHH', 0, 0, 0, 0) + response = fcntl.ioctl(1, termios.TIOCGWINSZ, query) + width = struct.unpack('HHHH', response)[1] + if width > 0: + return width + except: + pass + return 80 + + def ensure_unicode(obj, encoding='utf-8'): if isinstance(obj, basestring): if not isinstance(obj, unicode): |