-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
53 lines (44 loc) · 1.43 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python
# Copyright (c) 2022 SMHI, Swedish Meteorological and Hydrological Institute.
# License: MIT License (see LICENSE.txt or http://opensource.org/licenses/mit).
"""
Created on 2022-10-06 17:21
@author: johannes
"""
import os
from pathlib import Path
from time import monotonic_ns
from functools import lru_cache, wraps
def get_list_path():
"""Return the path to the station list.
Set environment variable to the versioned controlled station list file.
"""
return os.getenv(
'SHARK_STATION_LIST',
str(Path(__file__).parent.joinpath('handler/resources/station.txt'))
)
def timed_lru_cache(
_func=None, *,
seconds: int = 600,
maxsize: int = 128,
typed: bool = False
):
"""Cache function or method."""
def wrapper_cache(f):
f = lru_cache(maxsize=maxsize, typed=typed)(f)
f.delta = seconds * 10 ** 9
f.expiration = monotonic_ns() + f.delta
@wraps(f)
def wrapped_f(*args, **kwargs):
if monotonic_ns() >= f.expiration:
f.cache_clear()
f.expiration = monotonic_ns() + f.delta
return f(*args, **kwargs)
wrapped_f.cache_info = f.cache_info
wrapped_f.cache_clear = f.cache_clear
return wrapped_f
# To allow decorator to be used without arguments
if _func is None:
return wrapper_cache
else:
return wrapper_cache(_func)