From 78f9efefd90bd2d0602e0f970fc7bf4e2c9e2ffa Mon Sep 17 00:00:00 2001 From: John Garland Date: Sun, 3 Oct 2010 18:12:42 +1100 Subject: [PATCH] Move decode_string/utf8_encoded to common --- DEPENDS | 4 +--- deluge/common.py | 36 ++++++++++++++++++++++++++++++ deluge/core/torrentmanager.py | 4 +--- deluge/ui/common.py | 41 +++-------------------------------- 4 files changed, 41 insertions(+), 44 deletions(-) diff --git a/DEPENDS b/DEPENDS index 1f9d05344..967d499f9 100644 --- a/DEPENDS +++ b/DEPENDS @@ -7,6 +7,7 @@ * setuptools * gettext * pyxdg + * chardet * geoip-database (optional) * libtorrent >= 0.14, or build the included version @@ -16,9 +17,6 @@ * openssl * zlib -=== UIs === - * chardet - === Gtk === * python-notify (libnotify python wrapper) * pygame diff --git a/deluge/common.py b/deluge/common.py index 6c4267590..128777134 100644 --- a/deluge/common.py +++ b/deluge/common.py @@ -41,6 +41,7 @@ import time import subprocess import platform import sys +import chardet try: import json @@ -560,6 +561,41 @@ def xml_encode(string): string = string.replace(char, escape) 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): """ Used for comparing version numbers. diff --git a/deluge/core/torrentmanager.py b/deluge/core/torrentmanager.py index 6a799cff2..5e137c6fd 100644 --- a/deluge/core/torrentmanager.py +++ b/deluge/core/torrentmanager.py @@ -47,16 +47,14 @@ from twisted.internet.task import LoopingCall from deluge._libtorrent import lt - from deluge.event import * from deluge.error import * -import deluge.common import deluge.component as component from deluge.configmanager import ConfigManager, get_config_dir from deluge.core.torrent import Torrent from deluge.core.torrent import TorrentOptions import deluge.core.oldstateupgrader -from deluge.ui.common import utf8_encoded +from deluge.common import utf8_encoded from deluge.log import LOG as log diff --git a/deluge/ui/common.py b/deluge/ui/common.py index 8f666d572..5f53db84e 100644 --- a/deluge/ui/common.py +++ b/deluge/ui/common.py @@ -42,7 +42,6 @@ import os import sys import urlparse -import chardet import locale try: @@ -50,45 +49,11 @@ try: except ImportError: 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 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): """ Collects information about a torrent file. @@ -336,7 +301,7 @@ class FileTree2(object): """ def walk(directory, parent_path): 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": directory["contents"][path] = callback(full_path, directory["contents"][path]) or \ directory["contents"][path]