Coverage for odmpy/cli_utils.py: 90.5%
42 statements
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-14 08:51 +0000
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-14 08:51 +0000
1# Copyright (C) 2023 github.com/ping
2#
3# This file is part of odmpy.
4#
5# odmpy is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# odmpy is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with odmpy. If not, see <http://www.gnu.org/licenses/>.
17#
18import argparse
19from enum import Enum
20from typing import Tuple
23#
24# Stuff for the CLI
25#
28DEFAULT_FORMAT_FIELDS = ("Title", "Author", "Series", "ReadingOrder", "Edition", "ID")
31class OdmpyCommands(str, Enum):
32 """
33 Command strings
34 """
36 Information = "info"
37 Download = "dl"
38 Return = "ret"
39 Libby = "libby"
40 LibbyReturn = "libbyreturn"
41 LibbyRenew = "libbyrenew"
43 def __str__(self):
44 return str(self.value)
46 def __repr__(self):
47 # to ensure that proper values are printed out in arg command_name error help
48 return str(self.value)
51class OdmpyNoninteractiveOptions(str, Enum):
52 """
53 Non-interactive arguments
54 """
56 DownloadLatestN = "download_latest_n"
57 DownloadSelectedN = "selected_loans_indices"
58 DownloadSelectedId = "selected_loans_ids"
59 ExportLoans = "export_loans_path"
60 Check = "check_signed_in"
62 def __str__(self):
63 return str(self.value)
66def positive_int(value: str) -> int:
67 """
68 Ensure that argument is a positive integer
70 :param value:
71 :return:
72 """
73 try:
74 int_value = int(value)
75 except ValueError:
76 raise argparse.ArgumentTypeError(f'"{value}" is not a positive integer value')
77 if int_value <= 0:
78 raise argparse.ArgumentTypeError(f'"{value}" is not a positive integer value')
79 return int_value
82def valid_book_folder_file_format(
83 value: str,
84 fields: Tuple = DEFAULT_FORMAT_FIELDS,
85) -> str:
86 """
87 Ensure that the book folder format is valid
89 :param value:
90 :param fields:
91 :return:
92 """
93 values_dict = {}
94 for f in fields:
95 values_dict[f] = ""
96 try:
97 value % values_dict
98 except KeyError as err:
99 raise argparse.ArgumentTypeError(
100 f'"{value}" is not a valid book folder/file name format: Invalid field {err}'
101 ) from err
102 except Exception as err:
103 raise argparse.ArgumentTypeError(
104 f'"{value}" is not a valid book folder/file name format: {err}'
105 ) from err
106 return value