Skip to content

Commit

Permalink
store: clean up imports/layers
Browse files Browse the repository at this point in the history
Change-Id: I09700ade00f08d46160b4a772f3bb443fad4b6f4
  • Loading branch information
mo-ki committed Oct 25, 2023
1 parent 2c78300 commit db117b8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 20 deletions.
24 changes: 20 additions & 4 deletions cmk/gui/node_visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,12 @@ def _delete_topology_configuration(topology_configuration: TopologyConfiguration
return

try:
data = store.try_load_file_from_pickle_cache(topology_settings_lookup, default={})
data = store.try_load_file_from_pickle_cache(
topology_settings_lookup,
default={},
temp_dir=cmk.utils.paths.tmp_dir,
root_dir=cmk.utils.paths.omd_root,
)
except json.JSONDecodeError:
data = {}

Expand All @@ -211,7 +216,12 @@ def _save_topology_configuration(topology_configuration: TopologyConfiguration)
topology_settings_lookup.touch()

try:
data = store.try_load_file_from_pickle_cache(topology_settings_lookup, default={})
data = store.try_load_file_from_pickle_cache(
topology_settings_lookup,
default={},
temp_dir=cmk.utils.paths.tmp_dir,
root_dir=cmk.utils.paths.omd_root,
)
except json.JSONDecodeError:
data = {}

Expand All @@ -234,7 +244,10 @@ def _get_topology_frontend_configuration_for_filter(
return {}

found_topology_hash = store.try_load_file_from_pickle_cache(
topology_settings_lookup, default={}
topology_settings_lookup,
default={},
temp_dir=cmk.utils.paths.tmp_dir,
root_dir=cmk.utils.paths.omd_root,
).get(query_identifier.identifier)
if found_topology_hash is None:
return {}
Expand Down Expand Up @@ -1410,7 +1423,10 @@ def cleanup_topology_layouts() -> None:

with locked(topology_settings_lookup):
topology_settings: dict[str, str] = store.try_load_file_from_pickle_cache(
topology_settings_lookup, default={}
topology_settings_lookup,
default={},
temp_dir=cmk.utils.paths.tmp_dir,
root_dir=cmk.utils.paths.omd_root,
)
reverse_lookup: dict[str, str] = {y: x for x, y in topology_settings.items()}
paths = sorted(
Expand Down
8 changes: 7 additions & 1 deletion cmk/gui/pagetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

from typing_extensions import TypedDict

import cmk.utils.paths
import cmk.utils.store as store
from cmk.utils.exceptions import MKGeneralException
from cmk.utils.user import UserId
Expand Down Expand Up @@ -914,7 +915,12 @@ def load_raw(cls) -> Mapping[InstanceId, dict[str, object]]:
if not userdb.user_exists(user_id):
continue

user_pages = store.try_load_file_from_pickle_cache(path, default={})
user_pages = store.try_load_file_from_pickle_cache(
path,
default={},
temp_dir=cmk.utils.paths.tmp_dir,
root_dir=cmk.utils.paths.omd_root,
)
for name, page_dict in user_pages.items():
page_dict["owner"] = user_id
page_dict["name"] = name
Expand Down
16 changes: 13 additions & 3 deletions cmk/gui/visuals/_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,9 @@ def load_visuals_of_a_user(
user_id: UserId,
) -> CustomUserVisuals[TVisual]:
user_visuals: CustomUserVisuals[TVisual] = {}
for name, raw_visual in store.try_load_file_from_pickle_cache(path, default={}).items():
for name, raw_visual in store.try_load_file_from_pickle_cache(
path, default={}, temp_dir=cmk.utils.paths.tmp_dir, root_dir=cmk.utils.paths.omd_root
).items():
visual = internal_to_runtime_transformer(raw_visual)
visual["owner"] = user_id
visual["name"] = name
Expand All @@ -277,7 +279,10 @@ def _get_packaged_visuals(

try:
for name, raw_visual in store.try_load_file_from_pickle_cache(
dirpath, default={}
dirpath,
default={},
temp_dir=cmk.utils.paths.tmp_dir,
root_dir=cmk.utils.paths.omd_root,
).items():
visual = internal_to_runtime_transformer(raw_visual)
visual["owner"] = UserId.builtin()
Expand Down Expand Up @@ -409,7 +414,12 @@ def declare_packaged_visuals_permissions(what: VisualTypeName) -> None:
if dirpath.is_dir():
continue

for name, visual in store.try_load_file_from_pickle_cache(dirpath, default={}).items():
for name, visual in store.try_load_file_from_pickle_cache(
dirpath,
default={},
temp_dir=cmk.utils.paths.tmp_dir,
root_dir=cmk.utils.paths.omd_root,
).items():
visual["packaged"] = True
declare_packaged_visual_permission(what, name, visual)
except Exception:
Expand Down
15 changes: 3 additions & 12 deletions cmk/utils/store/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from pathlib import Path
from typing import Any

import cmk.utils.paths
from cmk.utils.exceptions import MKGeneralException, MKTerminate, MKTimeout
from cmk.utils.i18n import _
from cmk.utils.store._file import (
Expand Down Expand Up @@ -248,14 +247,6 @@ def _write(path: Path, serializer: Serializer, content: Any) -> None:
store.write_obj(content)


def _default_temp_dir() -> Path:
return cmk.utils.paths.tmp_dir


def _default_root_dir() -> Path:
return cmk.utils.paths.omd_root


def _pickled_files_cache_dir(temp_dir: Path) -> Path:
return temp_dir / "pickled_files_cache"

Expand All @@ -268,8 +259,8 @@ def try_load_file_from_pickle_cache(
*,
default: Any,
lock: bool = False,
temp_dir: Path = _default_temp_dir(),
root_dir: Path = _default_root_dir(),
temp_dir: Path,
root_dir: Path,
) -> Any:
"""Try to load a pickled version of the requested file from cache, otherwise load `path`
Expand Down Expand Up @@ -323,6 +314,6 @@ def try_load_file_from_pickle_cache(
return data


def clear_pickled_files_cache(temp_dir: Path = _default_temp_dir()) -> None:
def clear_pickled_files_cache(temp_dir: Path) -> None:
"""Remove all cached pickle files"""
shutil.rmtree(_pickled_files_cache_dir(temp_dir), ignore_errors=True)

0 comments on commit db117b8

Please sign in to comment.