From 3c8450f620ceebd8e21667c82bd82f53f15da0a3 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 12 Aug 2012 22:29:51 +0530 Subject: [PATCH] ... --- src/calibre/devices/mtp/windows/remote.py | 51 +++++++++++++++++++---- src/calibre/devices/mtp/windows/utils.cpp | 5 +-- src/calibre/devices/mtp/windows/wpd.cpp | 1 + 3 files changed, 47 insertions(+), 10 deletions(-) diff --git a/src/calibre/devices/mtp/windows/remote.py b/src/calibre/devices/mtp/windows/remote.py index 5bf971ce8f..7238110532 100644 --- a/src/calibre/devices/mtp/windows/remote.py +++ b/src/calibre/devices/mtp/windows/remote.py @@ -7,9 +7,9 @@ __license__ = 'GPL v3' __copyright__ = '2012, Kovid Goyal ' __docformat__ = 'restructuredtext en' -import subprocess, sys, os +import subprocess, sys, os, pprint -def build(): +def build(mod='wpd'): builder = subprocess.Popen('ssh xp_build ~/build-wpd'.split()) syncer = subprocess.Popen('ssh getafix ~/test-wpd'.split()) if builder.wait() != 0: @@ -17,9 +17,9 @@ def build(): if syncer.wait() != 0: raise Exception('Failed to rsync to getafix') subprocess.check_call( - 'scp xp_build:build/calibre/src/calibre/plugins/wpd.pyd /tmp'.split()) + ('scp xp_build:build/calibre/src/calibre/plugins/%s.pyd /tmp'%mod).split()) subprocess.check_call( - 'scp /tmp/wpd.pyd getafix:calibre/src/calibre/devices/mtp/windows'.split()) + ('scp /tmp/%s.pyd getafix:calibre/src/calibre/devices/mtp/windows'%mod).split()) p = subprocess.Popen( 'ssh getafix calibre-debug -e calibre/src/calibre/devices/mtp/windows/remote.py'.split()) p.wait() @@ -27,7 +27,6 @@ def build(): def main(): - import pprint sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import wpd from calibre.constants import plugins @@ -41,6 +40,44 @@ def main(): finally: wpd.uninit() -if __name__ == '__main__': - main() +def winutil(): + sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) + del sys.modules['winutil'] + import winutil + from calibre.constants import plugins + plugins._plugins['winutil'] = (winutil, '') + sys.path.pop(0) + print (winutil.serial_number_from_drive('F')) + +def get_subkeys(key): + import _winreg + index = -1 + while True: + index += 1 + try: + yield _winreg.EnumKey(key, index) + except OSError: + break + +def get_values(key): + import _winreg + index = -1 + while True: + index +=1 + try: + yield _winreg.EnumValue(key, index) + except OSError: + break + +def test(): + vid, pid = 0x1949, 0x4 + import _winreg as r + usb = r.OpenKey(r.HKEY_LOCAL_MACHINE, 'SYSTEM\\CurrentControlSet\\Enum\\USB') + q = ('vid_%4.4x&pid_%4.4x'%(vid, pid)) + dev = r.OpenKey(usb, q) + print (list(get_subkeys(dev))) + +if __name__ == '__main__': + # main() + winutil() diff --git a/src/calibre/devices/mtp/windows/utils.cpp b/src/calibre/devices/mtp/windows/utils.cpp index 424dae0852..7cf2757dc8 100644 --- a/src/calibre/devices/mtp/windows/utils.cpp +++ b/src/calibre/devices/mtp/windows/utils.cpp @@ -33,13 +33,12 @@ PyObject *wpd::hresult_set_exc(const char *msg, HRESULT hr) { wchar_t *wpd::unicode_to_wchar(PyObject *o) { wchar_t *buf; Py_ssize_t len; - if (!PyUnicode_Check(o)) {PyErr_Format(PyExc_TypeError, "The pnp id must be a unicode object"); return NULL;} + if (!PyUnicode_Check(o)) {PyErr_Format(PyExc_TypeError, "The python object must be a unicode object"); return NULL;} len = PyUnicode_GET_SIZE(o); - if (len < 1) {PyErr_Format(PyExc_TypeError, "The pnp id must not be empty."); return NULL;} buf = (wchar_t *)calloc(len+2, sizeof(wchar_t)); if (buf == NULL) { PyErr_NoMemory(); return NULL; } len = PyUnicode_AsWideChar((PyUnicodeObject*)o, buf, len); - if (len == -1) { free(buf); PyErr_Format(PyExc_TypeError, "Invalid pnp id."); return NULL; } + if (len == -1) { free(buf); PyErr_Format(PyExc_TypeError, "Invalid python unicode object."); return NULL; } return buf; } diff --git a/src/calibre/devices/mtp/windows/wpd.cpp b/src/calibre/devices/mtp/windows/wpd.cpp index 76ecea65b8..73b1767732 100644 --- a/src/calibre/devices/mtp/windows/wpd.cpp +++ b/src/calibre/devices/mtp/windows/wpd.cpp @@ -148,6 +148,7 @@ wpd_device_info(PyObject *self, PyObject *args) { if (!PyArg_ParseTuple(args, "O", &py_pnp_id)) return NULL; pnp_id = unicode_to_wchar(py_pnp_id); + if (wcslen(pnp_id) < 1) { PyErr_SetString(WPDError, "The PNP id must not be empty."); return NULL; } if (pnp_id == NULL) return NULL; client_information = get_client_information();