mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-08 10:44:09 -04:00
Port calibredb catalog
This commit is contained in:
parent
a376bc6b7d
commit
2e4100e8dd
@ -4,19 +4,127 @@
|
|||||||
|
|
||||||
from __future__ import absolute_import, division, print_function, unicode_literals
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
readonly = False
|
import os
|
||||||
|
|
||||||
|
from calibre.customize.ui import available_catalog_formats, plugin_for_catalog_format
|
||||||
|
from calibre.db.cli import integers_from_string
|
||||||
|
|
||||||
|
readonly = True
|
||||||
version = 0 # change this if you change signature of implementation()
|
version = 0 # change this if you change signature of implementation()
|
||||||
|
needs_srv_ctx = True
|
||||||
|
|
||||||
|
|
||||||
def implementation(db, notify_changes, *args):
|
def implementation(db, notify_changes, ctx):
|
||||||
is_remote = notify_changes is not None
|
raise NotImplementedError()
|
||||||
is_remote
|
|
||||||
|
|
||||||
|
|
||||||
def option_parser(get_parser, args):
|
def option_parser(get_parser, args): # {{{
|
||||||
pass
|
|
||||||
|
def add_plugin_parser_options(fmt, parser):
|
||||||
|
# Fetch the extension-specific CLI options from the plugin
|
||||||
|
# library.catalogs.<format>.py
|
||||||
|
plugin = plugin_for_catalog_format(fmt)
|
||||||
|
for option in plugin.cli_options:
|
||||||
|
if option.action:
|
||||||
|
parser.add_option(
|
||||||
|
option.option,
|
||||||
|
default=option.default,
|
||||||
|
dest=option.dest,
|
||||||
|
action=option.action,
|
||||||
|
help=option.help
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
parser.add_option(
|
||||||
|
option.option,
|
||||||
|
default=option.default,
|
||||||
|
dest=option.dest,
|
||||||
|
help=option.help
|
||||||
|
)
|
||||||
|
|
||||||
|
# Entry point
|
||||||
|
parser = get_parser(
|
||||||
|
_(
|
||||||
|
'''\
|
||||||
|
%prog catalog /path/to/destination.(csv|epub|mobi|xml...) [options]
|
||||||
|
|
||||||
|
Export a catalog in format specified by path/to/destination extension.
|
||||||
|
Options control how entries are displayed in the generated catalog output.
|
||||||
|
'''
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Add options common to all catalog plugins
|
||||||
|
parser.add_option(
|
||||||
|
'-i',
|
||||||
|
'--ids',
|
||||||
|
default=None,
|
||||||
|
dest='ids',
|
||||||
|
help=_(
|
||||||
|
"Comma-separated list of database IDs to catalog.\n"
|
||||||
|
"If declared, --search is ignored.\n"
|
||||||
|
"Default: all"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
parser.add_option(
|
||||||
|
'-s',
|
||||||
|
'--search',
|
||||||
|
default=None,
|
||||||
|
dest='search_text',
|
||||||
|
help=_(
|
||||||
|
"Filter the results by the search query. "
|
||||||
|
"For the format of the search query, please see "
|
||||||
|
"the search-related documentation in the User Manual.\n"
|
||||||
|
"Default: no filtering"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
parser.add_option(
|
||||||
|
'-v',
|
||||||
|
'--verbose',
|
||||||
|
default=False,
|
||||||
|
action='store_true',
|
||||||
|
dest='verbose',
|
||||||
|
help=_('Show detailed output information. Useful for debugging')
|
||||||
|
)
|
||||||
|
fmt = 'epub'
|
||||||
|
if args and '.' in args[0]:
|
||||||
|
fmt = args[0].rpartition('.')[-1].lower()
|
||||||
|
if fmt not in available_catalog_formats():
|
||||||
|
fmt = 'epub'
|
||||||
|
|
||||||
|
# Add options specific to fmt plugin
|
||||||
|
add_plugin_parser_options(fmt, parser)
|
||||||
|
|
||||||
|
return parser
|
||||||
|
|
||||||
|
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
def main(opts, args, dbctx):
|
def main(opts, args, dbctx):
|
||||||
raise NotImplementedError('TODO: implement this')
|
if len(args) < 1:
|
||||||
|
raise SystemExit(_('You must specify a catalog output file'))
|
||||||
|
if dbctx.is_remote:
|
||||||
|
raise SystemExit(_('Generating catalogs from server libraries is not supported'))
|
||||||
|
if opts.ids:
|
||||||
|
opts.ids = list(integers_from_string(opts.ids))
|
||||||
|
fmt = args[0].rpartition('.')[-1]
|
||||||
|
if fmt not in available_catalog_formats():
|
||||||
|
raise SystemExit(
|
||||||
|
_('Cannot generate a catalog in the {} format').format(fmt.upper())
|
||||||
|
)
|
||||||
|
|
||||||
|
# No support for connected device in CLI environment
|
||||||
|
# Parallel initialization in calibre.gui2.tools:generate_catalog()
|
||||||
|
opts.connected_device = {
|
||||||
|
'is_device_connected': False,
|
||||||
|
'kind': None,
|
||||||
|
'name': None,
|
||||||
|
'save_template': None,
|
||||||
|
'serial': None,
|
||||||
|
'storage': None,
|
||||||
|
}
|
||||||
|
dest = os.path.abspath(os.path.expanduser(args[0]))
|
||||||
|
plugin = plugin_for_catalog_format(fmt)
|
||||||
|
with plugin:
|
||||||
|
plugin.run(dest, opts, dbctx.db)
|
||||||
return 0
|
return 0
|
||||||
|
@ -239,7 +239,7 @@ def main(args=sys.argv):
|
|||||||
parser.print_help()
|
parser.print_help()
|
||||||
return 1
|
return 1
|
||||||
del args[i]
|
del args[i]
|
||||||
parser = option_parser_for(cmd, args)()
|
parser = option_parser_for(cmd, args[1:])()
|
||||||
opts, args = parser.parse_args(args)
|
opts, args = parser.parse_args(args)
|
||||||
return run_cmd(cmd, opts, args[1:], DBCtx(opts))
|
return run_cmd(cmd, opts, args[1:], DBCtx(opts))
|
||||||
|
|
||||||
|
@ -36,6 +36,8 @@ def cdb_run(ctx, rd, which, version):
|
|||||||
except Exception:
|
except Exception:
|
||||||
raise HTTPBadRequest('args are not valid encoded data')
|
raise HTTPBadRequest('args are not valid encoded data')
|
||||||
db = get_library_data(ctx, rd, strict_library_id=True)[0]
|
db = get_library_data(ctx, rd, strict_library_id=True)[0]
|
||||||
|
if getattr(m, 'needs_srv_ctx', False):
|
||||||
|
args = [ctx] + list(args)
|
||||||
try:
|
try:
|
||||||
result = m.implementation(db, ctx.notify_changes, *args)
|
result = m.implementation(db, ctx.notify_changes, *args)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user