Skip to content

Commit

Permalink
Export cache_directory and rest_url from utils (#11)
Browse files Browse the repository at this point in the history
* Export cache directory and rest url functions
* update package meta
  • Loading branch information
jkanche authored May 20, 2024
1 parent 08e30eb commit 6733adf
Show file tree
Hide file tree
Showing 22 changed files with 157 additions and 110 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[metadata]
name = gypsum-client
description = Python cline to gypsum
description = Python client to gypsum REST API
author = Jayaram Kancherla
author_email = [email protected]
license = MIT
Expand Down
2 changes: 2 additions & 0 deletions src/gypsum_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@


from .auth import access_token, set_access_token
from .cache_directory import cache_directory
from .clone_operations import clone_version
from .config import REQUESTS_MOD
from .create_operations import create_project
Expand All @@ -36,6 +37,7 @@
from .refresh_operations import refresh_latest, refresh_usage
from .remove_operations import remove_asset, remove_project, remove_version
from .resolve_links import resolve_links
from .rest_url import rest_url
from .s3_config import public_s3_config
from .save_operations import save_file, save_version
from .search_metadata import define_text_query, search_metadata_text
Expand Down
37 changes: 1 addition & 36 deletions src/gypsum_client/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import re
import shutil
import tempfile
from datetime import datetime, timezone
from pathlib import Path
from datetime import datetime
from typing import Optional
from urllib.parse import quote_plus

Expand All @@ -18,40 +17,6 @@
__license__ = "MIT"


def _rest_url(url: Optional[str] = None):
current = "https://gypsum.artifactdb.com"

if url is None:
return current
else:
return url


def _cache_directory(dir: Optional[str] = None):
current = None
if current is None:
_from_env = os.environ.get("GYPSUM_CACHE_DIR", None)
if _from_env is not None:
if not os.path.exists(_from_env):
raise FileNotFoundError(
f"Path {_from_env} does not exist or is not accessible."
)

current = _from_env
else:
current = os.path.join(str(Path.home()), "gypsum", "cache")

os.makedirs(current, exist_ok=True)

if dir is None:
return current
else:
if not os.path.exists(dir):
raise FileNotFoundError(f"Path {dir} does not exist or is not accessible.")

return dir


def _remove_slash_url(url: str):
if url.endswith("/"):
url = url.rstrip("/")
Expand Down
13 changes: 6 additions & 7 deletions src/gypsum_client/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
from filelock import FileLock

from ._github import github_access_token
from ._utils import _cache_directory, _is_interactive, _remove_slash_url, _rest_url
from ._utils import _is_interactive, _remove_slash_url
from .cache_directory import cache_directory
from .config import REQUESTS_MOD
from .rest_url import rest_url

__author__ = "Jayaram Kancherla"
__copyright__ = "Jayaram Kancherla"
Expand All @@ -23,7 +25,7 @@ def _token_cache_path(cache_dir):
def access_token(
full: bool = False,
request: bool = True,
cache_dir: Optional[str] = _cache_directory(),
cache_dir: Optional[str] = cache_directory(),
token_expiration_limit: int = 10,
) -> Optional[Union[str, dict]]:
"""Get GitHub access token for authentication to the gypsum API's.
Expand Down Expand Up @@ -69,7 +71,6 @@ def _token_func(x):
TOKEN_CACHE["auth_info"] = None

if cache_dir is not None:
cache_dir = _cache_directory(cache_dir)
cache_path = _token_cache_path(cache_dir)

if os.path.exists(cache_path):
Expand Down Expand Up @@ -99,12 +100,12 @@ def _token_func(x):

def set_access_token(
token: str = TOKEN_AUTO,
app_url: str = _rest_url(),
app_url: str = rest_url(),
app_key: Optional[str] = None,
app_secret: Optional[str] = None,
github_url: str = "https://api.github.com",
user_agent: Optional[str] = None,
cache_dir: Optional[str] = _cache_directory(),
cache_dir: Optional[str] = cache_directory(),
) -> dict:
"""Set GitHub access token for authentication to the gypsum API's.
Expand Down Expand Up @@ -141,7 +142,6 @@ def set_access_token(

cache_path = None
if cache_dir is not None:
cache_dir = _cache_directory(cache_dir)
cache_path = _token_cache_path(cache_dir)

if token is None:
Expand Down Expand Up @@ -211,7 +211,6 @@ def set_access_token(
if expires_header is not None:
expiry = float(expires_header.split(" ")[0])

cache_dir = _cache_directory(cache_dir)
if cache_dir is not None:
cache_path = _token_cache_path(cache_dir)
os.makedirs(os.path.dirname(cache_path), exist_ok=True)
Expand Down
52 changes: 52 additions & 0 deletions src/gypsum_client/cache_directory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import os
from pathlib import Path
from typing import Optional

__author__ = "Jayaram Kancherla"
__copyright__ = "Jayaram Kancherla"
__license__ = "MIT"

CURRENT_CACHE_DIRECTORY = None


def cache_directory(dir: Optional[str] = None):
"""Cache directory.
Specify the cache directory in the local filesystem
for gypsum-related data.
If the ``GYPSUM_CACHE_DIR`` environment variable is set
before the first call to ``cache_directory()``, it is used
as the initial location of the cache directory.
Otherwise, the initial location is set to user's home
directory defined by ``Path.home()``.
Args:
dir:
Path to the cache directory.
If `None`, a default cache location is used.
Returns:
Path to the cache directory.
"""
global CURRENT_CACHE_DIRECTORY

if CURRENT_CACHE_DIRECTORY is None:
_from_env = os.environ.get("GYPSUM_CACHE_DIR", None)
if _from_env is not None:
if not os.path.exists(_from_env):
raise FileNotFoundError(
f"Path {_from_env} does not exist or is not accessible."
)

CURRENT_CACHE_DIRECTORY = _from_env
else:
CURRENT_CACHE_DIRECTORY = os.path.join(str(Path.home()), "gypsum", "cache")
os.makedirs(CURRENT_CACHE_DIRECTORY, exist_ok=True)

if dir is not None:
if not os.path.exists(dir):
raise FileNotFoundError(f"Path {dir} does not exist or is not accessible.")
CURRENT_CACHE_DIRECTORY = dir

return CURRENT_CACHE_DIRECTORY
10 changes: 5 additions & 5 deletions src/gypsum_client/clone_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@
import os
import shutil

from ._utils import BUCKET_CACHE_NAME, _cache_directory, _rest_url
from ._utils import BUCKET_CACHE_NAME
from .cache_directory import cache_directory
from .fetch_operations import fetch_manifest
from .rest_url import rest_url
from .save_operations import save_version

__author__ = "Jayaram Kancherla"
Expand All @@ -57,8 +59,8 @@ def clone_version(
version: str,
destination: str,
download: bool = True,
cache_dir: str = _cache_directory(),
url: str = _rest_url(),
cache_dir: str = cache_directory(),
url: str = rest_url(),
**kwargs,
):
"""Clone a version's directory structure.
Expand Down Expand Up @@ -112,8 +114,6 @@ def clone_version(
Only used if ``download`` is `True`.
"""
cache_dir = _cache_directory(cache_dir)

if download:
save_version(project, asset, version, cache_dir=cache_dir, url=url, **kwargs)

Expand Down
5 changes: 3 additions & 2 deletions src/gypsum_client/create_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

import requests

from ._utils import _remove_slash_url, _rest_url, _sanitize_uploaders
from ._utils import _remove_slash_url, _sanitize_uploaders
from .auth import access_token
from .config import REQUESTS_MOD
from .rest_url import rest_url

__author__ = "Jayaram Kancherla"
__copyright__ = "Jayaram Kancherla"
Expand All @@ -19,7 +20,7 @@ def create_project(
baseline: int = None,
growth_rate: int = None,
year: int = None,
url: str = _rest_url(),
url: str = rest_url(),
token: str = None,
):
"""Create a new project with the associated permissions.
Expand Down
5 changes: 3 additions & 2 deletions src/gypsum_client/fetch_metadata_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
import requests
from filelock import FileLock

from ._utils import _cache_directory, _download_and_rename_file
from ._utils import _download_and_rename_file
from .cache_directory import cache_directory
from .config import REQUESTS_MOD

__author__ = "Jayaram Kancherla"
Expand Down Expand Up @@ -71,7 +72,7 @@ def fetch_metadata_database(
if cache_dir is None:
cache_path = tempfile.NamedTemporaryFile(suffix=".sqlite3").name
else:
cache_dir = os.path.join(_cache_directory(cache_dir), "databases")
cache_dir = os.path.join(cache_directory(cache_dir), "databases")

cache_path = os.path.join(cache_dir, name)
if not os.path.exists(cache_path):
Expand Down
4 changes: 2 additions & 2 deletions src/gypsum_client/fetch_metadata_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import requests
from filelock import FileLock

from ._utils import _cache_directory
from .cache_directory import cache_directory
from .config import REQUESTS_MOD

__author__ = "Jayaram Kancherla"
Expand Down Expand Up @@ -58,7 +58,7 @@ def fetch_metadata_schema(
if cache_dir is None:
cache_path = tempfile.mktemp(suffix=".json")
else:
cache_dir = os.path.join(_cache_directory(cache_dir), "schemas")
cache_dir = os.path.join(cache_directory(cache_dir), "schemas")

cache_path = os.path.join(cache_dir, name)
if not os.path.exists(cache_path):
Expand Down
20 changes: 10 additions & 10 deletions src/gypsum_client/fetch_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

from ._utils import (
BUCKET_CACHE_NAME,
_cache_directory,
_cast_datetime,
_fetch_cacheable_json,
_fetch_json,
_rest_url,
)
from .cache_directory import cache_directory
from .rest_url import rest_url

__author__ = "Jayaram Kancherla"
__copyright__ = "Jayaram Kancherla"
__license__ = "MIT"


def fetch_latest(project: str, asset: str, url: str = _rest_url()) -> str:
def fetch_latest(project: str, asset: str, url: str = rest_url()) -> str:
"""Fetch the latest version of a project's asset.
See Also:
Expand Down Expand Up @@ -48,9 +48,9 @@ def fetch_manifest(
project: str,
asset: str,
version: str,
cache_dir: str = _cache_directory(),
cache_dir: str = cache_directory(),
overwrite: bool = False,
url: str = _rest_url(),
url: str = rest_url(),
) -> dict:
"""Fetch the manifest for a version of an asset of a project.
Expand Down Expand Up @@ -102,7 +102,7 @@ def fetch_manifest(
)


def fetch_permissions(project: str, url: str = _rest_url()) -> dict:
def fetch_permissions(project: str, url: str = rest_url()) -> dict:
"""Fetch the permissions for a project.
See Also:
Expand Down Expand Up @@ -153,7 +153,7 @@ def fetch_permissions(project: str, url: str = _rest_url()) -> dict:
return perms


def fetch_quota(project: str, url: str = _rest_url()) -> dict:
def fetch_quota(project: str, url: str = rest_url()) -> dict:
"""Fetch the quota details for a project.
See Also:
Expand Down Expand Up @@ -185,9 +185,9 @@ def fetch_summary(
project: str,
asset: str,
version: str,
cache_dir: str = _cache_directory(),
cache_dir: str = cache_directory(),
overwrite: bool = False,
url: str = _rest_url(),
url: str = rest_url(),
) -> dict:
"""Fetch the summary for a version of an asset of a project.
Expand Down Expand Up @@ -247,7 +247,7 @@ def fetch_summary(
return _out


def fetch_usage(project: str, url: str = _rest_url()) -> int:
def fetch_usage(project: str, url: str = rest_url()) -> int:
"""Fetch the quota usage for a project.
See Also:
Expand Down
11 changes: 6 additions & 5 deletions src/gypsum_client/list_operations.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import requests

from ._utils import _list_for_prefix, _rest_url
from ._utils import _list_for_prefix
from .config import REQUESTS_MOD
from .rest_url import rest_url

__author__ = "Jayaram Kancherla"
__copyright__ = "Jayaram Kancherla"
__license__ = "MIT"


def list_projects(url: str = _rest_url()) -> list:
def list_projects(url: str = rest_url()) -> list:
"""List all projects in the gypsum backend.
Example:
Expand All @@ -27,7 +28,7 @@ def list_projects(url: str = _rest_url()) -> list:
return _list_for_prefix(prefix=None, url=url)


def list_assets(project: str, url: str = _rest_url()) -> list:
def list_assets(project: str, url: str = rest_url()) -> list:
"""List all assets in a project.
Example:
Expand All @@ -49,7 +50,7 @@ def list_assets(project: str, url: str = _rest_url()) -> list:
return _list_for_prefix(f"{project}/", url=url)


def list_versions(project: str, asset: str, url=_rest_url()) -> list:
def list_versions(project: str, asset: str, url: str = rest_url()) -> list:
"""List all versions for a project asset.
Example:
Expand Down Expand Up @@ -80,7 +81,7 @@ def list_files(
version: str,
prefix: str = None,
include_dot: bool = True,
url: str = _rest_url(),
url: str = rest_url(),
) -> list:
"""List all files for a specified version of a project and asset.
Expand Down
Loading

0 comments on commit 6733adf

Please sign in to comment.