Exemple #1
0
def cdb_run(ctx, rd, which, version):
    try:
        m = module_for_cmd(which)
    except ImportError:
        raise HTTPNotFound('No module named: {}'.format(which))
    if not getattr(m, 'readonly', False):
        ctx.check_for_write_access(rd)
    if getattr(m, 'version', 0) != int(version):
        raise HTTPNotFound(('The module {} is not available in version: {}.'
                           'Make sure the version of calibre used for the'
                            ' server and calibredb match').format(which, version))
    db = get_library_data(ctx, rd, strict_library_id=True)[0]
    if ctx.restriction_for(rd, db):
        raise HTTPForbidden('Cannot use the command-line db interface with a user who has per library restrictions')
    raw = rd.read()
    ct = rd.inheaders.get('Content-Type', all=True)
    try:
        if MSGPACK_MIME in ct:
            args = msgpack_loads(raw)
        elif 'application/json' in ct:
            args = json_loads(raw)
        else:
            raise HTTPBadRequest('Only JSON or msgpack requests are supported')
    except Exception:
        raise HTTPBadRequest('args are not valid encoded data')
    if getattr(m, 'needs_srv_ctx', False):
        args = [ctx] + list(args)
    try:
        result = m.implementation(db, partial(ctx.notify_changes, db.backend.library_path), *args)
    except Exception as err:
        import traceback
        return {'err': as_unicode(err), 'tb': traceback.format_exc()}
    return {'result': result}
Exemple #2
0
def run_cmd(cmd, opts, args, dbctx):
    m = module_for_cmd(cmd)
    if dbctx.is_remote and getattr(m, 'no_remote', False):
        raise SystemExit(_('The {} command is not supported with remote (server based) libraries').format(cmd))
    ret = m.main(opts, args, dbctx)
    # if not dbctx.is_remote and not opts.dont_notify_gui and not getattr(m, 'readonly', False):
    #     send_message()
    return ret
Exemple #3
0
def run_cmd(cmd, opts, args, dbctx):
    m = module_for_cmd(cmd)
    if dbctx.is_remote and getattr(m, 'no_remote', False):
        raise SystemExit(
            _('The {} command is not supported with remote (server based) libraries'
              ).format(cmd))
    ret = m.main(opts, args, dbctx)
    return ret
Exemple #4
0
def run_cmd(cmd, opts, args, dbctx):
    m = module_for_cmd(cmd)
    if dbctx.is_remote and getattr(m, 'no_remote', False):
        raise SystemExit(
            _('The {} command is not supported with remote (server based) libraries'
              ).format(cmd))
    ret = m.main(opts, args, dbctx)
    # if not dbctx.is_remote and not opts.dont_notify_gui and not getattr(m, 'readonly', False):
    #     send_message()
    return ret
Exemple #5
0
 def cmd_option_parser():
     return module_for_cmd(cmd).option_parser(get_parser, args)
Exemple #6
0
 def run(self, name, *args):
     m = module_for_cmd(name)
     if self.is_remote:
         return self.remote_run(name, m, *args)
     return m.implementation(self.db.new_api, None, *args)
Exemple #7
0
 def cmd_option_parser():
     return module_for_cmd(cmd).option_parser(get_parser, args)
Exemple #8
0
 def run(self, name, *args):
     m = module_for_cmd(name)
     if self.is_remote:
         return self.remote_run(name, m, *args)
     return m.implementation(self.db.new_api, None, *args)