Added preliminary support for unicode (utf8) to the library

This commit is contained in:
Kovid Goyal 2006-12-21 07:26:48 +00:00
parent 65bdf9c0b7
commit 8bd8bfb39c
3 changed files with 34 additions and 51 deletions

View File

@ -21,18 +21,22 @@ The public interface of libprs500 is in L{libprs500.communicate}. To use it
>>> dev.get_device_information() >>> dev.get_device_information()
('Sony Reader', 'PRS-500/U', '1.0.00.21081', 'application/x-bbeb-book') ('Sony Reader', 'PRS-500/U', '1.0.00.21081', 'application/x-bbeb-book')
There is also a script L{prs500} that provides a command-line interface to libprs500. See the script There is also a script L{prs500} that provides a command-line interface to
libprs500. See the script
for more usage examples. A GUI is available via the command prs500-gui. for more usage examples. A GUI is available via the command prs500-gui.
The packet structure used by the SONY Reader USB protocol is defined in the module L{prstypes}. The communication logic The packet structure used by the SONY Reader USB protocol is defined
in the module L{prstypes}. The communication logic
is defined in the module L{communicate}. is defined in the module L{communicate}.
This package requires U{PyUSB<http://pyusb.berlios.de/>}. In order to use it as a non-root user on Linux, you should have This package requires U{PyUSB<http://pyusb.berlios.de/>}.
In order to use it as a non-root user on Linux, you should have
the following rule in C{/etc/udev/rules.d/90-local.rules} :: the following rule in C{/etc/udev/rules.d/90-local.rules} ::
BUS=="usb", SYSFS{idProduct}=="029b", SYSFS{idVendor}=="054c", MODE="660", GROUP="plugdev" BUS=="usb", SYSFS{idProduct}=="029b", SYSFS{idVendor}=="054c",
You may have to adjust the GROUP and the location of the rules file to suit your distribution. MODE="660", GROUP="plugdev"
You may have to adjust the GROUP and the location of the rules file to
suit your distribution.
""" """
__version__ = "0.3.0a4" __version__ = "0.3.0a4"
__docformat__ = "epytext" __docformat__ = "epytext"
__author__ = "Kovid Goyal <kovid@kovidgoyal.net>" __author__ = "Kovid Goyal <kovid@kovidgoyal.net>"
TEMPORARY_FILENAME_TEMPLATE = "libprs500_"+__version__+"_temp"

View File

@ -186,9 +186,10 @@ class Main(QObject, Ui_MainWindow):
"Choose books to add to library", _dir, \ "Choose books to add to library", _dir, \
"Books (*.lrf *.lrx *.rtf *.pdf *.txt);;All files (*)") "Books (*.lrf *.lrx *.rtf *.pdf *.txt);;All files (*)")
if not files.isEmpty(): if not files.isEmpty():
x = str(files[0]) x = unicode(files[0].toUtf8(), 'utf-8')
settings.setValue("add books dialog dir", QVariant(os.path.dirname(x))) settings.setValue("add books dialog dir", \
files = str(files.join("|||")).split("|||") QVariant(os.path.dirname(x)))
files = unicode(files.join("|||").toUtf8(), 'utf-8').split("|||")
self.add_books(files) self.add_books(files)
def add_books(self, files): def add_books(self, files):

View File

