From 0962ebb3ba0016b223d603a819e0b59b5bfe6984 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Mon, 2 May 2011 14:58:26 +0100 Subject: [PATCH] Make user_defined_device information work on linux --- src/calibre/devices/__init__.py | 81 ++++++++++--------- .../gui2/preferences/device_user_defined.py | 36 +++++---- src/calibre/gui2/preferences/misc.py | 3 +- src/calibre/gui2/preferences/misc.ui | 2 +- 4 files changed, 66 insertions(+), 56 deletions(-) diff --git a/src/calibre/devices/__init__.py b/src/calibre/devices/__init__.py index 02c42a1d6e..e47cd82b50 100644 --- a/src/calibre/devices/__init__.py +++ b/src/calibre/devices/__init__.py @@ -162,45 +162,54 @@ def device_info(ioreg_to_tmp=False, buf=None): import re res = {} - if not iswindows: - return None + device_details = {} + device_set = set() + drive_details = {} + drive_set = set() + res['device_set'] = device_set + res['device_details'] = device_details + res['drive_details'] = drive_details + res['drive_set'] = drive_set + try: s = DeviceScanner() s.scan() devices = (s.devices) - device_details = {} - device_set = set() - for dev in devices: - vid = re.search('vid_([0-9a-f]*)&', dev) - if vid: - vid = vid.group(1) - pid = re.search('pid_([0-9a-f]*)&', dev) - if pid: - pid = pid.group(1) - rev = re.search('rev_([0-9a-f]*)$', dev) - if rev: - rev = rev.group(1) - d = vid+pid+rev - device_set.add(d) - device_details[d] = (vid, pid, rev) - res['device_set'] = device_set - res['device_details'] = device_details - drives = win_pnp_drives(debug=False) - drive_details = {} - drive_set = set() - for drive,details in drives.iteritems(): - order = 'ORD_' + str(drive.order) - ven = re.search('VEN_([^&]*)&', details) - if ven: - ven = ven.group(1) - prod = re.search('PROD_([^&]*)&', details) - if prod: - prod = prod.group(1) - d = (order, ven, prod) - drive_details[drive] = d - drive_set.add(drive) - res['drive_details'] = drive_details - res['drive_set'] = drive_set + if not iswindows: + devices = [list(x) for x in devices] + for dev in devices: + for i in range(3): + dev[i] = hex(dev[i]) + d = dev[0] + dev[1] + dev[2] + device_set.add(d) + device_details[d] = dev[0:3] + else: + for dev in devices: + vid = re.search('vid_([0-9a-f]*)&', dev) + if vid: + vid = vid.group(1) + pid = re.search('pid_([0-9a-f]*)&', dev) + if pid: + pid = pid.group(1) + rev = re.search('rev_([0-9a-f]*)$', dev) + if rev: + rev = rev.group(1) + d = vid+pid+rev + device_set.add(d) + device_details[d] = (vid, pid, rev) + + drives = win_pnp_drives(debug=False) + for drive,details in drives.iteritems(): + order = 'ORD_' + str(drive.order) + ven = re.search('VEN_([^&]*)&', details) + if ven: + ven = ven.group(1) + prod = re.search('PROD_([^&]*)&', details) + if prod: + prod = prod.group(1) + d = (order, ven, prod) + drive_details[drive] = d + drive_set.add(drive) finally: pass - return res \ No newline at end of file + return res diff --git a/src/calibre/gui2/preferences/device_user_defined.py b/src/calibre/gui2/preferences/device_user_defined.py index 914e2b5666..c2a27d3937 100644 --- a/src/calibre/gui2/preferences/device_user_defined.py +++ b/src/calibre/gui2/preferences/device_user_defined.py @@ -10,6 +10,8 @@ __docformat__ = 'restructuredtext en' from PyQt4.Qt import QDialog, QVBoxLayout, QPlainTextEdit, QTimer, \ QDialogButtonBox, QPushButton, QApplication, QIcon, QMessageBox +from calibre.constants import iswindows + def step_dialog(parent, title, msg, det_msg=''): d = QMessageBox(parent) d.setWindowTitle(title) @@ -26,10 +28,10 @@ class UserDefinedDevice(QDialog): self.setLayout(self._layout) self.log = QPlainTextEdit(self) self._layout.addWidget(self.log) - self.log.setPlainText(_('Getting debug information')+'...') + self.log.setPlainText(_('Getting device information')+'...') self.copy = QPushButton(_('Copy to &clipboard')) self.copy.setDefault(True) - self.setWindowTitle(_('Debug device detection')) + self.setWindowTitle(_('User-defined device information')) self.setWindowIcon(QIcon(I('debug.png'))) self.copy.clicked.connect(self.copy_to_clipboard) self.ok = QPushButton('&OK') @@ -59,7 +61,7 @@ class UserDefinedDevice(QDialog): new_drives = after['drive_set'] - before['drive_set'] new_devices = after['device_set'] - before['device_set'] res = '' - if len(new_drives) and len(new_devices) == 1: + if (not iswindows or len(new_drives)) and len(new_devices) == 1: for d in new_devices: res = _('USB Vendor ID (in hex)') + ': 0x' + \ after['device_details'][d][0] + '\n' @@ -67,20 +69,20 @@ class UserDefinedDevice(QDialog): after['device_details'][d][1] + '\n' res += _('USB Revision ID (in hex)') + ': 0x' + \ after['device_details'][d][2] + '\n' - # sort the drives by the order number - for i,d in enumerate(sorted(new_drives, - key=lambda x: after['drive_details'][x][0])): - if i == 0: - res += _('Windows main memory ID string') + ': ' + \ - after['drive_details'][d][1] + '\n' - res += _('Windows main memory ID string') + ': ' + \ - after['drive_details'][d][2] + '\n' - else: - res += _('Windows card A vendor string') + ': ' + \ - after['drive_details'][d][1] + '\n' - res += _('Windows card A ID string') + ': ' + \ - after['drive_details'][d][2] + '\n' - + if iswindows: + # sort the drives by the order number + for i,d in enumerate(sorted(new_drives, + key=lambda x: after['drive_details'][x][0])): + if i == 0: + res += _('Windows main memory ID string') + ': ' + \ + after['drive_details'][d][1] + '\n' + res += _('Windows main memory ID string') + ': ' + \ + after['drive_details'][d][2] + '\n' + else: + res += _('Windows card A vendor string') + ': ' + \ + after['drive_details'][d][1] + '\n' + res += _('Windows card A ID string') + ': ' + \ + after['drive_details'][d][2] + '\n' trailer = _('Enter the above values into the USER_DEVICE by ' 'customizing the device plugin. Be sure to also ' 'enter the folders where you want the books to ' diff --git a/src/calibre/gui2/preferences/misc.py b/src/calibre/gui2/preferences/misc.py index 179e8a995d..80bfdffcd8 100644 --- a/src/calibre/gui2/preferences/misc.py +++ b/src/calibre/gui2/preferences/misc.py @@ -9,7 +9,7 @@ __docformat__ = 'restructuredtext en' from calibre.gui2.preferences import ConfigWidgetBase, test_widget, Setting from calibre.gui2.preferences.misc_ui import Ui_Form from calibre.gui2 import error_dialog, config, open_local_file, info_dialog -from calibre.constants import isosx, iswindows +from calibre.constants import isosx class WorkersSetting(Setting): @@ -33,7 +33,6 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form): self.user_defined_device_button.clicked.connect(self.user_defined_device) self.button_osx_symlinks.clicked.connect(self.create_symlinks) self.button_osx_symlinks.setVisible(isosx) - self.user_defined_device_button.setVisible(iswindows) def debug_device_detection(self, *args): from calibre.gui2.preferences.device_debug import DebugDevice diff --git a/src/calibre/gui2/preferences/misc.ui b/src/calibre/gui2/preferences/misc.ui index df530bbe9a..843f0f01b7 100644 --- a/src/calibre/gui2/preferences/misc.ui +++ b/src/calibre/gui2/preferences/misc.ui @@ -61,7 +61,7 @@ - Get information to setup the &user defined device (Windows only) + Get information to setup the &user defined device