summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrian Dolbec <brian.dolbec@gmail.com>2011-01-16 22:52:52 -0800
committerBrian Dolbec <brian.dolbec@gmail.com>2011-02-11 02:49:14 -0800
commitc551ad652c54eaccc974c1bb87e8c7582adacbae (patch)
tree29c6a99d1d3377ae14e05784d24e91919df47590
parentd7510f1f7ee5c5bec7bdba68a310c1ce17444821 (diff)
downloadlayman-c551ad652c54eaccc974c1bb87e8c7582adacbae.tar.gz
layman-c551ad652c54eaccc974c1bb87e8c7582adacbae.tar.bz2
layman-c551ad652c54eaccc974c1bb87e8c7582adacbae.zip
move utility functions out of the Overlay class
-rw-r--r--layman/overlays/overlay.py47
-rw-r--r--layman/utils.py55
2 files changed, 61 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..79b3c18 100644
--- a/layman/utils.py
+++ b/layman/utils.py
@@ -30,14 +30,69 @@ __version__ = '$Id: utils.py 236 2006-09-05 20:39:37Z wrobel $'
#-------------------------------------------------------------------------------
import types, re, os
+import sys
+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):