diff --git a/src/calibre/devices/interface.py b/src/calibre/devices/interface.py index 64aff3bad2..2a87e39313 100644 --- a/src/calibre/devices/interface.py +++ b/src/calibre/devices/interface.py @@ -503,14 +503,16 @@ class DevicePlugin(Plugin): Called when calibre is is starting the device. Do any initialization required. Note that multiple instances of the class can be instantiated, and thus __init__ can be called multiple times, but only one instance - will have this method called. + will have this method called. This method is called on the device + thread, not the GUI thread. ''' pass def shutdown(self): ''' Called when calibre is shutting down, either for good or in preparation - to restart. Do any cleanup required. + to restart. Do any cleanup required. This method is called on the + device thread, not the GUI thread. ''' pass diff --git a/src/calibre/devices/prs500/cli/main.py b/src/calibre/devices/prs500/cli/main.py index e32d5e362e..16a9ab7b0a 100755 --- a/src/calibre/devices/prs500/cli/main.py +++ b/src/calibre/devices/prs500/cli/main.py @@ -174,6 +174,13 @@ def ls(dev, path, term, recurse=False, color=False, human_readable_size=False, l output.close() return listing +def shutdown_plugins(): + for d in device_plugins(): + try: + d.shutdown() + except: + pass + def main(): term = TerminalController() cols = term.COLS @@ -201,6 +208,10 @@ def main(): scanner.scan() connected_devices = [] for d in device_plugins(): + try: + d.startup() + except: + print ('Startup failed for device plugin: %s'%d) ok, det = scanner.is_device_connected(d) if ok: dev = d @@ -209,6 +220,7 @@ def main(): if dev is None: print >>sys.stderr, 'Unable to find a connected ebook reader.' + shutdown_plugins() return 1 for det, d in connected_devices: @@ -358,6 +370,9 @@ def main(): except (ArgumentError, DeviceError) as e: print >>sys.stderr, e return 1 + finally: + shutdown_plugins() + return 0 if __name__ == '__main__':