Skip to content

Commit

Permalink
added PROJECT_HOME support
Browse files Browse the repository at this point in the history
* use PROJECT_HOME if available instead of XDG_*_HOME variables in unix systems
  • Loading branch information
mohan43u committed Mar 12, 2022
1 parent 6485bc2 commit be44643
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 9 deletions.
41 changes: 32 additions & 9 deletions src/poetry/utils/appdirs.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ def user_cache_dir(appname: str) -> str:
Typical user cache directories are:
macOS: ~/Library/Caches/<AppName>
Unix: ~/.cache/<AppName> (XDG default)
Unix: ${PROJECT_HOME}/.cache/<AppName> or
${XDG_CACHE_HOME}/<AppName> or
~/.cache/<AppName>
Windows: C:\Users\<username>\AppData\Local\<AppName>\Cache
On Windows the only suggestion in the MSDN docs is that local settings go
Expand All @@ -64,7 +66,11 @@ def user_cache_dir(appname: str) -> str:
path = os.path.join(path, appname)
else:
# Get the base path
path = os.getenv("XDG_CACHE_HOME", expanduser("~/.cache"))
path = os.getenv("PROJECT_HOME")
if path is not None:
path = os.path.join(path, ".cache")
else:
path = os.getenv("XDG_CACHE_HOME", expanduser("~/.cache"))

# Add our app name to it
path = os.path.join(path, appname)
Expand All @@ -87,8 +93,9 @@ def user_data_dir(appname: str, roaming: bool = False) -> str:
Typical user data directories are:
macOS: ~/Library/Application Support/<AppName>
Unix: ~/.local/share/<AppName> # or in
$XDG_DATA_HOME, if defined
Unix: ${PROJECT_HOME}/.local/share/<AppName> or
${XDG_DATA_HOME}/<AppName> or
~/.local/share/<AppName>
Win XP (not roaming): C:\Documents and Settings\<username>\ ...
...Application Data\<AppName>
Win XP (roaming): C:\Documents and Settings\<username>\Local ...
Expand All @@ -105,9 +112,16 @@ def user_data_dir(appname: str, roaming: bool = False) -> str:
elif sys.platform == "darwin":
return os.path.join(expanduser("~/Library/Application Support/"), appname)
else:
return os.path.join(
os.getenv("XDG_DATA_HOME", expanduser("~/.local/share")), appname
)
# Get the base path
path = os.getenv("PROJECT_HOME")
if path is not None:
path = os.path.join(path, ".local/share")
else:
path = os.getenv("XDG_DATA_HOME", expanduser("~/.local/share"))

# Add our app name to it
path = os.path.join(path, appname)
return path


def user_config_dir(appname: str, roaming: bool = True) -> str:
Expand All @@ -124,7 +138,9 @@ def user_config_dir(appname: str, roaming: bool = True) -> str:
Typical user data directories are:
macOS: same as user_data_dir
Unix: ~/.config/<AppName>
Unix: ${PROJECT_HOME}/.config/<AppName> or
${XDG_CONFIG_HOME}/<AppName> or
~/.config/<AppName>
Win *: same as user_data_dir
For Unix, we follow the XDG spec and support $XDG_CONFIG_HOME.
Expand All @@ -135,7 +151,14 @@ def user_config_dir(appname: str, roaming: bool = True) -> str:
elif sys.platform == "darwin":
path = user_data_dir(appname)
else:
path = os.getenv("XDG_CONFIG_HOME", expanduser("~/.config"))
# Get the base path
path = os.getenv("PROJECT_HOME")
if path is not None:
path = os.path.join(path, ".config")
else:
path = os.getenv("XDG_CONFIG_HOME", expanduser("~/.config"))

# Add our app name to it
path = os.path.join(path, appname)

return path
Expand Down
71 changes: 71 additions & 0 deletions tests/utils/test_appdirs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from __future__ import annotations

from os import environ
from os import path
from sys import platform

from poetry.utils.appdirs import WINDOWS
from poetry.utils.appdirs import expanduser
from poetry.utils.appdirs import user_cache_dir
from poetry.utils.appdirs import user_config_dir
from poetry.utils.appdirs import user_data_dir


def test_user_cache_dir():
if WINDOWS or (platform == "darwin"):
return

environ["PROJECT_HOME"] = "/poetry"
environ["XDG_CACHE_HOME"] = "/xdg/.cache"
appname = "pypoetry"
default_user_cache_dir = path.join(expanduser("~/.cache"), appname)

assert user_cache_dir(appname) == path.join(
environ["PROJECT_HOME"], ".cache", appname
)

del environ["PROJECT_HOME"]
assert user_cache_dir(appname) == path.join(environ["XDG_CACHE_HOME"], appname)

del environ["XDG_CACHE_HOME"]
assert user_cache_dir(appname) == default_user_cache_dir


def test_user_config_dir():
if WINDOWS or (platform == "darwin"):
return

environ["PROJECT_HOME"] = "/poetry"
environ["XDG_CONFIG_HOME"] = "/xdg/.config"
appname = "pypoetry"
default_user_config_dir = path.join(expanduser("~/.config"), appname)

assert user_config_dir(appname) == path.join(
environ["PROJECT_HOME"], ".config", appname
)

del environ["PROJECT_HOME"]
assert user_config_dir(appname) == path.join(environ["XDG_CONFIG_HOME"], appname)

del environ["XDG_CONFIG_HOME"]
assert user_config_dir(appname) == default_user_config_dir


def test_user_data_dir():
if WINDOWS or (platform == "darwin"):
return

environ["PROJECT_HOME"] = "/poetry"
environ["XDG_DATA_HOME"] = "/xdg/.local/share"
appname = "pypoetry"
default_user_data_dir = path.join(expanduser("~/.local/share"), appname)

assert user_data_dir(appname) == path.join(
environ["PROJECT_HOME"], ".local/share", appname
)

del environ["PROJECT_HOME"]
assert user_data_dir(appname) == path.join(environ["XDG_DATA_HOME"], appname)

del environ["XDG_DATA_HOME"]
assert user_data_dir(appname) == default_user_data_dir

0 comments on commit be44643

Please sign in to comment.