Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""LICENSE
2Copyright 2015 Hermann Krumrey <hermann@krumreyh.com>
4This file is part of toktokkie.
6toktokkie is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
11toktokkie is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
16You should have received a copy of the GNU General Public License
17along with toktokkie. If not, see <http://www.gnu.org/licenses/>.
18LICENSE"""
20import os
21from typing import List, Type, Dict, Any
22from puffotter.prompt import yn_prompt
23from toktokkie.metadata.base.Metadata import Metadata
24from toktokkie.utils.update.Updater import Updater
25from toktokkie.utils.update.XDCCUpdater import XDCCUpdater
26from toktokkie.utils.update.MangadexUpdater import MangadexUpdater
27from toktokkie.utils.update.TorrentUpdater import TorrentUpdater
28from toktokkie.exceptions import InvalidUpdateInstructions, \
29 MissingUpdateInstructions
31updaters = [MangadexUpdater, XDCCUpdater, TorrentUpdater]
32"""
33List of all available updaters
34"""
37def perform_update(
38 args: Dict[str, Any],
39 metadata: Metadata,
40 applicable_updaters: List[Type[Updater]]
41):
42 """
43 Performs updates on metadata
44 :param args: The CLI arguments
45 :param metadata: The metadata
46 :param applicable_updaters: The applicable updater classes
47 :return: None
48 """
49 if args["create"]:
50 to_create = None
51 to_delete = []
52 if len(applicable_updaters) > 1:
53 for updater_cls in applicable_updaters:
54 selected = yn_prompt(f"Create update configuration for "
55 f"{updater_cls.name()}?")
56 if selected:
57 if to_create is not None:
58 print("Only one update configuration is allowed")
59 return
60 to_create = updater_cls
61 else:
62 to_delete.append(updater_cls)
63 else:
64 to_create = applicable_updaters[0]
66 if to_create is None:
67 print("No update instructions created")
68 else:
69 to_create.prompt(metadata)
70 for cls in to_delete:
71 update_file = cls.update_file(metadata.directory_path)
72 if os.path.isfile(update_file):
73 os.remove(update_file)
75 else:
76 has_instructions = []
77 for updater_cls in applicable_updaters:
78 if updater_cls.json_schema() is None:
79 has_instructions.append(updater_cls)
80 update_file = updater_cls.update_file(metadata.directory_path)
81 if os.path.isfile(update_file):
82 has_instructions.append(updater_cls)
84 if len(has_instructions) == 0:
85 print("No Update Instructions configured")
86 elif len(has_instructions) > 1:
87 print("More than one update instructions configured")
88 else:
89 try:
90 updater = has_instructions[0](metadata, args)
91 print("Updating {} using {} updater:".format(
92 metadata.name, updater.name()
93 ))
94 updater.update()
95 except MissingUpdateInstructions:
96 print("No update instructions for {}".format(metadata.name))
97 except InvalidUpdateInstructions as e:
98 print(
99 "Update instructions for {} are invalid: {}"
100 .format(metadata.name, e)
101 )