2021-11-27 11:54:42 +00:00
|
|
|
# Copyright: Ren Tatsumoto <tatsu at autistici.org>
|
|
|
|
# License: GNU GPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
2022-12-02 13:53:12 +00:00
|
|
|
import os
|
2021-04-20 10:31:51 +00:00
|
|
|
import sys
|
2022-04-03 14:57:00 +00:00
|
|
|
from typing import Callable
|
2021-04-20 11:14:09 +00:00
|
|
|
from urllib.error import URLError
|
|
|
|
|
2021-11-27 11:54:42 +00:00
|
|
|
from .common import ANTPError
|
2022-12-02 13:53:12 +00:00
|
|
|
from .consts import NOTE_TYPES_DIR
|
2021-11-27 11:54:42 +00:00
|
|
|
from .exporter import export_note_type
|
|
|
|
from .importer import import_note_type
|
2022-12-02 13:31:59 +00:00
|
|
|
from .overwriter import overwrite_note_type
|
2021-12-25 20:17:48 +00:00
|
|
|
from .updater import update_note_type
|
2021-04-20 10:31:51 +00:00
|
|
|
|
|
|
|
|
2022-04-03 14:57:00 +00:00
|
|
|
def wrap_errors(fn: Callable):
|
2021-04-20 10:31:51 +00:00
|
|
|
try:
|
2022-04-03 14:57:00 +00:00
|
|
|
fn()
|
2021-04-20 10:31:51 +00:00
|
|
|
except URLError:
|
|
|
|
print("Couldn't connect. Make sure Anki is open and AnkiConnect is installed.")
|
2021-11-27 11:54:42 +00:00
|
|
|
except ANTPError as ex:
|
2021-04-20 10:31:51 +00:00
|
|
|
print(ex)
|
2021-12-25 20:38:41 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
print("\nAborted.")
|
2021-04-20 10:31:51 +00:00
|
|
|
|
2021-04-20 09:27:48 +00:00
|
|
|
|
2022-04-03 14:57:00 +00:00
|
|
|
def print_help():
|
|
|
|
options = (
|
2022-12-02 13:31:59 +00:00
|
|
|
("import", "Add one of the stored note types to Anki."),
|
|
|
|
("update", "Overwrite a previously imported note type with new data. "
|
|
|
|
"Fields will not be updated."),
|
|
|
|
("overwrite", "Overwrite a note type in Anki with new data from a stored note type. "
|
|
|
|
"Fields will not be updated."),
|
2022-04-03 14:57:00 +00:00
|
|
|
("export", "Save your note type as a template."),
|
2022-12-02 13:53:12 +00:00
|
|
|
("list", "List models stored in the templates folder."),
|
2022-04-03 14:57:00 +00:00
|
|
|
("-v, --verbose", "Show detailed info when errors occur."),
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
"Usage: antp.sh [OPTIONS]\n\n"
|
|
|
|
"Options:"
|
|
|
|
)
|
|
|
|
col_width = [max(len(word) for word in col) + 2 for col in zip(*options)]
|
|
|
|
for row in options:
|
|
|
|
print(" " * 4, "".join(col.ljust(col_width[i]) for i, col in enumerate(row)), sep='')
|
|
|
|
|
|
|
|
|
2022-12-02 13:53:12 +00:00
|
|
|
def list_stored_note_types():
|
|
|
|
print('\n'.join(os.listdir(NOTE_TYPES_DIR)))
|
|
|
|
|
|
|
|
|
2022-04-03 14:57:00 +00:00
|
|
|
def main():
|
|
|
|
if len(sys.argv) < 2:
|
|
|
|
print("No action provided.")
|
|
|
|
print_help()
|
|
|
|
return
|
|
|
|
|
|
|
|
action = None
|
|
|
|
wrap = True
|
|
|
|
|
|
|
|
for arg in sys.argv[1:]:
|
|
|
|
match arg:
|
|
|
|
case 'export':
|
|
|
|
action = export_note_type
|
|
|
|
case 'import':
|
|
|
|
action = import_note_type
|
|
|
|
case 'update':
|
|
|
|
action = update_note_type
|
2022-12-02 13:31:59 +00:00
|
|
|
case 'overwrite':
|
|
|
|
action = overwrite_note_type
|
2022-12-02 13:53:12 +00:00
|
|
|
case 'list':
|
|
|
|
action = list_stored_note_types
|
2022-04-03 14:57:00 +00:00
|
|
|
case '-v' | '--verbose':
|
|
|
|
wrap = False
|
|
|
|
|
|
|
|
if action and wrap:
|
|
|
|
wrap_errors(action)
|
|
|
|
elif action:
|
|
|
|
action()
|
|
|
|
else:
|
|
|
|
print("Unknown action.")
|
|
|
|
print_help()
|
|
|
|
|
|
|
|
|
2021-04-20 10:31:51 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|