Fix showing non-utf8 encoded torrents in add torrent dialog -- this adds an additional dependency on chardet.

This commit is contained in:
Andrew Resch 2009-05-09 17:46:13 +00:00
commit 2856e948de
3 changed files with 33 additions and 9 deletions

View file

@ -5,6 +5,8 @@
==== GtkUI ==== ==== GtkUI ====
* Fix high cpu usage when displaying speeds in titlebar * Fix high cpu usage when displaying speeds in titlebar
* Fix showing non-utf8 encoded torrents in add torrent dialog -- this adds
an additional dependency on chardet.
==== WebUI ==== ==== WebUI ====
* Fix starting when -l option is used * Fix starting when -l option is used

3
README
View file

@ -41,7 +41,8 @@ sudo apt-get install g++ make python-all-dev python-all python-dbus \
python-gtk2 python-notify librsvg2-common python-xdg python-support \ python-gtk2 python-notify librsvg2-common python-xdg python-support \
subversion libboost-dev libboost-python-dev libboost-iostreams-dev \ subversion libboost-dev libboost-python-dev libboost-iostreams-dev \
libboost-thread-dev libboost-date-time-dev libboost-filesystem-dev \ libboost-thread-dev libboost-date-time-dev libboost-filesystem-dev \
libboost-serialization-dev libssl-dev zlib1g-dev python-setuptools libboost-serialization-dev libssl-dev zlib1g-dev python-setuptools \
python-chardet
The names of the packages may vary depending on your OS / distro. The names of the packages may vary depending on your OS / distro.

View file

@ -1,9 +1,9 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# #
# deluge/ui/common.py # deluge/ui/common.py
# #
# Copyright (C) Damien Churchill 2008 <damoxc@gmail.com> # Copyright (C) Damien Churchill 2008 <damoxc@gmail.com>
# Copyright (C) Andrew Resch 2009 <andrewresch@gmail.com>
# #
# This program is free software; you can redistribute it and/or modify # This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
@ -32,9 +32,6 @@
# statement from all source files in the program, then also delete it here. # statement from all source files in the program, then also delete it here.
# #
#
import os import os
try: try:
from hashlib import sha1 as sha from hashlib import sha1 as sha
@ -46,6 +43,28 @@ from deluge import bencode
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: str to decode
:param encoding: str, the encoding to use in the decoding
"""
try:
s = s.decode(encoding).encode("utf8")
except UnicodeDecodeError:
try:
import chardet
except ImportError:
s = s.decode(encoding, "replace").encode("utf8")
else:
s = s.decode(chardet.detect(s)["encoding"]).encode("utf8")
return s
class TorrentInfo(object): class TorrentInfo(object):
def __init__(self, filename): def __init__(self, filename):
# Get the torrent data from the torrent file # Get the torrent data from the torrent file
@ -65,29 +84,31 @@ class TorrentInfo(object):
elif "codepage" in self.__m_metadata: elif "codepage" in self.__m_metadata:
self.encoding = str(self.__m_metadata["codepage"]) self.encoding = str(self.__m_metadata["codepage"])
self.__m_name = decode_string(self.__m_metadata["info"]["name"])
# Get list of files from torrent info # Get list of files from torrent info
self.__m_files = [] self.__m_files = []
if self.__m_metadata["info"].has_key("files"): if self.__m_metadata["info"].has_key("files"):
prefix = "" prefix = ""
if len(self.__m_metadata["info"]["files"]) > 1: if len(self.__m_metadata["info"]["files"]) > 1:
prefix = self.__m_metadata["info"]["name"].decode(self.encoding, "replace").encode("utf8") prefix = self.__m_name
for f in self.__m_metadata["info"]["files"]: for f in self.__m_metadata["info"]["files"]:
self.__m_files.append({ self.__m_files.append({
'path': os.path.join(prefix, *f["path"]).decode(self.encoding, "replace").encode("utf8"), 'path': decode_string(os.path.join(prefix, *f["path"])),
'size': f["length"], 'size': f["length"],
'download': True 'download': True
}) })
else: else:
self.__m_files.append({ self.__m_files.append({
"path": self.__m_metadata["info"]["name"].decode(self.encoding, "replace").encode("utf8"), "path": self.__m_name,
"size": self.__m_metadata["info"]["length"], "size": self.__m_metadata["info"]["length"],
"download": True "download": True
}) })
@property @property
def name(self): def name(self):
return self.__m_metadata["info"]["name"].decode(self.encoding, "replace").encode("utf8") return self.__m_name
@property @property
def info_hash(self): def info_hash(self):