Skip to content

Commit

Permalink
Refactor for easier getting of all folders
Browse files Browse the repository at this point in the history
  • Loading branch information
bluedrink9 committed Jun 5, 2022
1 parent d88826c commit 243edce
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 25 deletions.
34 changes: 23 additions & 11 deletions lib/autokey/configmanager/configmanager.py
Expand Up @@ -268,17 +268,7 @@ def load_global_config(self):

self.workAroundApps = re.compile(self.SETTINGS[WORKAROUND_APP_REGEX])

for entryPath in glob.glob(CONFIG_DEFAULT_FOLDER + "/*"):
if os.path.isdir(entryPath):
logger.debug("Loading folder at '%s'", entryPath)
f = autokey.model.folder.Folder("", path=entryPath)
f.load(None)
self.folders.append(f)

for folderPath in data["folders"]:
f = autokey.model.folder.Folder("", path=folderPath)
f.load()
self.folders.append(f)
self.__load_folders(data)

self.toggleServiceHotkey.load_from_serialized(data["toggleServiceHotkey"])
self.configHotkey.load_from_serialized(data["configHotkey"])
Expand All @@ -289,6 +279,28 @@ def load_global_config(self):
self.config_altered(False)
logger.info("Successfully loaded configuration")

def __load_folders(self, data):
for path in self.get_all_config_folder_paths(data):
f = autokey.model.folder.Folder("", path=path)
f.load()
logger.debug("Loading folder at '%s'", path)
self.folders.append(f)


def get_all_config_folder_paths(self, data):
for path in glob.glob(CONFIG_DEFAULT_FOLDER + "/*"):
if os.path.isdir(path):
yield path
for path in data["folders"]:
yield path

def get_all_folders(self):
out = []
for folder in self.folders:
out.append(folder)
out.extend(folder.get_child_folders())
return out

def __checkExisting(self, path):
# Check if we already know about the path, and return object if found
for item in self.allItems:
Expand Down
2 changes: 1 addition & 1 deletion lib/autokey/configmanager/predefined_user_files.py
Expand Up @@ -210,7 +210,7 @@ def create_my_phrases_folder() -> Folder:
def create_sample_scripts_folder():
"""
Creates the "Sample Scripts" folder. It contains a bunch of pre-defined example scripts.
The exact script content is read from the predifined_user_scripts directory inside this Python package.
The exact script content is read from the predefined_user_scripts directory inside this Python package.
"""
sample_scripts = _create_folder("Sample Scripts")
sample_scripts.persist()
Expand Down
16 changes: 4 additions & 12 deletions lib/autokey/configmanager/version_upgrading.py
Expand Up @@ -64,8 +64,8 @@ def upgrade_configuration_format(configuration_manager, config_data: dict):
convert_v0_70_to_v0_80(config_data)
if version < vparse("0.95.3"):
convert_autostart_entries_for_v0_95_3()
if version < vparse("0.95.11"):
convertDotFiles_v95_11(config_data)
if version < vparse("0.95.11"): # actually 0.96.0
convertDotFiles_v95_11(configuration_manager, config_data)

def upgrade_configuration_after_load(configuration_manager, config_data: dict):
"""
Expand Down Expand Up @@ -185,15 +185,7 @@ def convertDotFiles_v95_11_folder(p: Path):
if name.is_dir():
convertDotFiles_v95_11_folder(name)

def all_config_folders(configData):
for entryPath in glob.glob(cm_constants.CONFIG_DEFAULT_FOLDER + "/*"):
if os.path.isdir(entryPath):
yield entryPath
for name in configData["folders"]:
yield name


def convertDotFiles_v95_11(configData):
def convertDotFiles_v95_11(cm, configData):
logger.info("Version update: Unhiding sidecar dotfiles for versions > 0.95.10")
for name in all_config_folders(configData):
for name in cm.get_all_config_folder_paths(configData):
convertDotFiles_v95_11_folder(Path(name))
7 changes: 7 additions & 0 deletions lib/autokey/model/folder.py
Expand Up @@ -154,6 +154,13 @@ def update_children(self):
for childItem in self.items:
childItem.build_path(os.path.basename(childItem.path))

def get_child_folders(self):
out = []
for folder in self.folders:
out.append(folder)
out.extend(folder.get_child_folders())
return out

def remove_data(self):
if self.path is not None:
for child in self.items:
Expand Down
18 changes: 17 additions & 1 deletion tests/test_configmanager.py
Expand Up @@ -23,7 +23,7 @@
from hamcrest import *
from tests.engine_helpers import *

import autokey.model.folder
import autokey.model.folder as akfolder
from autokey.configmanager.configmanager import ConfigManager
from autokey.configmanager.configmanager_constants import CONFIG_DEFAULT_FOLDER
import autokey.configmanager.predefined_user_files
Expand Down Expand Up @@ -59,6 +59,22 @@ def test_item_has_same_hotkey(create_engine):
testHK = create_test_hotkey(engine, folder, hotkey)
assert ConfigManager.item_has_same_hotkey(testHK, modifiers, key, None)

def test_get_all_folders(create_engine):
engine, folder = create_engine
cm = engine.configManager

first_child = akfolder.Folder("first child")
first_grandchild = akfolder.Folder("first grandchild")
second_grandchild = akfolder.Folder("second grandchild")
first_child.add_folder(first_grandchild)
first_child.add_folder(second_grandchild)
cm.folders.append(first_child)

expected = [folder, first_child, first_grandchild, second_grandchild]
result = cm.get_all_folders()

assert_that(result, equal_to(expected))

def test_create_predefined_user_files_my_phrases_folder(create_engine):
engine, folder = create_engine
# --- Setup ---
Expand Down

0 comments on commit 243edce

Please sign in to comment.