Skip to content

Commit

Permalink
Added ask_user_input to __init.py__
Browse files Browse the repository at this point in the history
  • Loading branch information
pirtim committed Mar 27, 2017
1 parent efffdcc commit b06fe15
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
39 changes: 22 additions & 17 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from calibre.utils.config import JSONConfig
from calibre.utils.podofo import get_podofo
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
from calibre_plugins.djvumaker.utils import create_backend_link, create_cli_parser, install_pdf2djvu, discover_backend, ask_yesno_input

# if iswindows and hasattr(sys, 'frozen'):
# # CREATE_NO_WINDOW=0x08 so that no ugly console is popped up
Expand All @@ -51,6 +51,10 @@ def empty_function(*args, **kwargs):
else:
printsd = empty_function

# LEFT TODO: 1. postimport if file dropped to library or through calibredb add
# LEFT TODO: 2. postimport if convert all
# LEFT TODO: 3. testing and documentation

# -- Calibre Plugin class --
class DJVUmaker(FileTypePlugin, InterfaceActionBase): # multiple inheritance for gui hooks!
name = PLUGINNAME # Name of the plugin
Expand Down Expand Up @@ -214,11 +218,12 @@ def cli_convert(self, args):
# `calibre-debug -r djvumaker -- convert --all`
printsd('in cli convert_all')
# TODO: make work `djvumaker -- convert --all`
raise NotImplementedError
raise NotImplementedError('Convert all is not implemented.')

user_input = ask_yesno_input('Do you wany to copy-convert all PDFs to DJVU?')
if not user_input:
return None

# TODO: Use humaninput from utils.py
prints("Press Enter to copy-convert all PDFs to DJVU, or CTRL+C to abort...")
raw_input('')
from calibre.library import db
from calibre.customize.ui import run_plugins_on_postimport
db = db() # initialize calibre library database
Expand All @@ -231,27 +236,27 @@ def cli_convert(self, args):
db.run_plugins_on_postimport(book_id, 'pdf')
continue
elif args.path is not None:
printsd('path')
# raise NotImplementedError
# `calibre-debug -r djvumaker -- convert -p test.pdf` -> tempfile(test.djvu)
printsd('in path')
if is_rasterbook(args.path):
# `calibre-debug -r djvumaker -- convert -p test.pdf` -> tempfile(test.djvu)
djvu = self.run_backend(args.path, log=prints)

djvu = self.run_backend(args.path, log=self.prints.func)
if djvu:
prints(("\n\nopening djvused in subshell, press Ctrl+D to exit and"
"delete '%s'\n\n") % djvu)
input_filename, _ = os.path.splitext(args.path)
shutil.copy2(djvu, input_filename + '.djvu')
prints("Finished DJVU outputed to: {}.".format(input_filename + '.djvu'))

user_input = ask_yesno_input('Do you want to open djvused in subshell? (may not work on not macOS)')
if not user_input:
return None
# de-munge the tty
sys.stdin = sys.__stdin__
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
os.system("stat '%s'" % djvu)
# TODO: doesn't work on Windows
# TODO: doesn't work on Windows, why is it here?
os.system("djvused -e dump '%s'" % djvu)
os.system("djvused -v '%s'" % djvu)
input_filename, _ = os.path.splitext(args.path)
shutil.copy2(djvu, input_filename + '.djvu')
if not isosx:
prints("Ready DJVU outputed to: {}.".format(input_filename + '.djvu'))

elif args.id is not None:
# `calibre-debug -r djvumaker -- convert -i 123 #id(123).pdf` -> tempfile(id(123).djvu)
printsd('in convert by id')
Expand Down
5 changes: 3 additions & 2 deletions gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ def _tjob_djvu_convert(self, db, book_id, fpath, ftype, abort, log, notification
find_plugin('djvumaker').postimport(book_id, ftype, db, log, fork_job=False,
abort=abort, notifications=notifications)
elif fpath:
# TODO: unknow keywords?
raise NotImplementedError
# TODO: proper english
raise NotImplementedError('Connot convert book outside of library. Add book to your library and then start convert.')
# TODO: Add ability to convert on devices; unknow keywords `path` and `flags`?
# find_plugin('djvumaker').djvudigital(path, flags, None)

def _tjob_refresh_books(self, job):
Expand Down
4 changes: 2 additions & 2 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,8 @@ def printProgressBar(iteration, total, prefix = '', suffix = '', decimals = 1, l
def ask_yesno_input(question, prints=print):
'''Ask user for yes/no input. Loops if other answer.'''
while True:
prints(question + ' (y/n)')
user_input = raw_input().lower()
prints('\n\t'+ question + ' (y/n)\n')
user_input = raw_input().strip().lower()
if user_input == 'y':
return True
elif user_input == 'n':
Expand Down

0 comments on commit b06fe15

Please sign in to comment.