-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcl_project.py
232 lines (180 loc) · 5.92 KB
/
cl_project.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# -*- coding: utf-8 -*-
# ----------------------------------------------------------------------------------------------------------------------
# Climate information dashboard.
#
# Class definition: Project and Projects.
#
# Contributors:
# 1. [email protected]
# (C) 2021-2022 Ouranos Inc., Canada
# ----------------------------------------------------------------------------------------------------------------------
# External libraries.
from typing import List, Union, Optional
# Dashboard libraries.
import cl_auth
import cl_gd
import cl_object
from cl_constant import const as c
from cl_context import cntx
from cl_stat import Stat, Stats
class Project(cl_object.Obj):
"""
--------------------------------------------------------------------------------------------------------------------
Class defining the object Project.
--------------------------------------------------------------------------------------------------------------------
"""
# Path of .geogjson file defining region boundaries.
_p_bounds = c.F_BOUNDS
# Directory holding data (on local ore remote drive).
_d_data = ""
# Cloud drive.
drive = None
def __init__(
self,
code: str = ""
):
"""
----------------------------------------
Constructor.
----------------------------------------
"""
super(Project, self).__init__(code=code, desc=code)
# Read project-specific configuration file.
if cntx.code == c.PLATFORM_SCRIPT:
cntx.load()
if code != "":
# Add statistics.
self._stats = Stats()
for _ in range(2):
self._stats.add(Stat(c.STAT_CENTILE, -1))
# Initialize cloud service.
self._d_data = str(cl_auth.path(code))
if "gd:" in self._d_data:
self._d_data = self._d_data.replace("gd:", "")
self.drive = cl_gd.GoogleDrive(self._d_data)
else:
self._d_data = c.D_DATA
@property
def d_data(
self
) -> str:
"""
----------------------------------------
Get the base directory of project.
Returns
-------
str
Local drive: Base directory of project.
Cloud drive: Directory ID.
----------------------------------------
"""
return self._d_data
@property
def p_bounds(
self
) -> str:
"""
----------------------------------------
Get the path of the .geojson file defining region boundaries.
Returns
-------
str
Local drive: Path of the .geojson file defining region boundaries.
Cloud drive: File ID.
----------------------------------------
"""
p = ""
# Codes.
project_code = cntx.project.code if cntx.project is not None else ""
# Base directory.
p_base = str(cl_auth.path(project_code))
if cntx.code in [c.PLATFORM_STREAMLIT, c.PLATFORM_JUPYTER]:
if self.drive is None:
p = p_base + "/" + project_code + "/<view_code>/" + self._p_bounds
p = p.replace("<view_code>", cntx.view.code)
else:
p_l = list(cntx.files(project_code + "/" + cntx.view.code + "/" + self._p_bounds)[cl_gd.PROP_ITEM_ID])
if len(p_l) > 0:
p = p_l[0]
else:
p = self._p_bounds
return p
@p_bounds.setter
def p_bounds(
self,
p_bounds: str
):
"""
----------------------------------------
Set the path of the .geojson file defining region boundaries.
Parameters
----------
p_bounds: str
Path of the .geojson file defining region boundaries.
----------------------------------------
"""
self._p_bounds = p_bounds
class Projects(cl_object.Objs):
"""
--------------------------------------------------------------------------------------------------------------------
Class defining the object Projects.
--------------------------------------------------------------------------------------------------------------------
"""
def __init__(
self,
code: Union[str, List[str]] = ""
):
"""
----------------------------------------
Constructor.
----------------------------------------
"""
super(Projects, self).__init__()
if code == "*":
self.load()
elif code != "":
self.add(code)
def load(
self
):
"""
----------------------------------------
Load items (all possibilities).
----------------------------------------
"""
# List projects.
code_l = str(cl_auth.Auth.projects)
if ";" in code_l:
code_l = code_l.split(";")
if "context" in code_l:
code_l.remove("context")
# Sort.
if isinstance(code_l, List):
code_l.sort()
self.add(code_l)
def add(
self,
item: Union[str, List[str], Project],
inplace: Optional[bool] = True
):
"""
----------------------------------------
Add one or several items.
Parameters
----------
item: Union[str, List[str], Project]
Item (code, list of codes or instance of Project).
inplace: Optional[bool]
If True, modifies the current instance.
----------------------------------------
"""
items = []
if isinstance(item, Project):
items = [item]
else:
code_l = item
if isinstance(item, str):
code_l = [item]
for i in range(len(code_l)):
items.append(Project(code_l[i]))
return super(Projects, self).add(items, inplace)