@ -24,8 +24,8 @@ from urlparse import urlparse, urlunparse
from urllib import quote, unquote from urllib import quote, unquote
from math import sin, cos, pi from math import sin, cos, pi
from libprs500 import TEMPORARY_FILENAME_TEMPLATE as TFT
from libprs500.gui import Error, _Warning from libprs500.gui import Error, _Warning
from libprs500.ptempfile import PersistentTemporaryFile
from PyQt4 import QtGui, QtCore from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import Qt, SIGNAL from PyQt4.QtCore import Qt, SIGNAL
@ -33,7 +33,7 @@ from PyQt4.Qt import QApplication, QString, QFont, QAbstractListModel, \
QVariant, QAbstractTableModel, QTableView, QListView, \ QVariant, QAbstractTableModel, QTableView, QListView, \
QLabel, QAbstractItemView, QPixmap, QIcon, QSize, \ QLabel, QAbstractItemView, QPixmap, QIcon, QSize, \
QMessageBox, QSettings, QFileDialog, QErrorMessage, \ QMessageBox, QSettings, QFileDialog, QErrorMessage, \
QSpinBox, QPoint, QTemporaryFile, QDir, QFile, \ QSpinBox, QPoint, \
QIODevice, QPainterPath, QItemDelegate, QPainter, QPen, \ QIODevice, QPainterPath, QItemDelegate, QPainter, QPen, \
QColor, QLinearGradient, QBrush, QStyle, QStringList, \ QColor, QLinearGradient, QBrush, QStyle, QStringList, \
QByteArray, QBuffer, QMimeData, QTextStream, QIODevice, \ QByteArray, QBuffer, QMimeData, QTextStream, QIODevice, \
@ -48,8 +48,12 @@ class FileDragAndDrop(object):
@classmethod @classmethod
def _bytes_to_string(cls, qba): def _bytes_to_string(cls, qba):
""" @type qba: QByteArray """ """
return unicode(QString.fromUtf8(qba.data())).strip() Assumes qba is encoded in ASCII which is usually fine, since
this method is used mainly for escaped URIs.
@type qba: QByteArray
"""
return str(QString.fromAscii(qba.data())).strip()
@classmethod @classmethod
def _get_r_ok_files(cls, event): def _get_r_ok_files(cls, event):
@ -68,7 +72,7 @@ class FileDragAndDrop(object):
continue continue
path = unquote(o.path) path = unquote(o.path)
if not os.access(path, os.R_OK): if not os.access(path, os.R_OK):
_Warning("You do not have read permission for: " + path) _Warning("You do not have read permission for: " + path, None)
continue continue
if os.path.isdir(path): if os.path.isdir(path):
root, dirs, files2 = os.walk(path) root, dirs, files2 = os.walk(path)
@ -134,7 +138,7 @@ class FileDragAndDrop(object):
self._dragged_files, urls = [], [] self._dragged_files, urls = [], []
for _file in files: for _file in files:
urls.append(urlunparse(('file', quote(gethostname()), \ urls.append(urlunparse(('file', quote(gethostname()), \
quote(str(_file.name)), '','',''))) quote(_file.name.encode('utf-8')), '','','')))
self._dragged_files.append(_file) self._dragged_files.append(_file)
mime_data.setData("text/uri-list", QByteArray("\n".join(urls))) mime_data.setData("text/uri-list", QByteArray("\n".join(urls)))
user = os.getenv('USER') user = os.getenv('USER')
@ -147,8 +151,7 @@ class FileDragAndDrop(object):
if extensions: if extensions:
files = [] files = []
for ext in extensions: for ext in extensions:
f = TemporaryFile(ext=ext) f = PersistentTemporaryFile(suffix="."+ext)
f.open()
files.append(f) files.append(f)
return self.drag_object_from_files(files), self._dragged_files return self.drag_object_from_files(files), self._dragged_files
@ -160,7 +163,7 @@ class TableView(FileDragAndDrop, QTableView):
@classmethod @classmethod
def wrap(cls, s, width=20): def wrap(cls, s, width=20):
return textwrap.fill(str(s), width) return textwrap.fill(s, width)
@classmethod @classmethod
def human_readable(cls, size): def human_readable(cls, size):
@ -202,31 +205,7 @@ class TableView(FileDragAndDrop, QTableView):
drag.setPixmap(self.render_to_pixmap(self.selectedIndexes())) drag.setPixmap(self.render_to_pixmap(self.selectedIndexes()))
return drag return drag
class TemporaryFile(QTemporaryFile):
_file_name = ""
def __del__(self):
if os.access(self.name, os.F_OK): os.remove(self.name)
def __init__(self, ext=""):
if ext: ext = "." + ext
path = QDir.tempPath() + "/" + TFT + "_XXXXXX"+ext
QTemporaryFile.__init__(self, path)
def open(self):
ok = QFile.open(self, QIODevice.ReadWrite)
self._file_name = os.path.normpath(os.path.abspath(\
str(QTemporaryFile.fileName(self))))
return ok
@apply
def name():
def fget(self):
return self._file_name
return property(**locals())
class NamedTemporaryFile(TemporaryFile):
def __init__(self, name):
path = QDir.tempPath() + "/" + "XXXXXX"+name
QTemporaryFile.__init__(self, path)
class CoverDisplay(FileDragAndDrop, QLabel): class CoverDisplay(FileDragAndDrop, QLabel):
def __init__(self, parent): def __init__(self, parent):
@ -426,11 +405,10 @@ class LibraryBooksModel(QAbstractTableModel):
else: else:
ext = "."+ext ext = "."+ext
name = basename+ext name = basename+ext
file = NamedTemporaryFile(name) file = PersistentTemporaryFile(suffix=name)
file.open()
if not fmt: if not fmt:
continue continue
file.write(QByteArray(fmt)) file.write(fmt)
file.close() file.close()
files.append(file) files.append(file)
return files return files
@ -465,7 +443,7 @@ class LibraryBooksModel(QAbstractTableModel):
row = index.row() row = index.row()
_id = self._data[row]["id"] _id = self._data[row]["id"]
col = index.column() col = index.column()
val = str(value.toString()) val = unicode(value.toString().toUtf8(), 'utf-8')
if col == 0: if col == 0:
col = "title" col = "title"
elif col == 1: elif col == 1: