Browse Source

Add unicode decode/encode helpers

tags/nilmdb-1.6.3
Jim Paris 11 years ago
parent
commit
9bbb95b18b
3 changed files with 19 additions and 6 deletions
  1. +3
    -6
      nilmdb/cmdline/cmdline.py
  2. +1
    -0
      nilmdb/utils/__init__.py
  3. +15
    -0
      nilmdb/utils/unicode.py

+ 3
- 6
nilmdb/cmdline/cmdline.py View File

@@ -73,13 +73,10 @@ class Complete(object): # pragma: no cover
if not path:
return []
results = []
prefix = nilmdb.utils.unicode.decode(prefix)
for (k,v) in client.stream_get_metadata(path).iteritems():
try:
k = str(k)
v = str(v)
except UnicodeEncodeError:
k = k.encode('utf-8')
v = v.encode('utf-8')
k = nilmdb.utils.unicode.encode(k)
v = nilmdb.utils.unicode.encode(v)
if k.startswith(prefix):
results.append(self.escape(k + '=' + v))
return results


+ 1
- 0
nilmdb/utils/__init__.py View File

@@ -14,3 +14,4 @@ import nilmdb.utils.iterator
import nilmdb.utils.interval
import nilmdb.utils.lock
import nilmdb.utils.sort
import nilmdb.utils.unicode

+ 15
- 0
nilmdb/utils/unicode.py View File

@@ -0,0 +1,15 @@
def encode(u):
"""Try to encode something from Unicode to a string using the
default encoding. If it fails, try encoding as UTF-8."""
try:
return u.encode()
except UnicodeEncodeError:
return u.encode("utf-8")

def decode(s):
"""Try to decode someting from string to Unicode using the
default encoding. If it fails, try decoding as UTF-8."""
try:
return s.decode()
except UnicodeDecodeError:
return s.decode("utf-8")

Loading…
Cancel
Save