Skip to content

Commit

Permalink
fix: searches inside calibre's config dir now; minor text fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pirtim committed Mar 29, 2017
1 parent bf6ede0 commit fe18b00
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 52 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ USER := kfix
PLUGIN := djvumaker
REPO := calibre_plugin_djvumaker
ZIP := $(PLUGIN).zip

GH_RELEASE_JSON = '{"tag_name": "v$(VERSION)","target_commitish": "master","name": "v$(VERSION)","body": "calibre plugin of version $(VERSION)","draft": false,"prerelease": false}'

all: clean release
Expand Down Expand Up @@ -37,6 +37,6 @@ clean:

test: $(ZIP)
calibre-customize -a $(ZIP)
calibre-debug -r djvumaker test.pdf
calibre-debug -r djvumaker -- convert -p test.pdf

.PHONY: clean tag release all test
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Installation

4. [Optional] go to Preferences -> Interface::Toolbars so you can place the DJVU menu where you see fit.

Installation of secondary backend
Installation of pdf2djvu backend
---
For all having troubles with building GsDjvu there is possibility to install secondary backend - [pdf2djvu](http://jwilk.net/software/pdf2djvu).
The *pdf2djvu* is a pdf to djvu converter developed by Jakub Wilk ([GitHub](https://github.com/jwilk/pdf2djvu)).
Expand Down Expand Up @@ -76,7 +76,7 @@ positional arguments:
conversion first, works for every backend
postimport Change postimport settings
-y, --yes sets plugin to convert PDF files after import (do not work for pdf2djvu)
-y, --yes sets plugin to convert PDF files after import (sometimes do not work for pdf2djvu)
-n, --no sets plugin to do not convert PDF files after import (default)
install_deps (depreciated) alias for `calibre-debug -r djvumaker -- backend install djvudigital`
Expand Down
89 changes: 54 additions & 35 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@
conversion first, works for every backend
postimport Change postimport settings
-y, --yes sets plugin to convert PDF files after import (do not work for pdf2djvu)
-y, --yes sets plugin to convert PDF files after import (sometimes do not work for pdf2djvu)
-n, --no sets plugin to do not convert PDF files after import (default)
install_deps (depreciated) alias for `calibre-debug -r djvumaker -- backend install djvudigital`
convert_all (depreciated) alias for `calibre-debug -r djvumaker -- convert --all`
test (only for debugging, first has to be turned on in utils.py:53) custom command
optional arguments:
-h, --help show help message and exit
Expand Down Expand Up @@ -69,16 +70,16 @@
Conversion can be started through:
* right click in GUI menu in library:
gui.py:#NODOC:ConvertToDJVUAction.initialization_complete ->
-> gui.py:#NODOC:ConvertToDJVUAction.convert_book ->
-> gui.py:#NODOC:ConvertToDJVUAction._convert_books ->
-> gui.py:#NODOC:ConvertToDJVUAction._tjob_djvu_convert ->
-> gui.py:#NODOC:DJVUPlugin._postimport ->
->__init__.py:#NODOC:DJVUPlugin.run_backend ->
->__init__.py:#NODOC:DJVUPlugin.REGISTERED_BACKENDS[use_backend] ->
->__init__.py:#NODOC:register_backend ->
->__init__.py:#NODOC:job_handler ->
->__init__.py:#NODOC:{pdf2djvu//djvudigital}
gui.py:052:ConvertToDJVUAction.initialization_complete ->
-> gui.py:069:ConvertToDJVUAction.convert_book ->
-> gui.py:074:ConvertToDJVUAction._convert_books ->
-> gui.py:110:ConvertToDJVUAction._tjob_djvu_convert ->
->__init__.py:612:DJVUPlugin._postimport ->
->__init__.py:356:DJVUPlugin.run_backend ->
->__init__.py:377:DJVUPlugin.REGISTERED_BACKENDS[use_backend] ->
->__init__.py:305:register_backend ->
->__init__.py:788:job_handler ->
->__init__.py:916:{pdf2djvu//djvudigital}
* right click in GUI menu in library-like view on device:
(currently NotImplemented)
...->gui.py:#NODOC:ConvertToDJVUAction._tjob_djvu_convert||elif fpath -> ???
Expand All @@ -88,7 +89,7 @@
->__init__.py:#NODOC:DJVUPlugin.worker_fork_job ->
->__init__.py:#NODOC:DJVUPlugin.plugin_prefs['use_backend'] ->
->__init__.py:#NODOC:job_handler -> ...
* through postimport conversion during CLI: `calibre-debug -r djvumaker -- convert --all`
* through postimport conversion during CLI with --all: `calibre-debug -r djvumaker -- convert --all`
__init__.py:#NODOC:DJVUPlugin.cli_main ->
-> utils.py:#NODOC:create_cli_parser ->
->__init__.py:#NODOC:DJVUPlugin.cli_convert ->
Expand All @@ -100,7 +101,7 @@
__init__.py:#NODOC:DJVUPlugin.postimport ->
->__init__.py:#NODOC:DJVUPlugin._postimport ->
->__init__.py:#NODOC:DJVUPlugin.run_backend -> ...
* through ID conversion during CLI: `calibre-debug -r djvumaker -- convert -i ID`
* through ID conversion during CLI: `calibre-debug -r djvumaker -- convert -i ID`
__init__.py:#NODOC:DJVUPlugin.cli_main ->
-> utils.py:#NODOC:create_cli_parser ->
->__init__.py:#NODOC:DJVUPlugin.cli_convert ->
Expand Down Expand Up @@ -224,10 +225,11 @@
* (M) inside gui.py -> _tjob_djvu_convert -> elif fpath -- conversion for devices
* (M) custom printing for djvudigital with notification support
* (M) pdf2djvu installation with GitHub API v3
* (M) cross import __init__.py inside utils for PLUGINNAME
* (M-H) custom scripts for conversion
* (M-H) installation scripts for djvudigital for not macOS
* (M-H) add other backend support
* (M-H) pdf2djvu doesn't work for postimport
* (M-H) pdf2djvu sometimes doesn't work for postimport
* (H) plugin settings QT widget
* (H) make general overhaul of starting conversion logic
* (H) add support for conversion from other formats
Expand All @@ -248,7 +250,7 @@
sys.stdout.write(PLUGINVER_DOT) #Makefile needs this to do releases
sys.exit()

import errno, os, sys, shutil, traceback, subprocess, collections#, argparse # TEST COMMENT
import errno, os, sys, shutil, traceback, subprocess, collections
from functools import partial, wraps

from calibre import force_unicode, prints
Expand All @@ -261,7 +263,7 @@
from calibre.utils.ipc.simple_worker import fork_job as worker_fork_job, WorkerError
from calibre_plugins.djvumaker.utils import (create_backend_link, create_cli_parser, install_pdf2djvu,
discover_backend, ask_yesno_input, empty_function,
EmptyClass, add_method_dec)
EmptyClass, add_method_dec, plugin_dir)

# if iswindows and hasattr(sys, 'frozen'):
# # CREATE_NO_WINDOW=0x08 so that no ugly console is popped up
Expand Down Expand Up @@ -336,20 +338,26 @@ def __init__(self, *args, **kwargs):
def site_customization_parser(self, use_backend):
"""Parse user input from "Customize plugin" menu. Return backend and cmd flags to use."""
backend, cmdflags = use_backend, self.plugin_prefs[use_backend]['flags']
if self.site_customization != '':
site_customization = self.site_customization.split()
if site_customization[0] in self.REGISTERED_BACKENDS:
backend = site_customization[0]
cmdflags = site_customization[1:]
elif site_customization[0][0] == '-':
backend = use_backend
cmdflags = site_customization
#`--gsarg=-dFirstPage=1,-dLastPage=1` how to limit page range
#more gsargs: https://leanpub.com/pdfkungfoo
else:
# TODO: Custom command implementation
# some template engine with %djvu, %src or sth
raise NotImplementedError('Custom commands are not implemented')
# site_customization is problematic, cannot assume about its content
try:
if self.site_customization is not None:
site_customization = self.site_customization.split()
if site_customization[0] in self.REGISTERED_BACKENDS:
backend = site_customization[0]
cmdflags = site_customization[1:]
elif site_customization[0][0] == '-':
backend = use_backend
cmdflags = site_customization
#`--gsarg=-dFirstPage=1,-dLastPage=1` how to limit page range
#more gsargs: https://leanpub.com/pdfkungfoo
else:
# TODO: Custom command implementation
# some template engine with %djvu, %src or sth
raise NotImplementedError('Custom commands are not implemented')
except NotImplementedError:
raise
except:
pass
return backend, cmdflags

def run_backend(self, *args, **kwargs):
Expand Down Expand Up @@ -425,7 +433,11 @@ def cli_main(self, args):

def cli_test(self, args):
"""Debug method."""
prints(subprocess.check_output(['pwd']))
from calibre.utils.config import config_dir
prints(config_dir)
prints(os.path.join(config_dir, 'plugins', 'djvumaker'))
prints(plugin_dir(PLUGINNAME))
# prints(subprocess.check_output(['pwd']))

def cli_backend(self, args):
#NODOC
Expand Down Expand Up @@ -476,8 +488,10 @@ def cli_install_backend(self, args):
sys.exit()
# need a cask for the caminova finder/safari plugin too
# TODO: make more install scripts
# for linux it should be relatively easy
# for plain windows probably impossible, only through cygwin
elif islinux: raise Exception('Only macOS supported')
elif iswindows: raise Exception('Only macOS supported')
elif iswindows: raise Exception('Only macOS supported. Check pdf2djvu backend for solution.')
elif isbsd: raise Exception('Only macOS supported')
else: raise Exception('Only macOS supported')
self.plugin_prefs['djvudigital']['installed'] = True
Expand All @@ -488,7 +502,7 @@ def cli_install_backend(self, args):
err_info = 'Only Windows supported. Try manual installation and add pdf2djvu to PATH env'
if iswindows:
success, version = install_pdf2djvu(PLUGINNAME, self.plugin_prefs, log=prints)
elif isosx: raise Exception(err_info)
elif isosx: raise Exception(err_info + ' Check djvudigital backend for solution.')
elif islinux: raise Exception(err_info + ' Can work: `sudo apt-get install pdf2djvu` or your distro equivalent.')
elif isbsd: raise Exception(err_info)
else: raise Exception(err_info)
Expand Down Expand Up @@ -755,6 +769,7 @@ def fun_basic_return(result, pages, images):
# Error: A NULL handle was passed, but initialized data was expected.
# It's probably a bug in calibre podofo image_count method:
# https://github.com/kovidgoyal/calibre/blob/master/src/calibre/utils/podofo/doc.cpp#L146
# or PDF file created with errors.
#
# This is not a big concern because raises mostly for heavy image PDFs
images = pdf.image_count()
Expand Down Expand Up @@ -785,7 +800,6 @@ def job_handler(fun):
def wrapper(srcdoc, log=None, abort=None, notifications=None, pages=None,
images=None, cmdflags=None, *args, **kwargs):
"""Wrap around every backend."""
log('In job handler')
# TODO: better notifications
if notifications is None:
notifications = EmptyClass()
Expand Down Expand Up @@ -910,7 +924,7 @@ def pdf2djvu_custom_printing(readout, pages, images):
def pdf2djvu(srcdoc, cmdflags, djvu, preferences):
"""pdf2djvu backend shell command generation"""
raise_if_not_supported(srcdoc, ['pdf'])
pdf2djvu_path, _, _, _ = discover_backend('pdf2djvu', preferences, PLUGINNAME)
pdf2djvu_path, _, _, _ = discover_backend('pdf2djvu', preferences, plugin_dir(PLUGINNAME))
if pdf2djvu_path is None:
raise OSError('pdf2djvu not found')
if djvu is None:
Expand All @@ -926,8 +940,13 @@ def pdf2djvu(srcdoc, cmdflags, djvu, preferences):
def djvudigital(srcdoc, cmdflags, djvu, preferences):
"""djvudigital backend shell command generation"""
raise_if_not_supported(srcdoc, ['pdf', 'ps'])

# DEBUG UNCOMMENT
return ['djvudigital'] + cmdflags + [srcdoc, djvu.name] # command passed to subprocess

#DEBUG COMMENT
# return ['XCOPY', r"C:\tools\bin\test.djvu", str(djvu.name)+'*', r'/Y'] # command passed to subprocess

def c44(srcdoc, cmdflags=[], log=None):
# part of djvulibre, converts jpegs to djvu
# then combine with djvm -c book.djvu pageN.djvu pageN+1.djvu ..
Expand Down
Binary file added djvumaker_1.1.0.zip
Binary file not shown.
35 changes: 22 additions & 13 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import subprocess

from calibre.constants import isosx, iswindows, islinux, isbsd
from calibre.utils.config import config_dir

def create_cli_parser(self_DJVUmaker, PLUGINNAME, PLUGINVER_DOT, REGISTERED_BACKENDS_KEYS):
"""Creates CLI for plugin."""
Expand All @@ -48,8 +49,10 @@ def create_cli_parser(self_DJVUmaker, PLUGINNAME, PLUGINVER_DOT, REGISTERED_BACK
parser.add_argument('-V', '--version', action='version', version='v{}'.format(PLUGINVER_DOT),
help="show plugin's version number and exit")
subparsers = parser.add_subparsers(metavar='command')
parser_test = subparsers.add_parser('test')
parser_test.set_defaults(func=self_DJVUmaker.cli_test)

# DEBUG COMMENT
# parser_test = subparsers.add_parser('test')
# parser_test.set_defaults(func=self_DJVUmaker.cli_test)

parser_backend = subparsers.add_parser('backend', help='Backends handling. See '
'`{}backend --help`'.format(parser.prog))
Expand Down Expand Up @@ -78,7 +81,7 @@ def create_cli_parser(self_DJVUmaker, PLUGINNAME, PLUGINVER_DOT, REGISTERED_BACK
group_postimport = parser_postimport.add_mutually_exclusive_group(required=False)
group_postimport.add_argument('-y', "--yes",
help=("sets plugin to convert PDF files after import"
" (do not work for pdf2djvu)"),
" (sometimes do not work for pdf2djvu)"),
action="store_true")
group_postimport.add_argument('-n', "--no",
help="sets plugin to do not convert PDF files after import (default)",
Expand Down Expand Up @@ -120,8 +123,8 @@ def discover_backend(backend_name, preferences, folder):
it's updates preferences to recognize this issue (set's value of version to None).
Checks:
1. Under djvumaker/{backend_name}-{saved_installed_version}/{backend_name}
2. Under djvumaker/{backend_name}-{other_versions}/{backend_name}
1. Under CALIBRE's_config_dir/plugin/djvumaker/{backend_name}-{saved_installed_version}/{backend_name}
2. Under CALIBRE's_config_dir/plugin/djvumaker/{backend_name}-{other_versions}/{backend_name}
3. Under {backend_name} (it works if backend_name is on PATH ENV)
Return values:
Expand Down Expand Up @@ -173,7 +176,7 @@ def discover_backend(backend_name, preferences, folder):

def create_backend_link(backend_name, version):
#NODOC
return os.path.join(os.path.join('djvumaker', '{}-{}'.format(backend_name, version), backend_name))
return os.path.join(os.path.join(plugin_dir('djvumaker'), '{}-{}'.format(backend_name, version), backend_name))

# TODO:
# class Installer_pdf2djvu(Installer):
Expand All @@ -182,8 +185,9 @@ def create_backend_link(backend_name, version):
def install_pdf2djvu(PLUGINNAME, preferences, log=print):
#NODOC
backend_path, saved_version, installed_version, path_version = discover_backend('pdf2djvu',
preferences, PLUGINNAME)
log("DEBUG: ", (backend_path, saved_version, installed_version, path_version))
preferences, plugin_dir(PLUGINNAME))
# DEBUG COMMENT
# log("DEBUG: ", (backend_path, saved_version, installed_version, path_version))

local_version = None
if saved_version is None and installed_version is None:
Expand Down Expand Up @@ -282,6 +286,8 @@ def gen_tar_url(code):
return r'https://github.com/jwilk/pdf2djvu/releases/download/{}/pdf2djvu-{}.tar.xz'.format(code, code)

# TODO: what with fallback!?! new argument
# TODO: cross import
PLUGINNAME = 'djvumaker'
fallback_version = '0.9.5'
if iswindows:
fallback_arch_url = gen_zip_url(fallback_version)
Expand All @@ -307,16 +313,16 @@ def check_msg(fpath, msg):
)

log('Downloading current version of pdf2djvu...')
if not os.path.isdir('djvumaker'):
os.mkdir('djvumaker')
fpath, msg = urllib.urlretrieve(arch_url, os.path.join('.', 'djvumaker', get_url_basename(arch_url)),
if not os.path.isdir(plugin_dir(PLUGINNAME)):
os.mkdir(plugin_dir(PLUGINNAME))
fpath, msg = urllib.urlretrieve(arch_url, os.path.join(plugin_dir(PLUGINNAME), get_url_basename(arch_url)),
download_progress_bar)
# print() # should progess bar function handle this TODO:
if not check_msg(fpath, msg):
log('Cannot download current version {} from GitHub.'.format(web_version))
if web_version != fallback_version:
log('Trying download version {}...'.format(fallback_version), download_progress_bar)
fpath, msg_fallback = urllib.urlretrieve(fallback_arch_url, os.path.join('.','djvumaker',
fpath, msg_fallback = urllib.urlretrieve(fallback_arch_url, os.path.join(plugin_dir(PLUGINNAME),
get_url_basename(fallback_arch_url)),
download_progress_bar)
# print() # should progess bar function handle this TODO:
Expand Down Expand Up @@ -347,7 +353,7 @@ def unpack_zip_or_tar(PLUGINNAME, fpath, log):
log('Removed downloaded archive')

# Print iterations progress
def printProgressBar(iteration, total, prefix = '', suffix = '', decimals = 1, length = 100, fill = '',
def printProgressBar(iteration, total, prefix = '', suffix = '', decimals = 1, length = 100, fill = '=',
prints=print):
"""
source: http://stackoverflow.com/a/34325723/2351523
Expand Down Expand Up @@ -392,3 +398,6 @@ def inner(fun):
setattr(fun, name, obj)
return fun
return inner

def plugin_dir(plugin_name):
return os.path.join(config_dir, 'plugins', plugin_name)

0 comments on commit fe18b00

Please sign in to comment.