-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract XDG directory standard implementation
- Loading branch information
Showing
6 changed files
with
114 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
"""Tests for astrality.xdg module.""" | ||
|
||
import os | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
from astrality.xdg import XDG | ||
|
||
|
||
@pytest.mark.dont_patch_xdg | ||
def test_xdg_data_home_default_location(monkeypatch): | ||
"""Default location for XDG_DATA_HOME should be respected.""" | ||
xdg = XDG() | ||
default_dir = xdg.data_home | ||
assert default_dir == Path('~/.local/share/astrality').expanduser() | ||
assert default_dir.is_dir() | ||
|
||
|
||
@pytest.mark.dont_patch_xdg | ||
def test_xdg_data_home_using_environment_variable(monkeypatch, tmpdir): | ||
"""XDG_DATA_HOME environment variables should be respected.""" | ||
custom_data_home = Path(tmpdir, 'data') | ||
monkeypatch.setattr( | ||
os, | ||
'environ', | ||
{'XDG_DATA_HOME': str(custom_data_home)}, | ||
) | ||
|
||
xdg = XDG() | ||
data_home = xdg.data_home | ||
assert data_home == custom_data_home / 'astrality' | ||
assert data_home.is_dir() | ||
|
||
|
||
def test_retrieving_data_resource(patch_xdg_directory_standard): | ||
"""The data method should retrieve file resource from data home.""" | ||
xdg = XDG() | ||
resource = xdg.data('modules/test.tmp') | ||
assert resource == patch_xdg_directory_standard / 'modules' / 'test.tmp' | ||
assert resource.exists() | ||
|
||
# We should get the same file again | ||
resource.write_text('hello') | ||
refetched_resource = xdg.data('modules/test.tmp') | ||
assert refetched_resource.read_text() == 'hello' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
"""Module implementing XDG directory standard for astrality.""" | ||
|
||
import os | ||
from pathlib import Path | ||
|
||
|
||
class XDG: | ||
""" | ||
Class for handling the XDG directory standard. | ||
:param application_name: Name of application to use XDG directory standard. | ||
""" | ||
|
||
def __init__(self, application_name: str = 'astrality') -> None: | ||
"""Contstruct XDG object for application.""" | ||
self.application_name = application_name | ||
self.XDG_DATA_HOME = Path(os.environ.get( | ||
'XDG_DATA_HOME', | ||
'~/.local/share', | ||
)).expanduser() | ||
self.XDG_CONFIG_HOME = Path(os.environ.get( | ||
'XDG_CONFIG_HOME', | ||
'~/.config', | ||
)).expanduser() | ||
|
||
@property | ||
def data_home(self) -> Path: | ||
""" | ||
Return XDG_DATA_HOME directory of application. | ||
:return: Path to resolved value of XDG_DATA_HOME. | ||
""" | ||
application_data_home = self.XDG_DATA_HOME / self.application_name | ||
application_data_home.mkdir(parents=True, exist_ok=True) | ||
return application_data_home | ||
|
||
def data(self, resource: str) -> Path: | ||
""" | ||
Return file resource of application. | ||
:param resource: Relative string path, i.e. $XDG_DATA_HOME/<resource>. | ||
:return: Path to touched data resource. | ||
""" | ||
resource_path = self.data_home / resource | ||
resource_path.parent.mkdir(parents=True, exist_ok=True) | ||
resource_path.touch(exist_ok=True) | ||
return resource_path |