~ubuntu-branches/debian/sid/djvusmooth/sid

« back to all changes in this revision

Viewing changes to lib/djvused.py

  • Committer: Package Import Robot
  • Author(s): Daniel Stender
  • Date: 2015-09-15 15:34:11 UTC
  • mfrom: (1.1.12)
  • Revision ID: package-import@ubuntu.com-20150915153411-zlz201vxr4wk5d3p
Tags: 0.2.16-1
* New upstream release (Closes: #799017).
* deb/control:
  + added dh-python to build deps.
  + put python-djvu and python-wxgtk3.0 also into build-deps (needed by the
    tests).
  + dropped Depends: against python-xdg (Closes: #793364).
* deb/copyright: updated.
* deb/watch: watch pypi.debian.net.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# encoding=UTF-8
2
2
 
3
 
# Copyright © 2008-2011 Jakub Wilk <jwilk@jwilk.net>
4
 
#
5
 
# This package is free software; you can redistribute it and/or modify
6
 
# it under the terms of the GNU General Public License as published by
7
 
# the Free Software Foundation; version 2 dated June, 1991.
8
 
#
9
 
# This package is distributed in the hope that it will be useful, but
10
 
# WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
 
# General Public License for more details.
 
3
# Copyright © 2008-2015 Jakub Wilk <jwilk@jwilk.net>
 
4
#
 
5
# This file is part of djvusmooth.
 
6
#
 
7
# djvusmooth is free software; you can redistribute it and/or modify it
 
8
# under the terms of the GNU General Public License version 2 as published
 
9
# by the Free Software Foundation.
 
10
#
 
11
# djvusmooth is distributed in the hope that it will be useful, but WITHOUT
 
12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
13
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 
14
# more details.
13
15
 
14
 
import pkgconfig
15
16
import os.path
16
17
import subprocess
17
18
import threading
19
20
from djvu.sexpr import Expression, Symbol
20
21
 
21
22
djvused_path = None
22
 
if os.name =='nt':
 
23
if os.name == 'nt':
23
24
    from . import dependencies
24
25
    djvused_path = os.path.join(dependencies.djvulibre_path, 'djvused.exe')
25
26
else:
 
27
    from . import pkgconfig
26
28
    try:
27
29
        djvulibre_bin_path = os.path.join(pkgconfig.Package('ddjvuapi').variable('exec_prefix'), 'bin')
28
30
    except (IOError, OSError):
33
35
    # Let's hope it's within $PATH...
34
36
    djvused_path = 'djvused'
35
37
 
36
 
DJVUSED_PATH = djvused_path
37
 
 
38
38
def _djvused_usability_check():
39
39
    try:
40
 
        djvused = subprocess.Popen([DJVUSED_PATH], stdout = subprocess.PIPE, stderr = subprocess.PIPE)
 
40
        djvused = subprocess.Popen(
 
41
            [djvused_path],
 
42
            stdout=subprocess.PIPE,
 
43
            stderr=subprocess.PIPE
 
44
        )
41
45
        djvused.communicate()
42
46
        if djvused.returncode == 10:
43
47
            return
44
48
    except (IOError, OSError):
45
49
        pass
46
 
    raise IOError('%r does not seem to be usable' % DJVUSED_PATH)
 
50
    raise IOError('{path!r} does not seem to be usable'.format(path=djvused_path))
47
51
 
48
52
_djvused_usability_check()
49
53
 
52
56
 
53
57
class StreamEditor(object):
54
58
 
55
 
    def __init__(self, file_name, autosave = False):
 
59
    def __init__(self, file_name, autosave=False):
56
60
        self._file_name = file_name
57
61
        self._commands = []
58
62
        self._autosave = autosave
123
127
    def set_page_title(self, title):
124
128
        self._add('set-page-title %s' % Expression(title))
125
129
 
126
 
    def save_page(self, file_name, include = False):
 
130
    def save_page(self, file_name, include=False):
127
131
        command = 'save-page'
128
132
        if include:
129
133
            command += '-with'
139
143
        self._add('save')
140
144
 
141
145
    def _reader_thread(self, fo, result):
142
 
        result[0] = fo.read(),
 
146
        result[0] = fo.read()
143
147
 
144
 
    def _execute(self, commands, save = False):
145
 
        args = [DJVUSED_PATH]
 
148
    def _execute(self, commands, save=False):
 
149
        args = [djvused_path]
146
150
        if save:
147
151
            args += '-s',
148
152
        args += self._file_name,
149
 
        djvused = subprocess.Popen(args, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
 
153
        djvused = subprocess.Popen(args,
 
154
            stdin=subprocess.PIPE,
 
155
            stdout=subprocess.PIPE,
 
156
            stderr=subprocess.PIPE
 
157
        )
150
158
        result = [None]
151
 
        reader_thread = threading.Thread(target = self._reader_thread, args = (djvused.stdout, result))
 
159
        reader_thread = threading.Thread(
 
160
            target=self._reader_thread,
 
161
            args=(djvused.stdout, result)
 
162
        )
152
163
        reader_thread.setDaemon(True)
153
164
        reader_thread.start()
154
165
        stdin = djvused.stdin
163
174
 
164
175
    def commit(self):
165
176
        try:
166
 
            return self._execute(self._commands, save = self._autosave)
 
177
            return self._execute(self._commands, save=self._autosave)
167
178
        finally:
168
179
            self._commands = []
169
180
 
170
 
# vim:ts=4 sw=4 et
 
181
# vim:ts=4 sts=4 sw=4 et