Skip to content

Commit

Permalink
#165 Use with_filelock from tum-esm-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
dostuffthatmatters committed May 3, 2023
1 parent 2700832 commit f208e4a
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 50 deletions.
19 changes: 15 additions & 4 deletions packages/cli/commands/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import shutil
import click
import os

import tum_esm_utils
from packages.core import types
from packages.core.utils import with_filelock, update_dict_recursively
from packages.core.utils import update_dict_recursively

_dir = os.path.dirname
_PROJECT_DIR = _dir(_dir(_dir(_dir(os.path.abspath(__file__)))))
Expand All @@ -25,7 +27,10 @@ def _print_red(text: str) -> None:
@click.command(
help="Read the current config.json file. If it does not exist, use the config.default.json as the config.json. The command validates the structure of the config.json but skips verifying filepath existence."
)
@with_filelock(_CONFIG_LOCK_PATH)
@tum_esm_utils.decorators.with_filelock(
lockfile_path=_CONFIG_LOCK_PATH,
timeout=5,
)
def _get_config() -> None:
if not os.path.isfile(_CONFIG_FILE_PATH):
shutil.copyfile(_DEFAULT_CONFIG_FILE_PATH, _CONFIG_FILE_PATH)
Expand All @@ -44,7 +49,10 @@ def _get_config() -> None:
help=f"Update config. Only a subset of the required config variables has to be passed. The non-occuring values will be reused from the current config.\n\nThe required schema can be found in the documentation (user guide -> usage).",
)
@click.argument("content", default="{}")
@with_filelock(_CONFIG_LOCK_PATH)
@tum_esm_utils.decorators.with_filelock(
lockfile_path=_CONFIG_LOCK_PATH,
timeout=5,
)
def _update_config(content: str) -> None:
# try to load the dict
try:
Expand Down Expand Up @@ -79,7 +87,10 @@ def _update_config(content: str) -> None:
@click.command(
help=f"Validate the current config.json file.\n\nThe required schema can be found in the documentation (user guide -> usage). This validation will check filepath existence."
)
@with_filelock(_CONFIG_LOCK_PATH)
@tum_esm_utils.decorators.with_filelock(
lockfile_path=_CONFIG_LOCK_PATH,
timeout=5,
)
def _validate_current_config() -> None:
# load the current json file
try:
Expand Down
9 changes: 7 additions & 2 deletions packages/cli/commands/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import click
import os
from packages.core.utils import with_filelock, Logger

import tum_esm_utils
from packages.core.utils import Logger

_dir = os.path.dirname
_PROJECT_DIR = _dir(_dir(_dir(_dir(os.path.abspath(__file__)))))
Expand All @@ -21,7 +23,10 @@ def _print_red(text: str) -> None:

@click.command(help="Read the current info.log or debug.log file.")
@click.option("--level", default="INFO", help="Log level INFO or DEBUG")
@with_filelock(_LOG_FILES_LOCK)
@tum_esm_utils.decorators.with_filelock(
lockfile_path=_LOG_FILES_LOCK,
timeout=5,
)
def _read_logs(level: str) -> None:
if level in ["INFO", "DEBUG"]:
with open(_INFO_LOG_FILE if level == "INFO" else _DEBUG_LOG_FILE, "r") as f:
Expand Down
7 changes: 6 additions & 1 deletion packages/cli/commands/plc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from typing import Callable, Optional
import click
import os

import tum_esm_utils
from packages.core import types, utils, interfaces, modules

_dir = os.path.dirname
Expand Down Expand Up @@ -116,7 +118,10 @@ def _set_cover_angle(angle: str) -> None:
plc_interface.disconnect()


@utils.with_filelock(_CONFIG_LOCK_PATH)
@tum_esm_utils.decorators.with_filelock(
lockfile_path=_CONFIG_FILE_PATH,
timeout=5,
)
def _enable_user_control_in_config() -> None:
with open(_CONFIG_FILE_PATH, "r") as f:
config = json.load(f)
Expand Down
7 changes: 6 additions & 1 deletion packages/cli/commands/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import json
import click
import os

import tum_esm_utils
from packages.core import utils, interfaces

_dir = os.path.dirname
Expand All @@ -13,7 +15,10 @@

