Skip to content

Commit

Permalink
add check for update on startup option
Browse files Browse the repository at this point in the history
  • Loading branch information
baskiton committed Jun 2, 2024
1 parent 3a15046 commit 458a1dc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
2 changes: 1 addition & 1 deletion SatsDecoder/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

if __name__ == '__main__':
cp = configparser.ConfigParser()
d = {'main': {},
d = {'main': {'check_update': 1},
'info': {'version': __version__}}
cp.read_dict(d)
cp.read(CONFIG)
Expand Down
38 changes: 26 additions & 12 deletions SatsDecoder/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,10 @@ def __init__(self, config):
self.master.bind('<Control-q>', self.exit)
self.master.bind('<F1>', self.about)

self.check_update_v = tk.IntVar(self, int(config.get('main', 'check_update')))
if self.check_update_v.get():
threading.Thread(target=self.check_updates, args=(self, 0, 0)).start()

# Notebook frame
# self.notebook = ttk.Notebook(self)
self.notebook = utils.DynamicNotebook(self, self.create_new_tab)
Expand Down Expand Up @@ -938,6 +942,7 @@ def exit(self, evt=None):
self.close_df(name, df)

self.config.set('main', 'pos', self.master.winfo_geometry())
self.config.set('main', 'check_update', str(self.check_update_v.get()))
self.config.set('info', 'version', __version__)
self.quit()

Expand Down Expand Up @@ -1045,21 +1050,25 @@ def foo(ss):
btns_frame.columnconfigure((0, 1), weight=1)

upd_btn = ttk.Button(btns_frame, text='Check updates',
command=lambda: threading.Thread(target=self.check_updates, args=(about, btns_frame,)).start())
command=lambda: threading.Thread(target=self.check_updates, args=(about, btns_frame, 1)).start())
upd_btn.grid(column=0, row=0)

ok_btn = ttk.Button(btns_frame, text='Ok', command=lambda: (about.grab_release(), about.destroy()))
ok_btn.grid(column=1, row=0)

check_update_ckb = ttk.Checkbutton(frame, text='Check updates on startup', variable=self.check_update_v)
check_update_ckb.grid(columnspan=2)

about.update()

@staticmethod
def check_updates(about, btns_frame):
def check_updates(parent, label_frame, showerror):
m = re.match(r'([\d.]+).*', __version__)
if m:
v = tuple(map(int, m.group(1).split('.')))
else:
messagebox.showerror(message=f'Invalid version, can\'t be compared: {__version__}')
if showerror:
messagebox.showerror(message=f'Invalid version, can\'t be compared: {__version__}')
return

try:
Expand All @@ -1068,21 +1077,26 @@ def check_updates(about, btns_frame):
headers={'accept': 'application/vnd.github+json'})) as r:
resp = json.load(r)
except urllib.error.URLError as e:
messagebox.showerror(message=str(e))
if showerror:
messagebox.showerror(message=str(e))
return

if v < tuple(map(int, resp['tag_name'].split('.'))):
fg = 'green'
msg = resp['tag_name']
if not label_frame:
if messagebox.askyesno(message=f'New version found: {msg}\nFollow to download link?', parent=parent):
webbrowser.open('https://github.com/baskiton/SatsDecoder/releases/latest')
return
else:
fg = 'red'
msg = 'not found'

for i in btns_frame.winfo_children():
if isinstance(i, ttk.Label):
i.config(text=f'New version: {msg}', foreground=fg)
break
else:
ttk.Label(btns_frame, text=f'New version: {msg}', foreground=fg, justify='center').grid(columnspan=2)

about.update()
if label_frame:
for i in label_frame.winfo_children():
if isinstance(i, ttk.Label):
i.config(text=f'New version: {msg}', foreground=fg)
break
else:
ttk.Label(label_frame, text=f'New version: {msg}', foreground=fg, justify='center').grid(columnspan=2)
parent.update()

0 comments on commit 458a1dc

Please sign in to comment.