Skip to content

Commit

Permalink
refactor(Config): Move possible config paths out of func to constant
Browse files Browse the repository at this point in the history
  • Loading branch information
rlaphoenix committed Apr 19, 2024
1 parent 4f1dfd7 commit c101136
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions devine/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,29 +77,27 @@ def from_yaml(cls, path: Path) -> Config:
return cls(**yaml.safe_load(path.read_text(encoding="utf8")) or {})


def get_config_path() -> Optional[Path]:
"""
Get Path to Config from various locations.
# noinspection PyProtectedMember
POSSIBLE_CONFIG_PATHS = (
# The Devine Namespace Folder (e.g., %appdata%/Python/Python311/site-packages/devine)
Config._Directories.namespace_dir / Config._Filenames.root_config,
# The Parent Folder to the Devine Namespace Folder (e.g., %appdata%/Python/Python311/site-packages)
Config._Directories.namespace_dir.parent / Config._Filenames.root_config,
# The AppDirs User Config Folder (e.g., %localappdata%/devine)
Config._Directories.user_configs / Config._Filenames.root_config
)

Looks for a config file in the following folders in order:

1. The Devine Namespace Folder (e.g., %appdata%/Python/Python311/site-packages/devine)
2. The Parent Folder to the Devine Namespace Folder (e.g., %appdata%/Python/Python311/site-packages)
3. The AppDirs User Config Folder (e.g., %localappdata%/devine)
def get_config_path() -> Optional[Path]:
"""
Get Path to Config from any one of the possible locations.
Returns None if no config file could be found.
"""
# noinspection PyProtectedMember
path = Config._Directories.namespace_dir / Config._Filenames.root_config
if not path.exists():
# noinspection PyProtectedMember
path = Config._Directories.namespace_dir.parent / Config._Filenames.root_config
if not path.exists():
# noinspection PyProtectedMember
path = Config._Directories.user_configs / Config._Filenames.root_config
if not path.exists():
path = None
return path
for path in POSSIBLE_CONFIG_PATHS:
if path.exists():
return path
return None


config_path = get_config_path()
Expand Down

0 comments on commit c101136

Please sign in to comment.