Added better type hinting

This commit is contained in:
itdominator 2022-10-27 21:26:58 -05:00
parent eeef0a4330
commit ee123c4916
5 changed files with 103 additions and 98 deletions

View File

@ -117,7 +117,6 @@ class Controller_Data:
state.store = state.icon_grid.get_model() state.store = state.icon_grid.get_model()
state.warning_alert = self.warning_alert state.warning_alert = self.warning_alert
selected_files = state.icon_grid.get_selected_items() selected_files = state.icon_grid.get_selected_items()
if selected_files: if selected_files:
state.selected_files = self.format_to_uris(state.store, state.wid, state.tid, selected_files, True) state.selected_files = self.format_to_uris(state.store, state.wid, state.tid, selected_files, True)

View File

@ -16,100 +16,100 @@ def threaded(fn):
class WindowController: class WindowController:
def __init__(self): def __init__(self):
USER_HOME = path.expanduser('~') USER_HOME: str = path.expanduser('~')
CONFIG_PATH = USER_HOME + "/.config/solarfm" CONFIG_PATH: str = f"{USER_HOME}/.config/solarfm"
self._session_file = CONFIG_PATH + "/session.json" self._session_file: srr = f"{CONFIG_PATH}/session.json"
self._event_sleep_time = 1 self._event_sleep_time: int = 1
self._active_window_id = "" self._active_window_id: str = ""
self._active_tab_id = "" self._active_tab_id: str = ""
self._windows = [] self._windows: list = []
def set_wid_and_tid(self, wid, tid): def set_wid_and_tid(self, wid: int, tid: int) -> None:
self._active_window_id = str(wid) self._active_window_id = str(wid)
self._active_tab_id = str(tid) self._active_tab_id = str(tid)
def get_active_wid_and_tid(self): def get_active_wid_and_tid(self) -> list:
return self._active_window_id, self._active_tab_id return self._active_window_id, self._active_tab_id
def create_window(self): def create_window(self) -> Window:
window = Window() window = Window()
window.set_nickname(f"window_{str(len(self._windows) + 1)}") window.set_nickname(f"window_{str(len(self._windows) + 1)}")
self._windows.append(window) self._windows.append(window)
return window return window
def add_tab_for_window(self, win_id): def add_tab_for_window(self, win_id: str) -> None:
for window in self._windows: for window in self._windows:
if window.get_id() == win_id: if window.get_id() == win_id:
return window.create_tab() return window.create_tab()
def add_tab_for_window_by_name(self, name): def add_tab_for_window_by_name(self, name: str) -> None:
for window in self._windows: for window in self._windows:
if window.get_name() == name: if window.get_name() == name:
return window.create_tab() return window.create_tab()
def add_tab_for_window_by_nickname(self, nickname): def add_tab_for_window_by_nickname(self, nickname: str) -> None:
for window in self._windows: for window in self._windows:
if window.get_nickname() == nickname: if window.get_nickname() == nickname:
return window.create_tab() return window.create_tab()
def pop_window(self): def pop_window(self) -> None:
self._windows.pop() self._windows.pop()
def delete_window_by_id(self, win_id): def delete_window_by_id(self, win_id: str) -> None:
for window in self._windows: for window in self._windows:
if window.get_id() == win_id: if window.get_id() == win_id:
self._windows.remove(window) self._windows.remove(window)
break break
def delete_window_by_name(self, name): def delete_window_by_name(self, name: str) -> str:
for window in self._windows: for window in self._windows:
if window.get_name() == name: if window.get_name() == name:
self._windows.remove(window) self._windows.remove(window)
break break
def delete_window_by_nickname(self, nickname): def delete_window_by_nickname(self, nickname: str) -> str:
for window in self._windows: for window in self._windows:
if window.get_nickname() == nickname: if window.get_nickname() == nickname:
self._windows.remove(window) self._windows.remove(window)
break break
def get_window_by_id(self, win_id): def get_window_by_id(self, win_id: str) -> Window:
for window in self._windows: for window in self._windows:
if window.get_id() == win_id: if window.get_id() == win_id:
return window return window
raise(f"No Window by ID {win_id} found!") raise(f"No Window by ID {win_id} found!")
def get_window_by_name(self, name): def get_window_by_name(self, name: str) -> Window:
for window in self._windows: for window in self._windows:
if window.get_name() == name: if window.get_name() == name:
return window return window
raise(f"No Window by Name {name} found!") raise(f"No Window by Name {name} found!")
def get_window_by_nickname(self, nickname): def get_window_by_nickname(self, nickname: str) -> Window:
for window in self._windows: for window in self._windows:
if window.get_nickname() == nickname: if window.get_nickname() == nickname:
return window return window
raise(f"No Window by Nickname {nickname} found!") raise(f"No Window by Nickname {nickname} found!")
def get_window_by_index(self, index): def get_window_by_index(self, index: int) -> Window:
return self._windows[index] return self._windows[index]
def get_all_windows(self): def get_all_windows(self) -> list:
return self._windows return self._windows
def set_window_nickname(self, win_id = None, nickname = ""): def set_window_nickname(self, win_id: str = None, nickname: str = "") -> None:
for window in self._windows: for window in self._windows:
if window.get_id() == win_id: if window.get_id() == win_id:
window.set_nickname(nickname) window.set_nickname(nickname)
def list_windows(self): def list_windows(self) -> None:
print("\n[ ---- Windows ---- ]\n") print("\n[ ---- Windows ---- ]\n")
for window in self._windows: for window in self._windows:
print(f"\nID: {window.get_id()}") print(f"\nID: {window.get_id()}")
@ -121,18 +121,18 @@ class WindowController:
def list_files_from_tabs_of_window(self, win_id): def list_files_from_tabs_of_window(self, win_id: str) -> None:
for window in self._windows: for window in self._windows:
if window.get_id() == win_id: if window.get_id() == win_id:
window.list_files_from_tabs() window.list_files_from_tabs()
break break
def get_tabs_count(self, win_id): def get_tabs_count(self, win_id: str) -> int:
for window in self._windows: for window in self._windows:
if window.get_id() == win_id: if window.get_id() == win_id:
return window.get_tabs_count() return window.get_tabs_count()
def get_tabs_from_window(self, win_id): def get_tabs_from_window(self, win_id: str) -> list:
for window in self._windows: for window in self._windows:
if window.get_id() == win_id: if window.get_id() == win_id:
return window.get_all_tabs() return window.get_all_tabs()
@ -140,13 +140,13 @@ class WindowController:
def unload_tabs_and_windows(self): def unload_tabs_and_windows(self) -> None:
for window in self._windows: for window in self._windows:
window.get_all_tabs().clear() window.get_all_tabs().clear()
self._windows.clear() self._windows.clear()
def save_state(self, session_file = None): def save_state(self, session_file: str = None) -> None:
if not session_file: if not session_file:
session_file = self._session_file session_file = self._session_file
@ -174,7 +174,7 @@ class WindowController:
else: else:
raise Exception("Window data corrupted! Can not save session!") raise Exception("Window data corrupted! Can not save session!")
def get_state_from_file(self, session_file = None): def get_state_from_file(self, session_file: str = None) -> dict:
if not session_file: if not session_file:
session_file = self._session_file session_file = self._session_file

View File

@ -7,20 +7,20 @@ import os
class Path: class Path:
def get_home(self): def get_home(self) -> str:
return os.path.expanduser("~") + self.subpath return os.path.expanduser("~") + self.subpath
def get_path(self): def get_path(self) -> str:
return f"/{'/'.join(self.path)}" if self.path else f"/{''.join(self.path)}" return f"/{'/'.join(self.path)}" if self.path else f"/{''.join(self.path)}"
def get_path_list(self): def get_path_list(self) -> list:
return self.path return self.path
def push_to_path(self, dir): def push_to_path(self, dir: str):
self.path.append(dir) self.path.append(dir)
self.load_directory() self.load_directory()
def pop_from_path(self): def pop_from_path(self) -> None:
try: try:
self.path.pop() self.path.pop()
@ -32,9 +32,9 @@ class Path:
except Exception as e: except Exception as e:
pass pass
def set_path(self, path): def set_path(self, path: str) -> bool:
if path == self.get_path(): if path == self.get_path():
return return False
if os.path.isdir(path): if os.path.isdir(path):
self.path = list( filter(None, path.replace("\\", "/").split('/')) ) self.path = list( filter(None, path.replace("\\", "/").split('/')) )
@ -43,7 +43,7 @@ class Path:
return False return False
def set_path_with_sub_path(self, sub_path): def set_path_with_sub_path(self, sub_path: str) -> bool:
path = os.path.join(self.get_home(), sub_path) path = os.path.join(self.get_home(), sub_path)
if path == self.get_path(): if path == self.get_path():
return False return False
@ -55,7 +55,7 @@ class Path:
return False return False
def set_to_home(self): def set_to_home(self) -> None:
home = os.path.expanduser("~") + self.subpath home = os.path.expanduser("~") + self.subpath
path = list( filter(None, home.replace("\\", "/").split('/')) ) path = list( filter(None, home.replace("\\", "/").split('/')) )
self.path = path self.path = path

