-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
56 lines (44 loc) · 1.63 KB
/
util.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
54
55
56
import json
import os
from typing import Optional
from const import _LOGGER
import datetime
def settings_from_file(filename: str, config: dict = None) -> Optional[str]:
"""Reads/writes json from/to a filename."""
if config:
# We're writing configuration
try:
with open(filename, "w") as fdesc:
fdesc.write(json.dumps(config))
return True
except IOError as error:
_LOGGER.exception(error)
return False
else:
# We're reading config
if os.path.isfile(filename):
try:
with open(filename, "r") as fdesc:
return json.loads(fdesc.read())
except IOError as error:
_LOGGER.exception(error)
return False
else:
return {}
def get_modification_date(path: str):
# Return epoch time, in UTC offset 0
epoch = os.path.getmtime(path)
# return datetime.datetime.fromtimestamp(epoch).strftime('%c')
return datetime.datetime.fromtimestamp(epoch)
def is_file_expired(path: str) -> bool:
"""Checks if the file is expired. Use the last modification date if the file."""
if not (os.path.isfile(path)):
return True
modification_date = get_modification_date(path)
# current_date = datetime.datetime.now('UTC')
# current_date = datetime.datetime.utcnow()
current_date = datetime.datetime.now()
_LOGGER.info("modification_date: " + str(modification_date))
_LOGGER.info("current_date: " + str(current_date))
delta = current_date - modification_date
return delta >= datetime.timedelta(hours=24)