@click.command(help="Read the current state.json file.")
@click.option("--indent", is_flag=True, help="Print the JSON in an indented manner")
@utils.with_filelock(_STATE_LOCK_PATH)
@tum_esm_utils.decorators.with_filelock(
lockfile_path=_STATE_LOCK_PATH,
timeout=5,
)
def _get_state(indent: bool) -> None:
if not os.path.isfile(_STATE_FILE_PATH):
interfaces.StateInterface.initialize()
Expand Down
8 changes: 6 additions & 2 deletions packages/core/interfaces/config_interface.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import json
import os
from typing import Any
from packages.core import types, utils
import tum_esm_utils
from packages.core import types

_dir = os.path.dirname
_PROJECT_DIR = _dir(_dir(_dir(_dir(os.path.abspath(__file__)))))
Expand All @@ -12,7 +13,10 @@

class ConfigInterface:
@staticmethod
@utils.with_filelock(_CONFIG_LOCK_PATH, timeout=10)
@tum_esm_utils.decorators.with_filelock(
lockfile_path=_CONFIG_LOCK_PATH,
timeout=10,
)
def read() -> types.ConfigDict:
"""
Read the contents of the current config.json file.
Expand Down
27 changes: 22 additions & 5 deletions packages/core/interfaces/state_interface.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json
import os
import shutil

import tum_esm_utils
from packages.core import types, utils

_dir = os.path.dirname
Expand Down Expand Up @@ -99,7 +101,10 @@ class StateInterface:
```"""

@staticmethod
@utils.with_filelock(_STATE_LOCK_PATH, timeout=10)
@tum_esm_utils.decorators.with_filelock(
lockfile_path=_STATE_LOCK_PATH,
timeout=10,
)
def initialize() -> None:
"""Clear `state.json`, create empty `prersistent-state.json` if
it does not exist yet."""
Expand All @@ -119,7 +124,10 @@ def initialize() -> None:
json.dump(_EMPTY_PERSISTENT_STATE_OBJECT, f, indent=4)

@staticmethod
@utils.with_filelock(_STATE_LOCK_PATH, timeout=10)
@tum_esm_utils.decorators.with_filelock(
lockfile_path=_STATE_LOCK_PATH,
timeout=10,
)
def read() -> types.StateDict:
"""Read the state file and return its content"""
return StateInterface.read_without_filelock()
Expand All @@ -139,7 +147,10 @@ def read_without_filelock() -> types.StateDict:
return _EMPTY_STATE_OBJECT

@staticmethod
@utils.with_filelock(_STATE_LOCK_PATH, timeout=10)
@tum_esm_utils.decorators.with_filelock(
lockfile_path=_STATE_LOCK_PATH,
timeout=10,
)
def read_persistent() -> types.PersistentStateDict:
"""Read the persistent state file and return its content"""
return StateInterface.read_persistent_without_filelock()
Expand All @@ -159,7 +170,10 @@ def read_persistent_without_filelock() -> types.PersistentStateDict:
return _EMPTY_PERSISTENT_STATE_OBJECT

@staticmethod
@utils.with_filelock(_STATE_LOCK_PATH, timeout=10)
@tum_esm_utils.decorators.with_filelock(
lockfile_path=_STATE_LOCK_PATH,
timeout=10,
)
def update(update: types.StateDictPartial) -> None:
"""Update the (persistent) state file and return its content.
The update object should only include the properties to be
Expand All @@ -171,7 +185,10 @@ def update(update: types.StateDictPartial) -> None:
json.dump(new_state, f, indent=4)

@staticmethod
@utils.with_filelock(_STATE_LOCK_PATH, timeout=10)
@tum_esm_utils.decorators.with_filelock(
lockfile_path=_STATE_LOCK_PATH,
timeout=10,
)
def update_persistent(update: types.PersistentStateDictPartial) -> None:
"""Update the (persistent) state file and return its content.
The update object should only include the properties to be
Expand Down
1 change: 0 additions & 1 deletion packages/core/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
from .exception_email_client import ExceptionEmailClient
from .update_dict_recursively import update_dict_recursively
from .helios_image_processing import HeliosImageProcessing
from .with_filelock import with_filelock
34 changes: 0 additions & 34 deletions packages/core/utils/with_filelock.py

This file was deleted.

0 comments on commit f208e4a

Please sign in to comment.