mirror of
https://git.deluge-torrent.org/deluge
synced 2025-08-04 07:28:39 +00:00
Move decode_string/utf8_encoded to common
This commit is contained in:
parent
6b228ce31f
commit
78f9efefd9
4 changed files with 41 additions and 44 deletions
4
DEPENDS
4
DEPENDS
|
@ -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
|
||||||
|
|
|
@ -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.
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue