class ProxyEntryDialog(Dialog): def __init__(self, parent, controller): super(ProxyEntryDialog, self).__init__(None, parent, "New Proxy") self._protocol = None self._url = None self._controller = controller def body(self, parent, options): ttk.Label(parent, text="Protocol:", borderwidth=0, anchor=tk.E).grid(padx=7, pady=6, row=0, sticky='nse') self._protocol = EntryCustom(parent, state=tk.NORMAL) self._protocol.grid(padx=(0, 7), pady=6, row=0, column=1, sticky='nsw') ttk.Label(parent, text="URL:", borderwidth=0, anchor=tk.E).grid(padx=7, pady=6, row=1, sticky='nse') self._url = EntryCustom(parent, state=tk.NORMAL, width=50) self._url.grid(padx=(0, 7), pady=6, row=1, column=1, sticky='nsew') return self._protocol # initial focus def validate(self): protocol = self._protocol.get().strip() if len(protocol) == 0 or protocol in self._controller._proxy_urls: self.initial_focus = self._protocol return False url = self._url.get().strip() if not CredentialsView._validate_url(url): self.initial_focus = self._url return False self.initial_focus = self._protocol return True def apply(self): self.result = (self._protocol.get().strip(), self._url.get().strip())
class PackageEntryDialog(Dialog): def __init__(self,parent,controller): super(PackageEntryDialog, self).__init__(None,parent,"New Package") self._package = None self._controller = controller def body(self, parent,options): ttk.Label(parent, text="Package:", borderwidth=0, anchor=tk.E).grid(padx=7,pady=6,row=0,sticky='nse') self._package = EntryCustom(parent,state=tk.NORMAL) self._package.grid(padx=(0,7),pady=6,row=0, column=1,sticky='nsw') return self._package # initial focus def validate(self): package = self._package.get().strip() if len(package) == 0 or package in self._controller._packages: self.initial_focus = self._package return False self.initial_focus = self._package return True def apply(self): self.result = self._package.get().strip()
class MainPage(ttk.Frame): def __init__(self, parent, preferences, **options): super(MainPage, self).__init__(parent, **options) self._label_text = None self._label = None self._apiTokenEntry = None self._apiToken = tk.StringVar() self._urlEntry = None self._url = tk.StringVar() self.pack(fill=tk.BOTH, expand=tk.TRUE) self._apiToken.set(preferences.get_token('')) self._url.set(preferences.get_url(Preferences.URL)) ttk.Label(self, text="Token:", borderwidth=0, anchor=tk.E).grid(row=0, column=0, pady=5, sticky='nsew') self._apiTokenEntry = EntryCustom(self, textvariable=self._apiToken, width=120, state=tk.NORMAL) self._apiTokenEntry.grid(row=0, column=1, pady=5, sticky='nsew') ttk.Label(self, text="URL:", borderwidth=0, anchor=tk.E).grid(row=1, column=0, pady=5, sticky='nsew') self._urlEntry = EntryCustom(self, textvariable=self._url, width=80, state=tk.NORMAL) self._urlEntry.grid(row=1, column=1, pady=5, sticky='nsw') self.initial_focus = self._apiTokenEntry def is_valid(self): return CredentialsView._is_valid_url(self._url.get().strip()) def validate(self): if not CredentialsView._validate_url(self._url.get().strip()): self.initial_focus = self._urlEntry return False self.initial_focus = self._apiTokenEntry return True def apply(self, preferences): token = self._apiToken.get().strip() url = self._url.get().strip() preferences.set_token(token if len(token) > 0 else None) preferences.set_url(url if len(url) > 0 else None)
class CredentialsView(ttk.Frame): def __init__(self, parent, **options): super(CredentialsView, self).__init__(parent, **options) self.pack(fill=tk.BOTH, expand=tk.TRUE) preferences = Preferences() self._credentials_preferences = preferences.credentials_preferences ttk.Label(self, text="URL:", borderwidth=0, anchor=tk.E).grid(row=0, column=0, pady=5, sticky='nsew') urls = [ credentials.url for credentials in self._credentials_preferences.get_all_credentials() ] self._urlCombobox = URLCombobox(self, self, width=80, exportselection=0, state='readonly', values=urls) self._urlCombobox._text = self._credentials_preferences.get_url('') self._urlCombobox.set(self._urlCombobox._text) if len(urls) > 0: if self._urlCombobox.get() in urls: self._urlCombobox.current(urls.index(self._urlCombobox.get())) else: self._urlCombobox.current(0) self._urlCombobox.grid(row=0, column=1, pady=5, sticky='nsew') button_container = tk.Frame(self) button_container.grid(row=0, column=2, pady=5, sticky='nsw') self._add_button = ttk.Button(button_container, text='Add', state='enable', command=self.onadd) self._remove_button = ttk.Button(button_container, text='Remove', state='enable', command=self.onremove) self._add_button.pack(side=tk.LEFT) if len(urls) > 0: self._remove_button.pack(side=tk.LEFT) self._apiToken = tk.StringVar() self._apiToken.set(self._credentials_preferences.get_token('')) ttk.Label(self, text="Token:", borderwidth=0, anchor=tk.E).grid(row=1, column=0, pady=5, sticky='nsew') self._apiTokenEntry = EntryCustom( self, textvariable=self._apiToken, width=120, state=tk.NORMAL if len(urls) > 0 else tk.DISABLED) self._apiTokenEntry.grid(row=1, column=1, columnspan=2, pady=5, sticky='nsew') ttk.Label(self, text="Proxies:", borderwidth=0, anchor=tk.E).grid(row=2, column=0, pady=5, sticky='nsew') self._proxiespage = ProxiesPage(self, self._credentials_preferences) self._proxiespage.grid(row=3, column=0, columnspan=3, pady=5, sticky='nsew') self._proxiespage.show_add_button(True) self._proxiespage.show_remove_button(self._proxiespage.has_selection()) self._proxiespage.show_defaults_button(False) if len(urls) == 0: self._proxiespage.enable(False) self.initial_focus = self._urlCombobox def onadd(self): urls = [ credentials.url for credentials in self._credentials_preferences.get_all_credentials() ] dialog = URLEntryDialog(self.master, self) dialog.do_init(tk.LEFT) if CredentialsPreferences.URL not in urls: dialog._url.insert(0, CredentialsPreferences.URL) dialog.do_modal() if dialog.result is None: return url = dialog.result pref_credentials = self._credentials_preferences.get_credentials_with_same_key( url) if pref_credentials is not None: msg = "URL '{}' will replace current URL '{}'. Continue?".format( url, pref_credentials.url) if not messagebox.askyesno('Duplicate Account', msg): return credentials = self._credentials_preferences.set_credentials('', url) self._credentials_preferences.select_credentials(credentials.url) urls = [ credentials.url for credentials in self._credentials_preferences.get_all_credentials() ] self._urlCombobox.config(values=urls) self._urlCombobox._text = self._credentials_preferences.get_url('') self._urlCombobox.set(self._urlCombobox._text) if len(urls) > 0: if self._urlCombobox.get() in urls: self._urlCombobox.current(urls.index(self._urlCombobox.get())) else: self._urlCombobox.current(0) self._remove_button.pack(side=tk.LEFT) self._apiTokenEntry.config(state=tk.NORMAL) self._proxiespage.enable(True) self._apiToken.set(self._credentials_preferences.get_token('')) self._proxiespage._proxy_urls = self._credentials_preferences.get_proxy_urls( {}) self._proxiespage.populate() def onremove(self): self._credentials_preferences.remove_credentials( self._urlCombobox.get().strip()) urls = [ credentials.url for credentials in self._credentials_preferences.get_all_credentials() ] self._urlCombobox.config(values=urls) self._urlCombobox._text = self._credentials_preferences.get_url('') self._urlCombobox.set(self._urlCombobox._text) if len(urls) > 0: if self._urlCombobox.get() in urls: self._urlCombobox.current(urls.index(self._urlCombobox.get())) else: self._urlCombobox.current(0) self._apiToken.set(self._credentials_preferences.get_token('')) self._proxiespage._proxy_urls = self._credentials_preferences.get_proxy_urls( {}) self._proxiespage.populate() if len(urls) == 0: self._remove_button.pack_forget() self._apiTokenEntry.config(state=tk.DISABLED) self._proxiespage.enable(False) def on_url_set(self, url): # save previously shown data if self._credentials_preferences.selected_credentials is not None: token = self._apiToken.get().strip() if token == '': # go back to previous selection urls = [ credentials.url for credentials in self._credentials_preferences.get_all_credentials() ] self._urlCombobox._text = self._credentials_preferences.get_url( '') self._urlCombobox.set(self._urlCombobox._text) if len(urls) > 0: if self._urlCombobox.get() in urls: self._urlCombobox.current( urls.index(self._urlCombobox.get())) else: self._urlCombobox.current(0) self._urlCombobox.selection_clear() self.initial_focus = self._apiTokenEntry def set_focus(): self.initial_focus.focus_set() self.after(0, set_focus) return proxy_urls = self._proxiespage._proxy_urls if token != self._credentials_preferences.get_token('') or \ proxy_urls != self._credentials_preferences.get_proxy_urls({}): self._credentials_preferences.set_credentials( token, self._credentials_preferences.selected_credentials.url, proxy_urls) self._credentials_preferences.select_credentials(url) self._apiToken.set(self._credentials_preferences.get_token('')) self._proxiespage._proxy_urls = self._credentials_preferences.get_proxy_urls( {}) self._proxiespage.populate() def is_valid(self): return self._proxiespage.is_valid() def validate(self): # check current show token if self._credentials_preferences.selected_credentials is not None: token = self._apiToken.get().strip() if token == '': self.initial_focus = self._apiTokenEntry return False if not self._proxiespage.is_valid(): self.initial_focus = self._proxiespage.initial_focus return False self.initial_focus = self._urlCombobox return True def apply(self, preferences): # save previously shown data if self._credentials_preferences.selected_credentials is not None: token = self._apiToken.get().strip() proxy_urls = self._proxiespage._proxy_urls if token != self._credentials_preferences.get_token('') or \ proxy_urls != self._credentials_preferences.get_proxy_urls({}): self._credentials_preferences.set_credentials( token, self._credentials_preferences.selected_credentials.url, proxy_urls) preferences._credentials_preferences = self._credentials_preferences @staticmethod def _is_valid_url(url): if url is None or not isinstance(url, str): return False url = url.strip() if len(url) == 0: return False min_attributes = ('scheme', 'netloc') valid = True try: token = urllib.parse.urlparse(url) if not all([getattr(token, attr) for attr in min_attributes]): valid = False except: valid = False return valid @staticmethod def _validate_url(url): valid = CredentialsView._is_valid_url(url) if not valid: messagebox.showerror("Error", 'Invalid url') return valid
class MainPage(ttk.Frame): def __init__(self, parent, preferences, **options): super(MainPage, self).__init__(parent, **options) self._label_text = None self._label = None self._apiTokenEntry = None self._apiToken = tk.StringVar() self._urlEntry = None self._url = tk.StringVar() self._hubEntry = None self._hub = tk.StringVar() self._groupEntry = None self._group = tk.StringVar() self._projectEntry = None self._project = tk.StringVar() self._verifyEntry = None self.pack(fill=tk.BOTH, expand=tk.TRUE) self._apiToken.set(preferences.get_token('')) self._url.set(preferences.get_url(Preferences.URL)) self._hub.set(preferences.get_hub('')) self._group.set(preferences.get_group('')) self._project.set(preferences.get_project('')) self._verify = preferences.get_verify(Preferences.VERIFY) ttk.Label(self, text="Token:", borderwidth=0, anchor=tk.E).grid(row=0, column=0, sticky='nsew') self._apiTokenEntry = EntryCustom(self, textvariable=self._apiToken, width=120, state=tk.NORMAL) self._apiTokenEntry.grid(row=0, column=1, sticky='nsew') ttk.Label(self, text="URL:", borderwidth=0, anchor=tk.E).grid(row=1, column=0, sticky='nsew') self._urlEntry = EntryCustom(self, textvariable=self._url, width=60, state=tk.NORMAL) self._urlEntry.grid(row=1, column=1, sticky='nsw') ttk.Label(self, text="Hub:", borderwidth=0, anchor=tk.E).grid(row=2, column=0, sticky='nsew') self._hubEntry = EntryCustom(self, textvariable=self._hub, state=tk.NORMAL) self._hubEntry.grid(row=2, column=1, sticky='nsw') ttk.Label(self, text="Group:", borderwidth=0, anchor=tk.E).grid(row=3, column=0, sticky='nsew') self._groupEntry = EntryCustom(self, textvariable=self._group, state=tk.NORMAL) self._groupEntry.grid(row=3, column=1, sticky='nsw') ttk.Label(self, text="Project:", borderwidth=0, anchor=tk.E).grid(row=4, column=0, sticky='nsew') self._projectEntry = EntryCustom(self, textvariable=self._project, state=tk.NORMAL) self._projectEntry.grid(row=4, column=1, sticky='nsw') ttk.Label(self, text="Verify:", borderwidth=0, anchor=tk.E).grid(row=5, column=0, sticky='nsew') values = ['True', 'False'] self._verifyEntry = ttk.Combobox(self, exportselection=0, state='readonly', values=values, width=6) self._verifyEntry.current(values.index(str(self._verify))) self._verifyEntry.grid(row=5, column=1, sticky='nsw') self.initial_focus = self._apiTokenEntry def is_valid(self): return QconfigView._is_valid_url(self._url.get().strip()) def validate(self): if not QconfigView._validate_url(self._url.get().strip()): self.initial_focus = self._urlEntry return False self.initial_focus = self._apiTokenEntry return True def apply(self, preferences): token = self._apiToken.get().strip() url = self._url.get().strip() hub = self._hub.get().strip() group = self._group.get().strip() project = self._project.get().strip() verify = self._verifyEntry.get().lower() == 'true' preferences.set_token(token if len(token) > 0 else None) preferences.set_url(url if len(url) > 0 else None) preferences.set_hub(hub if len(hub) > 0 else None) preferences.set_group(group if len(group) > 0 else None) preferences.set_project(project if len(project) > 0 else None) preferences.set_verify(verify)