[kupfer] kupferstring: Add fromlocale



commit 0d3ca115159e2f1cf3c9a07afe4d1bb4bbf935ce
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Tue Oct 27 00:29:45 2009 +0100

    kupferstring: Add fromlocale
    
    Fromlocale to read byte strings (from command line or other sources)
    in the locale encoding and return unicode strings.

 kupfer/kupferstring.py |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
---
diff --git a/kupfer/kupferstring.py b/kupfer/kupferstring.py
index 2378c0b..31a51b2 100644
--- a/kupfer/kupferstring.py
+++ b/kupfer/kupferstring.py
@@ -1,5 +1,6 @@
 # -*- encoding: UTF-8 -*-
 
+import locale
 from unicodedata import normalize, category
 
 def _folditems():
@@ -41,6 +42,12 @@ def toutf8(ustr):
 		return ustr
 	return ustr.encode("UTF-8", "replace")
 
+def fromlocale(lstr):
+	"""Return a unicode string from locale bytestring @lstr"""
+	enc = locale.getpreferredencoding()
+	return lstr.decode(enc, "replace")
+
+
 def tofolded(ustr):
 	u"""Fold @ustr
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]