Move decode_string/utf8_encoded to common

This commit is contained in:
John Garland 2010-10-03 18:12:42 +11:00
commit 78f9efefd9
4 changed files with 41 additions and 44 deletions

View file

@ -7,6 +7,7 @@
* setuptools * setuptools
* gettext * gettext
* pyxdg * pyxdg
* chardet
* geoip-database (optional) * geoip-database (optional)
* libtorrent >= 0.14, or build the included version * libtorrent >= 0.14, or build the included version
@ -16,9 +17,6 @@
* openssl * openssl
* zlib * zlib
=== UIs ===
* chardet
=== Gtk === === Gtk ===
* python-notify (libnotify python wrapper) * python-notify (libnotify python wrapper)
* pygame * pygame

View file

@ -41,6 +41,7 @@ import time
import subprocess import subprocess
import platform import platform
import sys import sys
import chardet
try: try:
import json import json
@ -560,6 +561,41 @@ def xml_encode(string):
string = string.replace(char, escape) string = string.replace(char, escape)
return string return string
def decode_string(s, encoding="utf8"):
"""
Decodes a string and re-encodes it in utf8. If it cannot decode using
`:param:encoding` then it will try to detect the string encoding and
decode it.
:param s: string to decode
:type s: string
:keyword encoding: the encoding to use in the decoding
:type encoding: string
"""
try:
s = s.decode(encoding).encode("utf8", "ignore")
except UnicodeDecodeError:
s = s.decode(chardet.detect(s)["encoding"], "ignore").encode("utf8", "ignore")
return s
def utf8_encoded(s):
"""
Returns a utf8 encoded string of s
:param s: (unicode) string to (re-)encode
:type s: basestring
:returns: a utf8 encoded string of s
:rtype: str
"""
if isinstance(s, str):
s = decode_string(s, locale.getpreferredencoding())
elif isinstance(s, unicode):
s = s.encode("utf8", "ignore")
return s
class VersionSplit(object): class VersionSplit(object):
""" """
Used for comparing version numbers. Used for comparing version numbers.

View file

@ -47,16 +47,14 @@ from twisted.internet.task import LoopingCall
from deluge._libtorrent import lt from deluge._libtorrent import lt
from deluge.event import * from deluge.event import *
from deluge.error import * from deluge.error import *
import deluge.common
import deluge.component as component import deluge.component as component
from deluge.configmanager import ConfigManager, get_config_dir from deluge.configmanager import ConfigManager, get_config_dir
from deluge.core.torrent import Torrent from deluge.core.torrent import Torrent
from deluge.core.torrent import TorrentOptions from deluge.core.torrent import TorrentOptions
import deluge.core.oldstateupgrader import deluge.core.oldstateupgrader
from deluge.ui.common import utf8_encoded from deluge.common import utf8_encoded
from deluge.log import LOG as log from deluge.log import LOG as log

View file

@ -42,7 +42,6 @@ import os
import sys import sys
import urlparse import urlparse
import chardet
import locale import locale
try: try:
@ -50,45 +49,11 @@ try:
except ImportError: except ImportError:
from sha import sha from sha import sha
from deluge import bencode, common from deluge import bencode
from deluge.common import decode_string, path_join
from deluge.log import LOG as log from deluge.log import LOG as log
import deluge.configmanager import deluge.configmanager
def decode_string(s, encoding="utf8"):
"""
Decodes a string and re-encodes it in utf8. If it cannot decode using
`:param:encoding` then it will try to detect the string encoding and
decode it.
:param s: string to decode
:type s: string
:keyword encoding: the encoding to use in the decoding
:type encoding: string
"""
try:
s = s.decode(encoding).encode("utf8", "ignore")
except UnicodeDecodeError:
s = s.decode(chardet.detect(s)["encoding"], "ignore").encode("utf8", "ignore")
return s
def utf8_encoded(s):
"""
Returns a utf8 encoded string of s
:param s: (unicode) string to (re-)encode
:type s: basestring
:returns: a utf8 encoded string of s
:rtype: str
"""
if isinstance(s, str):
s = decode_string(s, locale.getpreferredencoding())
elif isinstance(s, unicode):
s = s.encode("utf8", "ignore")
return s
class TorrentInfo(object): class TorrentInfo(object):
""" """
Collects information about a torrent file. Collects information about a torrent file.
@ -336,7 +301,7 @@ class FileTree2(object):
""" """
def walk(directory, parent_path): def walk(directory, parent_path):
for path in directory["contents"].keys(): for path in directory["contents"].keys():
full_path = common.path_join(parent_path, path) full_path = path_join(parent_path, path)
if directory["contents"][path]["type"] == "dir": if directory["contents"][path]["type"] == "dir":
directory["contents"][path] = callback(full_path, directory["contents"][path]) or \ directory["contents"][path] = callback(full_path, directory["contents"][path]) or \
directory["contents"][path] directory["contents"][path]