Add type annotations

This commit adds type annotations to main.py and fixes typos as well.

It also adds a FIXME to check if we really should add a new line at
end of password
main
Micke Nordin 4 years ago
parent 602ff241ad
commit fd49fc8111
Signed by: micke
GPG Key ID: 014B273D614BE877

@ -3,6 +3,7 @@
A mobile first interface for the standard unix password manager written in python A mobile first interface for the standard unix password manager written in python
""" """
import os import os
from typing import Union
import wx import wx
import wx.lib.scrolledpanel as scrolled import wx.lib.scrolledpanel as scrolled
@ -28,6 +29,7 @@ class PassUi(wx.Frame):
""" """
self.sizer.Clear(delete_windows=True) self.sizer.Clear(delete_windows=True)
func(self, *wrapper_args) func(self, *wrapper_args)
self.pnl.SetupScrolling()
self.sizer.Layout() self.sizer.Layout()
return wrapper return wrapper
@ -39,12 +41,12 @@ class PassUi(wx.Frame):
:param kw: :param kw:
""" """
super().__init__(*args, **kw) super().__init__(*args, **kw)
self.pass_handler = Pass() self.pass_handler: Pass = Pass()
# create a panel in the frame # create a panel in the frame
self.pnl = scrolled.ScrolledPanel(self, -1, style=wx.VSCROLL) self.pnl: wx.lib.scrolledpanel.ScrolledPanel = scrolled.ScrolledPanel(self, -1, style=wx.VSCROLL)
self.pnl.SetupScrolling() self.pnl.SetupScrolling()
# and create a sizer to manage the layout of child widgets # and create a sizer to manage the layout of child widgets
self.sizer = wx.BoxSizer(wx.VERTICAL) self.sizer: wx.BoxSizer = wx.BoxSizer(wx.VERTICAL)
self.pnl.SetSizer(self.sizer) self.pnl.SetSizer(self.sizer)
self.add_buttons() self.add_buttons()
@ -53,57 +55,56 @@ class PassUi(wx.Frame):
"""add_buttons.""" """add_buttons."""
self.add_tools() self.add_tools()
if self.pass_handler.cur_dir != self.pass_handler.top_dir: if self.pass_handler.cur_dir != self.pass_handler.top_dir:
btn = self.make_back_button() btn: wx.Button = self.make_back_button()
self.sizer.Add(btn, 0, wx.EXPAND) # pylint: disable=no-member self.sizer.Add(btn, 0, wx.EXPAND) # pylint: disable=no-member
self.Bind(wx.EVT_BUTTON, self.Bind(wx.EVT_BUTTON,
lambda event: self.path_button_clicked(), btn) lambda event: self.path_button_clicked(), btn)
index = 0 index: int = 0
for cpath in self.pass_handler.cur_paths: for c_path in self.pass_handler.cur_paths:
if cpath != self.pass_handler.cur_dir: if c_path != self.pass_handler.cur_dir:
label = '🗀 ' + os.path.basename(os.path.normpath(cpath)) label: str = '🗀 ' + os.path.basename(os.path.normpath(c_path))
btn = wx.Button(self.pnl, label=label) btn: wx.Button = wx.Button(self.pnl, label=label)
self.sizer.Add(btn, 0, wx.EXPAND) # pylint: disable=no-member self.sizer.Add(btn, 0, wx.EXPAND) # pylint: disable=no-member
self.Bind(wx.EVT_BUTTON, self.Bind(wx.EVT_BUTTON,
lambda event, path=cpath: self.path_button_clicked( lambda event, path=c_path: self.path_button_clicked(
path), path),
btn) btn)
index = index + 1 index = index + 1
index = 0 index = 0
for password in self.pass_handler.cur_passwords: for password in self.pass_handler.cur_passwords:
label = '🗝 ' + os.path.splitext( label: str = '🗝 ' + os.path.splitext(
os.path.basename(os.path.normpath(password)))[0] os.path.basename(os.path.normpath(password)))[0]
btn = wx.Button(self.pnl, label=label) btn: wx.Button = wx.Button(self.pnl, label=label)
self.sizer.Add(btn, 0, wx.EXPAND) # pylint: disable=no-member self.sizer.Add(btn, 0, wx.EXPAND) # pylint: disable=no-member
self.Bind(wx.EVT_BUTTON, self.Bind(wx.EVT_BUTTON,
lambda event, mindex=index: self.password_button_clicked( lambda event, m_index=index: self.password_button_clicked(
mindex), m_index),
btn) btn)
index = index + 1 index = index + 1
self.pnl.SetupScrolling()
def add_push_pull(self): def add_push_pull(self):
"""add_push_pull.""" """add_push_pull."""
pushbtn = wx.Button(self.pnl, label="Push to remote") push_btn: wx.Button = wx.Button(self.pnl, label="Push to remote")
self.sizer.Add(pushbtn, 0, wx.EXPAND) # pylint: disable=no-member self.sizer.Add(push_btn, 0, wx.EXPAND) # pylint: disable=no-member
self.Bind(wx.EVT_BUTTON, lambda event: pass_push(), pushbtn) self.Bind(wx.EVT_BUTTON, lambda event: pass_push(), push_btn)
pullbtn = wx.Button(self.pnl, label="Pull from remote") pull_btn: wx.Button = wx.Button(self.pnl, label="Pull from remote")
self.sizer.Add(pullbtn, 0, wx.EXPAND) # pylint: disable=no-member self.sizer.Add(pull_btn, 0, wx.EXPAND) # pylint: disable=no-member
self.Bind(wx.EVT_BUTTON, lambda event: pass_pull(), pullbtn) self.Bind(wx.EVT_BUTTON, lambda event: pass_pull(), pull_btn)
def add_tools(self, index=None): def add_tools(self, index: Union[None, int] = None):
"""add_tools. """add_tools.
:param index: :param index:
""" """
btn = wx.Button(self.pnl, label="Show tools") btn: wx.Button = wx.Button(self.pnl, label="Show tools")
font = btn.GetFont().MakeBold() font: wx.Font = btn.GetFont().MakeBold()
btn.SetFont(font) btn.SetFont(font)
self.sizer.Add(btn, 0, wx.EXPAND) # pylint: disable=no-member self.sizer.Add(btn, 0, wx.EXPAND) # pylint: disable=no-member
self.Bind(wx.EVT_BUTTON, self.Bind(wx.EVT_BUTTON,
lambda event, mindex=index: self.show_tools(mindex), lambda event, m_index=index: self.show_tools(m_index),
btn) btn)
def back_button_clicked(self, index=None): def back_button_clicked(self, index: Union[None, int] = None):
"""back_button_clicked. """back_button_clicked.
:param index: :param index:
@ -113,45 +114,45 @@ class PassUi(wx.Frame):
else: else:
self.add_buttons() self.add_buttons()
def delete_password(self, index): def delete_password(self, index: int):
"""delete_password. """delete_password.
:param index: :param index:
""" """
path = self.pass_handler.get_pass_path_from_index(index, "password") path: str = self.pass_handler.get_pass_path_from_index(index, "password")
dlg = wx.MessageDialog(self.pnl, dlg: wx.MessageDialog = wx.MessageDialog(self.pnl,
"Delete " + path + "?", "Delete " + path + "?",
"Are you sure?", "Are you sure?",
style=wx.CANCEL | wx.CANCEL_DEFAULT | wx.OK) style=wx.CANCEL | wx.CANCEL_DEFAULT | wx.OK)
dlg.SetOKCancelLabels("&Yes", "&Don't delete") dlg.SetOKCancelLabels("&Yes", "&Don't delete")
reply = dlg.ShowModal() reply: int = dlg.ShowModal()
if reply == wx.ID_CANCEL: if reply == wx.ID_CANCEL:
return return
self.pass_handler.delete_password(path) self.pass_handler.delete_password(path)
self.back_button_clicked() self.back_button_clicked()
def make_back_button(self, index=None): def make_back_button(self, index: Union[None, int] = None) -> wx.Button:
"""make_back_button. """make_back_button.
:param index: :param index:
""" """
if index is not None: if index is not None:
label = self.pass_handler.get_pass_path_from_index(index, "password") label: str = self.pass_handler.get_pass_path_from_index(index, "password")
else: else:
label = self.pass_handler.cur_dir.replace(self.pass_handler.top_dir, '') label: str = self.pass_handler.cur_dir.replace(self.pass_handler.top_dir, '')
btn = wx.Button(self.pnl, label=label + '') btn: wx.Button = wx.Button(self.pnl, label=label + '')
font = btn.GetFont().MakeItalic().MakeBold() font: wx.Font = btn.GetFont().MakeItalic().MakeBold()
btn.SetFont(font) btn.SetFont(font)
return btn return btn
def password_button_clicked(self, index): def password_button_clicked(self, index: int):
"""password_button_clicked. """password_button_clicked.
:param index: :param index:
""" """
self.show_password_dialog(index) self.show_password_dialog(index)
def path_button_clicked(self, path=None): def path_button_clicked(self, path: Union[None, str] = None):
"""path_button_clicked. """path_button_clicked.
:param path: :param path:
@ -163,42 +164,42 @@ class PassUi(wx.Frame):
self.pass_handler.cur_passwords = self.pass_handler.get_pass_passwords() self.pass_handler.cur_passwords = self.pass_handler.get_pass_passwords()
self.add_buttons() self.add_buttons()
def save_to_pass(self, path, text, name=None): def save_to_pass(self, path: str, text: wx.TextCtrl, name: Union[None, wx.TextCtrl] = None):
"""save_to_pass. """save_to_pass.
:param path: :param path:
:param text: :param text:
:param name: :param name:
""" """
fullpath = os.path.dirname(self.pass_handler.top_dir + '/' + path.lstrip('/')) full_path: str = os.path.dirname(self.pass_handler.top_dir + '/' + path.lstrip('/'))
if name is not None: if name is not None:
path = name.GetLineText(0) path: str = name.GetLineText(0)
fullpath = os.path.dirname(self.pass_handler.top_dir + '/' + path.lstrip('/')) full_path: str = os.path.dirname(self.pass_handler.top_dir + '/' + path.lstrip('/'))
filename = fullpath + '.gpg' filename: str = full_path + '.gpg'
if os.path.exists(fullpath) or os.path.exists(filename): if os.path.exists(full_path) or os.path.exists(filename):
dlg = wx.MessageDialog( dlg: wx.MessageDialog = wx.MessageDialog(
self.pnl, self.pnl,
"Path: " + path + "Path: " + path +
" allready exists! Please update password path", " already exists! Please update password path",
"Error", "Error",
style=wx.OK) style=wx.OK)
dlg.ShowModal() dlg.ShowModal()
reply = wx.ID_CANCEL reply: int = wx.ID_CANCEL
else: else:
dlg = wx.MessageDialog(self.pnl, dlg: wx.MessageDialog = wx.MessageDialog(self.pnl,
"Save to " + path + "?", "Save to " + path + "?",
"Are you sure?", "Are you sure?",
style=wx.CANCEL | wx.CANCEL_DEFAULT | wx.OK) style=wx.CANCEL | wx.CANCEL_DEFAULT | wx.OK)
dlg.SetOKCancelLabels("&Yes", "&Don't save") dlg.SetOKCancelLabels("&Yes", "&Don't save")
reply = dlg.ShowModal() reply: int = dlg.ShowModal()
if reply == wx.ID_CANCEL: if reply == wx.ID_CANCEL:
return return
password = str() password: str = str()
for lineno in range(text.GetNumberOfLines()): for line_no in range(text.GetNumberOfLines()):
password += text.GetLineText(lineno) password += text.GetLineText(line_no)
password += '\n' password += '\n' # FIXME: Is this right? Maybe we break stuff with the trailing newline?
self.pass_handler.save_to_pass(password, path, fullpath) self.pass_handler.save_to_pass(password, path, full_path)
self.back_button_clicked() self.back_button_clicked()
@redraw @redraw
@ -206,134 +207,130 @@ class PassUi(wx.Frame):
"""show_new_dialog. """show_new_dialog.
""" """
passpath = self.pass_handler.cur_dir.replace(self.pass_handler.top_dir, '') pass_path: str = self.pass_handler.cur_dir.replace(self.pass_handler.top_dir, '')
btn = self.make_back_button() btn: wx.Button = self.make_back_button()
self.sizer.Add(btn, 0, wx.EXPAND) # pylint: disable=no-member self.sizer.Add(btn, 0, wx.EXPAND) # pylint: disable=no-member
self.Bind(wx.EVT_BUTTON, self.Bind(wx.EVT_BUTTON,
lambda event, path=self.pass_handler.cur_dir: self.path_button_clicked( lambda event, path=self.pass_handler.cur_dir: self.path_button_clicked(
path), path),
btn) btn)
name_sizer = wx.BoxSizer(orient=wx.VERTICAL) # pylint: disable=no-member name_sizer: wx.BoxSizer = wx.BoxSizer(orient=wx.VERTICAL) # pylint: disable=no-member
name_sizer.Add(wx.StaticText(self.pnl, -1, "Password path:"), 0, name_sizer.Add(wx.StaticText(self.pnl, -1, "Password path:"), 0,
wx.EXPAND) # pylint: disable=no-member wx.EXPAND) # pylint: disable=no-member
pw_sizer = wx.BoxSizer(orient=wx.VERTICAL) # pylint: disable=no-member pw_sizer: wx.BoxSizer = wx.BoxSizer(orient=wx.VERTICAL) # pylint: disable=no-member
pw_sizer.Add(wx.StaticText(self.pnl, -1, "Password:"), 0, wx.EXPAND) # pylint: disable=no-member pw_sizer.Add(wx.StaticText(self.pnl, -1, "Password:"), 0, wx.EXPAND) # pylint: disable=no-member
name = wx.TextCtrl(self.pnl, value=passpath, style=wx.TE_DONTWRAP) name: wx.TextCtrl = wx.TextCtrl(self.pnl, value=pass_path, style=wx.TE_DONTWRAP)
text = wx.TextCtrl(self.pnl, style=wx.TE_MULTILINE | wx.TE_DONTWRAP) text: wx.TextCtrl = wx.TextCtrl(self.pnl, style=wx.TE_MULTILINE | wx.TE_DONTWRAP)
sbtn = wx.Button(self.pnl, label="Save password") s_btn: wx.Button = wx.Button(self.pnl, label="Save password")
name_sizer.Add(name, 0, wx.EXPAND) # pylint: disable=no-member name_sizer.Add(name, 0, wx.EXPAND) # pylint: disable=no-member
pw_sizer.Add(text, 0, wx.EXPAND) # pylint: disable=no-member pw_sizer.Add(text, 0, wx.EXPAND) # pylint: disable=no-member
self.sizer.Add(name_sizer, 0, wx.EXPAND) # pylint: disable=no-member self.sizer.Add(name_sizer, 0, wx.EXPAND) # pylint: disable=no-member
self.sizer.Add(pw_sizer, 0, wx.EXPAND) # pylint: disable=no-member self.sizer.Add(pw_sizer, 0, wx.EXPAND) # pylint: disable=no-member
self.sizer.Add(sbtn, 0, wx.EXPAND) # pylint: disable=no-member self.sizer.Add(s_btn, 0, wx.EXPAND) # pylint: disable=no-member
self.Bind(wx.EVT_BUTTON, self.Bind(wx.EVT_BUTTON,
lambda event, path=passpath, mtext=text, mname=name: self. lambda event, path=pass_path, m_text=text, m_name=name: self.
save_to_pass(path, mtext, mname), save_to_pass(path, m_text, m_name),
sbtn) s_btn)
self.pnl.SetupScrolling()
name.SetFocus() name.SetFocus()
@redraw @redraw
def show_password_dialog(self, index): def show_password_dialog(self, index: int):
"""show_password_dialog. """show_password_dialog.
:param index: :param index:
""" """
self.add_tools(index) self.add_tools(index)
passpath = self.pass_handler.get_pass_path_from_index(index, "password") pass_path: str = self.pass_handler.get_pass_path_from_index(index, "password")
cpath = self.pass_handler.top_dir + os.path.dirname(passpath) c_path: str = self.pass_handler.top_dir + os.path.dirname(pass_path)
password = get_password_from_path(passpath) password: str = get_password_from_path(pass_path)
btn = self.make_back_button(index) btn: wx.Button = self.make_back_button(index)
self.sizer.Add(btn, 0, wx.EXPAND) # pylint: disable=no-member self.sizer.Add(btn, 0, wx.EXPAND) # pylint: disable=no-member
self.Bind( self.Bind(
wx.EVT_BUTTON, wx.EVT_BUTTON,
lambda event, path=cpath: self.path_button_clicked(path), lambda event, path=c_path: self.path_button_clicked(path),
btn) btn)
sbtn = wx.Button(self.pnl, label="Show/edit password") s_btn: wx.Button = wx.Button(self.pnl, label="Show/edit password")
cbtn = wx.Button(self.pnl, label="Copy password") c_btn: wx.Button = wx.Button(self.pnl, label="Copy password")
dbtn = wx.Button(self.pnl, label="Delete password") d_btn: wx.Button = wx.Button(self.pnl, label="Delete password")
self.sizer.Add(sbtn, 0, wx.EXPAND) # pylint: disable=no-member self.sizer.Add(s_btn, 0, wx.EXPAND) # pylint: disable=no-member
self.sizer.Add(cbtn, 0, wx.EXPAND) # pylint: disable=no-member self.sizer.Add(c_btn, 0, wx.EXPAND) # pylint: disable=no-member
self.sizer.Add(dbtn, 0, wx.EXPAND) # pylint: disable=no-member self.sizer.Add(d_btn, 0, wx.EXPAND) # pylint: disable=no-member
self.Bind(wx.EVT_BUTTON, self.Bind(wx.EVT_BUTTON,
lambda event, text=password: copy_to_clipboard(text), lambda event, text=password: copy_to_clipboard(text),
cbtn) c_btn)
self.Bind( self.Bind(
wx.EVT_BUTTON, wx.EVT_BUTTON,
lambda event, mindex=index: self.show_password(mindex), lambda event, m_index=index: self.show_password(m_index),
sbtn) s_btn)
self.Bind( self.Bind(
wx.EVT_BUTTON, wx.EVT_BUTTON,
lambda event, mindex=index: self.delete_password(mindex), lambda event, m_index=index: self.delete_password(m_index),
dbtn) d_btn)
self.pnl.SetupScrolling()
@redraw @redraw
def show_password(self, index): def show_password(self, index: int):
"""show_password. """show_password.
:param index: :param index:
""" """
self.add_tools(index) self.add_tools(index)
passpath = self.pass_handler.get_pass_path_from_index(index, "password") pass_path: str = self.pass_handler.get_pass_path_from_index(index, "password")
cpath = self.pass_handler.top_dir + os.path.dirname(passpath) c_path: str = self.pass_handler.top_dir + os.path.dirname(pass_path)
password = get_password_from_path(passpath) password: str = get_password_from_path(pass_path)
btn = self.make_back_button(index) btn: wx.Button = self.make_back_button(index)
self.sizer.Add(btn, 0, wx.EXPAND) # pylint: disable=no-member self.sizer.Add(btn, 0, wx.EXPAND) # pylint: disable=no-member
self.Bind( self.Bind(
wx.EVT_BUTTON, wx.EVT_BUTTON,
lambda event, path=cpath: self.path_button_clicked(path), lambda event, path=c_path: self.path_button_clicked(path),
btn) btn)
text = wx.TextCtrl(self.pnl, text: wx.TextCtrl = wx.TextCtrl(self.pnl,
value=password, value=password,
style=wx.TE_MULTILINE | wx.TE_DONTWRAP) style=wx.TE_MULTILINE | wx.TE_DONTWRAP)
cbtn = wx.Button(self.pnl, label="Copy password") c_btn: wx.Button = wx.Button(self.pnl, label="Copy password")
sbtn = wx.Button(self.pnl, label="Save password") s_btn: wx.Button = wx.Button(self.pnl, label="Save password")
self.sizer.Add(text, 0, wx.EXPAND) # pylint: disable=no-member self.sizer.Add(text, 0, wx.EXPAND) # pylint: disable=no-member
self.sizer.Add(cbtn, 0, wx.EXPAND) # pylint: disable=no-member self.sizer.Add(c_btn, 0, wx.EXPAND) # pylint: disable=no-member
self.sizer.Add(sbtn, 0, wx.EXPAND) # pylint: disable=no-member self.sizer.Add(s_btn, 0, wx.EXPAND) # pylint: disable=no-member
self.Bind(wx.EVT_BUTTON, self.Bind(wx.EVT_BUTTON,
lambda event, mtext=password: copy_to_clipboard(mtext), lambda event, m_text=password: copy_to_clipboard(m_text),
cbtn) c_btn)
self.Bind(wx.EVT_BUTTON, self.Bind(wx.EVT_BUTTON,
lambda event, path=passpath, mtext=text: self.save_to_pass( lambda event, path=pass_path, m_text=text: self.save_to_pass(
path, mtext), path, m_text),
sbtn) s_btn)
self.pnl.SetupScrolling()
@redraw @redraw
def show_tools(self, index=None): def show_tools(self, index: Union[None, int] = None):
"""show_tools. """show_tools.
:param index: :param index:
""" """
btn = wx.Button(self.pnl, label="Go back") btn: wx.Button = wx.Button(self.pnl, label="Go back")
font = btn.GetFont().MakeBold() font: wx.Font = btn.GetFont().MakeBold()
btn.SetFont(font) btn.SetFont(font)
self.sizer.Add(btn, 0, wx.EXPAND) # pylint: disable=no-member self.sizer.Add(btn, 0, wx.EXPAND) # pylint: disable=no-member
self.Bind( self.Bind(
wx.EVT_BUTTON, wx.EVT_BUTTON,
lambda event, mindex=index: self.back_button_clicked(mindex), lambda event, m_index=index: self.back_button_clicked(m_index),
btn) btn)
nbtn = wx.Button(self.pnl, label="Add new password") n_btn: wx.Button = wx.Button(self.pnl, label="Add new password")
font = nbtn.GetFont().MakeBold() font: wx.Font = n_btn.GetFont().MakeBold()
nbtn.SetFont(font) n_btn.SetFont(font)
self.sizer.Add(nbtn, 0, wx.EXPAND) # pylint: disable=no-member self.sizer.Add(n_btn, 0, wx.EXPAND) # pylint: disable=no-member
self.Bind(wx.EVT_BUTTON, lambda event: self.show_new_dialog(), self.Bind(wx.EVT_BUTTON, lambda event: self.show_new_dialog(),
nbtn) n_btn)
self.add_push_pull() self.add_push_pull()
self.pnl.SetupScrolling()
if __name__ == '__main__': if __name__ == '__main__':
# When this module is run (not imported) then create the app, the # When this module is run (not imported) then create the app, the
# frame, show it, and start the event loop. # frame, show it, and start the event loop.
app = wx.App() app: wx.App = wx.App()
frm = PassUi(None, title='PassUi') frm: PassUi = PassUi(None, title='PassUi')
frm.Show() frm.Show()
app.MainLoop() app.MainLoop()

Loading…
Cancel
Save