View File

@ -21,24 +21,24 @@ from .path import Path
class Tab(Settings, FileHandler, Launcher, Icon, Path): class Tab(Settings, FileHandler, Launcher, Icon, Path):
def __init__(self): def __init__(self):
self.logger = None self.logger = None
self._id_length = 10 self._id_length: int = 10
self._id = "" self._id: str = ""
self._wid = None self._wid: str = None
self._dir_watcher = None self._dir_watcher = None
self._hide_hidden = self.HIDE_HIDDEN_FILES self._hide_hidden: bool = self.HIDE_HIDDEN_FILES
self._files = [] self._files: list = []
self._dirs = [] self._dirs: list = []
self._vids = [] self._vids: list = []
self._images = [] self._images: list = []
self._desktop = [] self._desktop: list = []
self._ungrouped = [] self._ungrouped: list = []
self._hidden = [] self._hidden: list = []
self._generate_id() self._generate_id()
self.set_to_home() self.set_to_home()
def load_directory(self): def load_directory(self) -> None:
path = self.get_path() path = self.get_path()
self._dirs = [] self._dirs = []
self._vids = [] self._vids = []
@ -97,7 +97,7 @@ class Tab(Settings, FileHandler, Launcher, Icon, Path):
return False return False
def get_not_hidden_count(self): def get_not_hidden_count(self) -> int:
return len(self._files) + \ return len(self._files) + \
len(self._dirs) + \ len(self._dirs) + \
len(self._vids) + \ len(self._vids) + \
@ -105,13 +105,13 @@ class Tab(Settings, FileHandler, Launcher, Icon, Path):
len(self._desktop) + \ len(self._desktop) + \
len(self._ungrouped) len(self._ungrouped)
def get_hidden_count(self): def get_hidden_count(self) -> int:
return len(self._hidden) return len(self._hidden)
def get_files_count(self): def get_files_count(self) -> int:
return len(self._files) return len(self._files)
def get_path_part_from_hash(self, hash): def get_path_part_from_hash(self, hash: str) -> str:
files = self.get_files() files = self.get_files()
file = None file = None
@ -122,7 +122,7 @@ class Tab(Settings, FileHandler, Launcher, Icon, Path):
return file return file
def get_files_formatted(self): def get_files_formatted(self) -> dict:
files = self._hash_set(self._files), files = self._hash_set(self._files),
dirs = self._hash_set(self._dirs), dirs = self._hash_set(self._dirs),
videos = self.get_videos(), videos = self.get_videos(),
@ -154,7 +154,7 @@ class Tab(Settings, FileHandler, Launcher, Icon, Path):
return data return data
def get_gtk_icon_str_combo(self): def get_gtk_icon_str_combo(self) -> list:
data = [] data = []
dir = self.get_current_directory() dir = self.get_current_directory()
for file in self._files: for file in self._files:
@ -163,57 +163,57 @@ class Tab(Settings, FileHandler, Launcher, Icon, Path):
return data return data
def get_current_directory(self): def get_current_directory(self) -> str:
return self.get_path() return self.get_path()
def get_current_sub_path(self): def get_current_sub_path(self) -> str:
path = self.get_path() path = self.get_path()
home = f"{self.get_home()}/" home = f"{self.get_home()}/"
return path.replace(home, "") return path.replace(home, "")
def get_end_of_path(self): def get_end_of_path(self) -> str:
parts = self.get_current_directory().split("/") parts = self.get_current_directory().split("/")
size = len(parts) size = len(parts)
return parts[size - 1] return parts[size - 1]
def set_hiding_hidden(self, state): def set_hiding_hidden(self, state: bool) -> None:
self._hide_hidden = state self._hide_hidden = state
def is_hiding_hidden(self): def is_hiding_hidden(self) -> bool:
return self._hide_hidden return self._hide_hidden
def get_dot_dots(self): def get_dot_dots(self) -> list:
return self._hash_set(['.', '..']) return self._hash_set(['.', '..'])
def get_files(self): def get_files(self) -> list:
return self._hash_set(self._files) return self._hash_set(self._files)
def get_dirs(self): def get_dirs(self) -> list:
return self._hash_set(self._dirs) return self._hash_set(self._dirs)
def get_videos(self): def get_videos(self) -> list:
return self._hash_set(self._vids) return self._hash_set(self._vids)
def get_images(self): def get_images(self) -> list:
return self._hash_set(self._images) return self._hash_set(self._images)
def get_desktops(self): def get_desktops(self) -> list:
return self._hash_set(self._desktop) return self._hash_set(self._desktop)
def get_ungrouped(self): def get_ungrouped(self) -> list:
return self._hash_set(self._ungrouped) return self._hash_set(self._ungrouped)
def get_hidden(self): def get_hidden(self) -> list:
return self._hash_set(self._hidden) return self._hash_set(self._hidden)
def get_id(self): def get_id(self) -> str:
return self._id return self._id
def set_wid(self, _wid): def set_wid(self, _wid: str) -> None:
self._wid = _wid self._wid = _wid
def get_wid(self): def get_wid(self) -> str:
return self._wid return self._wid
def set_dir_watcher(self, watcher): def set_dir_watcher(self, watcher):
@ -228,19 +228,19 @@ class Tab(Settings, FileHandler, Launcher, Icon, Path):
def _natural_keys(self, text): def _natural_keys(self, text):
return [ self._atoi(c) for c in re.split('(\d+)',text) ] return [ self._atoi(c) for c in re.split('(\d+)',text) ]
def _hash_text(self, text): def _hash_text(self, text) -> str:
return hashlib.sha256(str.encode(text)).hexdigest()[:18] return hashlib.sha256(str.encode(text)).hexdigest()[:18]
def _hash_set(self, arry): def _hash_set(self, arry: list) -> list:
data = [] data = []
for arr in arry: for arr in arry:
data.append([arr, self._hash_text(arr)]) data.append([arr, self._hash_text(arr)])
return data return data
def _random_with_N_digits(self, n): def _random_with_N_digits(self, n: int) -> int:
range_start = 10**(n-1) range_start = 10**(n-1)
range_end = (10**n)-1 range_end = (10**n)-1
return randint(range_start, range_end) return randint(range_start, range_end)
def _generate_id(self): def _generate_id(self) -> str:
self._id = str(self._random_with_N_digits(self._id_length)) self._id = str(self._random_with_N_digits(self._id_length))

