def main(self, gallery_or_id: db.Gallery=None, number: int=None, args=tuple()) -> bool: assert isinstance(gallery_or_id, (db.Gallery, int)) if isinstance(gallery_or_id, int): gallery = database_cmd.GetModelItems().run(db.Gallery, {gallery_or_id}) if gallery: gallery = gallery[0] else: gallery = gallery_or_id self.gallery = gallery if number is None: number = 1 opened = False if self.gallery.pages.count(): with self._resolve.call(self.gallery, number) as plg: r = plg.first_or_default() if len(r) == 2: self.path, self.first_file = r args = args if args else tuple(x.strip() for x in config.external_image_viewer_args.value.split()) with self._open.call(self.path, self.first_file, self.gallery, args) as plg: try: opened = plg.first_or_default() except OSError as e: raise exceptions.CommandError(utils.this_command(self), "Failed to open gallery with external viewer: {}".format(e.args[1])) else: log.w("Error opening gallery (), no page count".format(self.gallery.id)) if opened: self._opened.emit(self.path, self.first_file, self.gallery, number) return opened
def main(self, model_name: str) -> db.Base: if not hasattr(db, model_name): raise exceptions.CoreError( utils.this_command(self), "No database model named '{}'".format(model_name)) return getattr(db, model_name)
def main(self, model: db.Base, item_id: int, image_size: enums.ImageSize) -> db.Profile: self.model = model if image_size == enums.ImageSize.Original: image_size = utils.ImageSize(0, 0) else: image_size = utils.ImageSize( *constants.image_sizes[image_size.name.lower()]) with self.models.call() as plg: for p in plg.all(default=True): self._supported_models.update(p) if self.model not in self._supported_models: raise exceptions.CommandError( utils.this_command(self), "Model '{}' is not supported".format(model)) img_hash = io_cmd.ImageItem.gen_hash(model, image_size, item_id) generate = True sess = constants.db_session() profile_size = str(tuple(image_size)) self.cover = sess.query(db.Profile).filter( db.and_op(db.Profile.data == img_hash, db.Profile.size == profile_size)).first() old_img_hash = None if self.cover: if io_cmd.CoreFS(self.cover.path).exists: generate = False else: old_img_hash = self.cover.data self.next_progress() if not generate: model_name = db.model_name(model) with self.invalidate.call_capture(model_name, model_name, item_id, image_size) as plg: if plg.first_or_default(): generate = True self.next_progress() if generate: constants.task_command.thumbnail_cleaner.wake_up() self.cover = self.run_native(self._generate_and_add, img_hash, old_img_hash, generate, model, item_id, image_size, profile_size).get() self.cover_event.emit(self.cover) return self.cover
def main(self, model: db.Base, term: str, match_options: dict = {}) -> typing.Set[int]: self.model = model model_name = db.model_name(self.model) self.term = term with self.models.call() as plg: for p in plg.all(default=True): self._supported_models.update(p) if self.model not in self._supported_models: raise exceptions.CommandError( utils.this_command(self), "Model '{}' is not supported".format(model)) options = get_search_options(match_options) log.d("Match options", options) related_models = db.related_classes(model) sess = constants.db_session() model_count = sess.query(model).count() with self.match_model.call_capture(model_name, model_name, model_name, self.term, options) as plg: for i in plg.all_or_default(): self.matched_ids.update(i) if len(self.matched_ids) == model_count: break has_all = False for m in related_models: if m in self._supported_models: with self.match_model.call_capture(db.model_name(m), model_name, db.model_name(m), self.term, options) as plg: for i in plg.all_or_default(): self.matched_ids.update(i) if len(self.matched_ids) == model_count: has_all = True break if has_all: break self.matched.emit(self.matched_ids) return self.matched_ids
def main(self, model: db.Base, item_id: int, image_size: enums.ImageSize) -> db.Profile: self.model = model if image_size == enums.ImageSize.Original: image_size = utils.ImageSize(0, 0) else: image_size = utils.ImageSize( *constants.image_sizes[image_size.name.lower()]) with self.models.call() as plg: for p in plg.all(default=True): self._supported_models.update(p) if self.model not in self._supported_models: raise exceptions.CommandError( utils.this_command(self), "Model '{}' is not supported".format(model)) img_hash = io_cmd.ImageItem.gen_hash(model, image_size, item_id) generate = True sess = constants.db_session() self.cover = sess.query( db.Profile).filter(db.Profile.data == img_hash).one_or_none() if self.cover: if io_cmd.CoreFS(self.cover.path).exists: generate = False else: self.cover = db.Profile() if generate: model_name = db.model_name(model) with self.generate.call_capture(model_name, model_name, item_id, image_size) as plg: self.cover.path = plg.first() self.cover.data = img_hash self.cover.size = str(tuple(image_size)) if self.cover.path and generate: i = GetModelItemByID().run(model, {item_id})[0] i.profiles.append(self.cover) sess.commit() elif not self.cover.path: self.cover = None self.cover_event.emit(self.cover) return self.cover
def main(self, model_name: str) -> db.Base: e = False try: if not hasattr(db, model_name) or not issubclass( getattr(db, model_name), db.Base): e = True except TypeError: e = True if e: raise exceptions.CoreError( utils.this_command(self), "No database model named '{}'".format(model_name)) return getattr(db, model_name)
def main(self, model: db.Base, item_id: int, image_size: enums.ImageSize) -> db.Profile: self.model = model if image_size == enums.ImageSize.Original: image_size = utils.ImageSize(0, 0) else: image_size = utils.ImageSize( *constants.image_sizes[image_size.name.lower()]) with self.models.call() as plg: for p in plg.all(default=True): self._supported_models.update(p) if self.model not in self._supported_models: raise exceptions.CommandError( utils.this_command(self), "Model '{}' is not supported".format(model)) img_hash = io_cmd.ImageItem.gen_hash(model, image_size, item_id) cover_path = "" generate = True sess = constants.db_session() self.cover = sess.query( db.Profile).filter(db.Profile.data == img_hash).one_or_none() if self.cover: if io_cmd.CoreFS(self.cover.path).exists: generate = False else: cover_path = self.cover.path if generate: self.cover = self.run_native(self._generate_and_add, img_hash, generate, cover_path, model, item_id, image_size).get() self.cover_event.emit(self.cover) return self.cover