~ubuntu-branches/ubuntu/karmic/calibre/karmic

« back to all changes in this revision

Viewing changes to src/calibre/utils/ipc/worker.py

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-07-30 12:49:41 UTC
  • mfrom: (1.3.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090730124941-qjdsmri25zt8zocn
Tags: 0.6.3+dfsg-0ubuntu1
* New upstream release. Please see http://calibre.kovidgoyal.net/new_in_6/
  for the list of new features and changes.
* remove_postinstall.patch: Update for new version.
* build_debug.patch: Does not apply any more, disable for now. Might not be
  necessary any more.
* debian/copyright: Fix reference to versionless GPL.
* debian/rules: Drop obsolete dh_desktop call.
* debian/rules: Add workaround for weird Python 2.6 setuptools behaviour of
  putting compiled .so files into src/calibre/plugins/calibre/plugins
  instead of src/calibre/plugins.
* debian/rules: Drop hal fdi moving, new upstream version does not use hal
  any more. Drop hal dependency, too.
* debian/rules: Install udev rules into /lib/udev/rules.d.
* Add debian/calibre.preinst: Remove unmodified
  /etc/udev/rules.d/95-calibre.rules on upgrade.
* debian/control: Bump Python dependencies to 2.6, since upstream needs
  it now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
 
3
from __future__ import with_statement
 
4
 
 
5
__license__   = 'GPL v3'
 
6
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
 
7
__docformat__ = 'restructuredtext en'
 
8
 
 
9
import os, cPickle, sys
 
10
from multiprocessing.connection import Client
 
11
from threading import Thread
 
12
from Queue import Queue
 
13
from contextlib import closing
 
14
from binascii import unhexlify
 
15
from calibre import prints
 
16
 
 
17
PARALLEL_FUNCS = {
 
18
      'lrfviewer'    :
 
19
        ('calibre.gui2.lrf_renderer.main', 'main', None),
 
20
 
 
21
      'ebook-viewer'    :
 
22
        ('calibre.gui2.viewer.main', 'main', None),
 
23
 
 
24
      'render_pages' :
 
25
        ('calibre.ebooks.comic.input', 'render_pages', 'notification'),
 
26
 
 
27
      'gui_convert'     :
 
28
        ('calibre.gui2.convert.gui_conversion', 'gui_convert', 'notification'),
 
29
 
 
30
      'move_library'     :
 
31
        ('calibre.library.move', 'move_library', 'notification'),
 
32
 
 
33
      'read_metadata' :
 
34
      ('calibre.ebooks.metadata.worker', 'read_metadata_', 'notification'),
 
35
 
 
36
      'read_pdf_metadata' :
 
37
      ('calibre.utils.podofo.__init__', 'get_metadata_', None),
 
38
 
 
39
      'write_pdf_metadata' :
 
40
      ('calibre.utils.podofo.__init__', 'set_metadata_', None),
 
41
 
 
42
      'save_book' :
 
43
      ('calibre.ebooks.metadata.worker', 'save_book', 'notification'),
 
44
}
 
45
 
 
46
class Progress(Thread):
 
47
 
 
48
    def __init__(self, conn):
 
49
        Thread.__init__(self)
 
50
        self.daemon = True
 
51
        self.conn = conn
 
52
        self.queue = Queue()
 
53
 
 
54
    def __call__(self, percent, msg=''):
 
55
        self.queue.put((percent, msg))
 
56
 
 
57
    def run(self):
 
58
        while True:
 
59
            x = self.queue.get()
 
60
            if x is None:
 
61
                break
 
62
            try:
 
63
                self.conn.send(x)
 
64
            except:
 
65
                break
 
66
 
 
67
 
 
68
 
 
69
def get_func(name):
 
70
    module, func, notification = PARALLEL_FUNCS[name]
 
71
    module = __import__(module, fromlist=[1])
 
72
    func = getattr(module, func)
 
73
    return func, notification
 
74
 
 
75
def main():
 
76
    address = cPickle.loads(unhexlify(os.environ['CALIBRE_WORKER_ADDRESS']))
 
77
    key     = unhexlify(os.environ['CALIBRE_WORKER_KEY'])
 
78
    resultf = unhexlify(os.environ['CALIBRE_WORKER_RESULT'])
 
79
    with closing(Client(address, authkey=key)) as conn:
 
80
        name, args, kwargs, desc = conn.recv()
 
81
        if desc:
 
82
            prints(desc)
 
83
            sys.stdout.flush()
 
84
        func, notification = get_func(name)
 
85
        notifier = Progress(conn)
 
86
        if notification:
 
87
            kwargs[notification] = notifier
 
88
            notifier.start()
 
89
 
 
90
        result = func(*args, **kwargs)
 
91
        if result is not None:
 
92
            cPickle.dump(result, open(resultf, 'wb'), -1)
 
93
 
 
94
        notifier.queue.put(None)
 
95
 
 
96
    sys.stdout.flush()
 
97
    sys.stderr.flush()
 
98
    return 0
 
99
 
 
100
 
 
101
 
 
102
if __name__ == '__main__':
 
103
    sys.exit(main())