-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.py
58 lines (38 loc) · 1.84 KB
/
files.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
57
58
import os
from dependency import Component
def get_work_folder() -> str:
home = os.path.expanduser("~")
work_folder = os.path.join(home, '.noticeCreator')
if not os.path.exists(work_folder):
os.mkdir(work_folder)
return work_folder
def get_cache_folder() -> str:
cache_folder = os.path.join(get_work_folder(), 'cache')
if not os.path.exists(cache_folder):
os.mkdir(cache_folder)
return cache_folder
def get_overwrite_folder() -> str:
overwrite_folder = os.path.join(get_work_folder(), 'overwrite')
if not os.path.exists(overwrite_folder):
os.mkdir(overwrite_folder)
return overwrite_folder
def make_file_name(component: Component) -> str:
return f'{component.type}_{component.provider}_{component.namespace}_{component.name}_{component.revision}.json'\
.replace('/', '_')
def is_component_cached(component: Component) -> bool:
return os.path.exists(os.path.join(get_cache_folder(), make_file_name(component)))
def is_component_overwritten(component: Component) -> bool:
return os.path.exists(os.path.join(get_overwrite_folder(), make_file_name(component)))
def write_component_to_cache(component: Component, data: str):
with open(os.path.join(get_cache_folder(), make_file_name(component)), 'w') as file:
file.write(data)
def write_component_to_overwritten(component: Component, data: str):
with open(os.path.join(get_overwrite_folder(), make_file_name(component)), 'w') as file:
file.write(data)
def read_file(name: str) -> str:
with open(name, 'r') as file:
return file.read()
def read_component(component: Component) -> str:
if is_component_overwritten(component):
return read_file(os.path.join(get_overwrite_folder(), make_file_name(component)))
return read_file(os.path.join(get_cache_folder(), make_file_name(component)))