Skip to content

Commit ccd43af

Browse files
committed
Improve --version
1 parent c636729 commit ccd43af

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

manga.py

+21-7
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,35 @@ def set_args():
7575
parser.add_argument("--fullsize", action='store_true', help="Do not stretch images to the profile's device resolution")
7676
parser.add_argument("--cache", action='store_true', help="Avoid downloading chapters and use already downloaded chapters instead (offline)")
7777
parser.add_argument("--remove-alpha", action='store_true', help="When converting to PDF remove alpha channel on images using ImageMagick Wand")
78-
parser.add_argument("--version", "-v", action='version', help="Display current InMangaKindle version", version=version())
78+
parser.add_argument("--version", "-v", action=CheckVersion, help="Display current InMangaKindle version", version=VERSION)
7979
args = parser.parse_args()
8080

81-
def version():
82-
return f'{Style.BRIGHT}%(prog)s {Fore.CYAN}{VERSION}{Style.RESET_ALL}'
81+
class CheckVersion(argparse.Action):
82+
def __init__(self, option_strings, version=VERSION, **kwargs):
83+
super(CheckVersion, self).__init__(option_strings, nargs=0, **kwargs)
84+
self.version = version
85+
def __call__(self, parser, namespace, values, option_string=None):
86+
if not is_python_version_supported():
87+
print_colored(python_not_supported(), Fore.RED)
88+
print_colored(NAME, Style.BRIGHT, end=' ')
89+
print_colored(self.version, Style.BRIGHT, Fore.CYAN)
90+
if check_version():
91+
print_colored('✅ Up to date', Fore.GREEN)
92+
exit()
8393

8494
def check_version():
85-
latest_version = VERSION
95+
latest_version = None
8696
try:
8797
response = requests.get(f'https://api.github.com/repos/Carleslc/{NAME}/releases/latest')
88-
latest_version = load_json(response.content, 'tag_name')
8998
html_url = load_json(response.content, 'html_url')
99+
latest_version = load_json(response.content, 'tag_name')
90100
except:
91101
if not args.cache:
92102
print_dim(f'Cannot check for updates. Version: {VERSION}', Fore.YELLOW)
93-
if latest_version != VERSION:
103+
if latest_version is None:
104+
return False
105+
is_updated = latest_version == VERSION
106+
if not is_updated:
94107
print_colored(f'New version is available! {VERSION} -> {latest_version}', Style.BRIGHT, Fore.GREEN)
95108
print_colored(f'Upgrade to the latest version: {html_url}', Fore.GREEN)
96109
if os.path.isdir('.git'):
@@ -103,6 +116,7 @@ def check_version():
103116
except:
104117
print('If you want to update later manually use ', end='')
105118
print_colored(f'git fetch && git checkout {latest_version}', Fore.YELLOW)
119+
return is_updated
106120

107121
def is_python_version_supported():
108122
min_version, max_version = SUPPORT_PYTHON
@@ -115,7 +129,7 @@ def python_not_supported():
115129
min_version, max_version = SUPPORT_PYTHON
116130
min_version = '.'.join(map(str, min_version))
117131
max_version = '.'.join(map(str, max_version))
118-
return f'Your Python version {platform.python_version()} is not supported ({sys.executable} --version). Please, use a Python version between {min_version} and {max_version}\n{RECOMMENDED_PYTHON}'
132+
return f'Your Python version {platform.python_version()} is not fully supported ({sys.executable} --version). Please, use a Python version between {min_version} and {max_version}\n{RECOMMENDED_PYTHON}'
119133

120134
def print_colored(message, *colors, end='\n'):
121135
def printnoln(s):

0 commit comments

Comments
 (0)