View File

@ -11,62 +11,68 @@ from .tabs.tab import Tab
class Window: class Window:
def __init__(self): def __init__(self):
self._id_length = 10 self._id_length: int = 10
self._id = "" self._id: str = ""
self._name = "" self._name: str = ""
self._nickname = "" self._nickname:str = ""
self._isHidden = False self._isHidden: bool = False
self._tabs = [] self._active_tab: int = 0
self._tabs: list = []
self._generate_id() self._generate_id()
self._set_name() self._set_name()
def create_tab(self): def create_tab(self) -> Tab:
tab = Tab() tab = Tab()
self._tabs.append(tab) self._tabs.append(tab)
return tab return tab
def pop_tab(self): def pop_tab(self) -> None:
self._tabs.pop() self._tabs.pop()
def delete_tab_by_id(self, tid): def delete_tab_by_id(self, tid: str):
for tab in self._tabs: for tab in self._tabs:
if tab.get_id() == tid: if tab.get_id() == tid:
self._tabs.remove(tab) self._tabs.remove(tab)
break break
def get_tab_by_id(self, tid): def get_tab_by_id(self, tid: str) -> Tab:
for tab in self._tabs: for tab in self._tabs:
if tab.get_id() == tid: if tab.get_id() == tid:
return tab return tab
def get_tab_by_index(self, index): def get_tab_by_index(self, index) -> Tab:
return self._tabs[index] return self._tabs[index]
def get_tabs_count(self): def get_tabs_count(self) -> int:
return len(self._tabs) return len(self._tabs)
def get_all_tabs(self): def get_all_tabs(self) -> list:
return self._tabs return self._tabs
def get_id(self): def get_id(self) -> str:
return self._id return self._id
def get_name(self): def get_name(self) -> str:
return self._name return self._name
def get_nickname(self): def get_nickname(self) -> str:
return self._nickname return self._nickname
def is_hidden(self): def is_hidden(self) -> bool:
return self._isHidden return self._isHidden
def list_files_from_tabs(self): def list_files_from_tabs(self) -> None:
for tab in self._tabs: for tab in self._tabs:
print(tab.get_files()) print(tab.get_files())
def set_active_tab(self, index: int):
self._active_tab = index
def get_active_tab(self) -> Tab:
return self._tabs[self._active_tab]
def set_nickname(self, nickname): def set_nickname(self, nickname):
self._nickname = f"{nickname}" self._nickname = f"{nickname}"