Skip to content

Commit

Permalink
remove caching (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
christinahedges authored Jul 23, 2024
1 parent c43d645 commit 334a92e
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 172 deletions.
1 change: 0 additions & 1 deletion .github/workflows/black.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ jobs:
with:
options: "--check --verbose"
src: "./src"
version: "22.12.0"
5 changes: 1 addition & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,4 @@ jobs:
poetry install --with dev
- name: Test with pytest
run: |
make pytest
- name: Deploy website
run: |
make deploy
make pytest
13 changes: 1 addition & 12 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ aiohttp = "^3.8.6"
asyncio = "^3.4.3"
aiobotocore = "^2.7.0"
rich = "^13.7.0"
joblib = "^1.3.2"

[tool.poetry.group.dev]
optional = true
Expand Down
57 changes: 57 additions & 0 deletions src/tessrip/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
__version__ = "0.1.0dev"
# Standard library
import os # noqa
import tempfile

PACKAGEDIR = os.path.abspath(os.path.dirname(__file__))

Expand All @@ -10,5 +11,61 @@
DATA_OFFSET = HDR_SIZE * 2
BUCKET_NAME = "stpubdata"


import logging # noqa: E402

# This library lets us have log messages with syntax highlighting
from rich.logging import RichHandler # noqa: E402


def get_logger():
"""Configure and return a logger with RichHandler."""
logger = logging.getLogger(__name__)
logger.setLevel(logging.WARN)

# Add RichHandler
rich_handler = RichHandler()
rich_handler.setFormatter(
logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
)

logger.addHandler(rich_handler)
return logger


# Make sure that we have a directory we can put package config in
def check_package_cache_dir(package_cache_dir):
if not os.path.exists(package_cache_dir):
try:
os.makedirs(package_cache_dir) # Try creating the package directory
except OSError as e:
raise PermissionError(
f"Unable to create package cache directory at {package_cache_dir}."
) from e

# Check if the directory is writable
try:
with tempfile.NamedTemporaryFile(dir=package_cache_dir, delete=False) as fp:
fp.write(b"Hello world!")
fp.close()
with open(fp.name, mode="rb") as f:
f.read()
os.unlink(fp.name)
except OSError as e:
raise PermissionError(
f"The directory {package_cache_dir} is not writable."
) from e
return package_cache_dir


_package_cache_dir = check_package_cache_dir(
os.path.join(os.path.expanduser("~"), ".tessrip")
)

# Make sure we can load the config
from .config import load_config # noqa

load_config()

from .query import Rip # noqa
from .utils import get_FFI # noqa
109 changes: 17 additions & 92 deletions src/tessrip/config.py
Original file line number Diff line number Diff line change
@@ -1,118 +1,43 @@
import configparser
# Standard library
import logging # noqa: E402
import os
import tempfile

# This library lets us have log messages with syntax highlighting
from rich.logging import RichHandler # noqa: E402


def get_logger():
"""Configure and return a logger with RichHandler."""
logger = logging.getLogger(__name__)
logger.setLevel(logging.WARN)

# Add RichHandler
rich_handler = RichHandler()
rich_handler.setFormatter(
logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
)

logger.addHandler(rich_handler)
return logger

from . import _package_cache_dir, get_logger

log = get_logger()


def check_cache_dir(cache_dir):
if not os.path.exists(cache_dir):
try:
os.makedirs(cache_dir) # Try creating the cache directory
except OSError as e:
raise PermissionError(
f"Unable to create cache directory at {cache_dir}."
) from e

# Check if the directory is writable
try:
with tempfile.NamedTemporaryFile(dir=cache_dir, delete=False) as fp:
fp.write(b"Hello world!")
fp.close()
with open(fp.name, mode="rb") as f:
f.read()
except OSError as e:
raise PermissionError(
f"The cache directory at {cache_dir} is not writable."
) from e
return cache_dir
CONFIG_FILE_PATH = os.path.join(_package_cache_dir, "config.ini")


def make_default_config():
config = configparser.ConfigParser()
config["CacheSettings"] = {
"use_disk_cache": "False",
"cache_dir": _default_cache_dir,
}
with open(_config_file_path, "w") as f:
config["Settings"] = {}
log.debug("Writing default config file.")
with open(CONFIG_FILE_PATH, "w") as f:
config.write(f)
log.debug("Writen.")


def load_config():
config = configparser.ConfigParser()
if not os.path.exists(_config_file_path):
if not os.path.exists(CONFIG_FILE_PATH):
log.debug("No configuration file found, creating the default file.")
make_default_config()
else:
config.read(_config_file_path)
config.read(CONFIG_FILE_PATH)
return config


def save_config(config):
with open(_config_file_path, "w") as f:
with open(CONFIG_FILE_PATH, "w") as f:
config.write(f)


def set_use_disk_cache(use_disk_cache: bool):
"""Set whether to cache on disk. Default is False."""
try:
config = load_config()
if not isinstance(use_disk_cache, bool):
raise ValueError("`use_disk_cache` must been type `bool`.")
config["CacheSettings"]["use_disk_cache"] = use_disk_cache
save_config(config)
except:
log.warning(
"Can not update config parameters. Setting `USE_DISK_CACHE` in this session."
)
global USE_DISK_CACHE
USE_DISK_CACHE = use_disk_cache


def set_disk_cache_directory(cache_directory):
try:
config = load_config()
if not isinstance(cache_directory, str):
raise ValueError("`cache_directory` must been type `str`.")
check_cache_dir(cache_directory)
config["CacheSettings"]["cache_dir"] = cache_directory
save_config(config)
except:
log.warning(
"Can not update config parameters. Setting `CACHE_DIR` in this session."
)
global CACHE_DIR
CACHE_DIR = cache_directory

def set_config_parameter(parameter_name: str, parameter_value: str):
"""Update a configuration parameter"""
config = load_config()
config["Settings"][parameter_name] = f"{parameter_value}"
save_config(config)

_default_cache_dir = check_cache_dir(os.path.join(os.path.expanduser("~"), ".tessrip"))
_config_file_path = os.path.join(_default_cache_dir, "config.ini")

USE_DISK_CACHE = load_config()["CacheSettings"]["use_disk_cache"].lower() in [
"true",
"y",
"yes",
"on",
"disk",
]
CACHE_DIR = load_config()["CacheSettings"]["cache_dir"]
def get_config_parameter(parameter_name: str):
return load_config()["Settings"][parameter_name]
Loading

0 comments on commit 334a92e

Please sign in to comment.