1
+ ##############################################################################
2
+ #
3
+ # Copyright (c) His Majesty the King in Right of Canada, as
4
+ # represented by the Minister of Natural Resources, 2023
5
+ #
6
+ # Licensed under the MIT license
7
+ # (see LICENSE or <http://opensource.org/licenses/MIT>) All files in the
8
+ # project carrying such notice may not be copied, modified, or distributed
9
+ # except according to those terms.
10
+ #
11
+ ##############################################################################
12
+
1
13
import configparser
2
14
import os
3
15
import shutil
@@ -50,7 +62,10 @@ def __init__(self):
50
62
"# The minimum date the download files will "
51
63
"be kept; all files prior to this date will "
52
64
"be deleted (format = yyyy-mm-dd)" : None ,
53
- "keep_downloads" : '' },
65
+ "keep_downloads" : '' ,
66
+ "# Determines whether to use colours in the "
67
+ "CLI output" : None ,
68
+ "colourize" : 'True' },
54
69
"Credentials" :
55
70
{"# Username of the eodms account used to "
56
71
"access the rapi" : None ,
@@ -83,113 +98,6 @@ def __init__(self):
83
98
}
84
99
}
85
100
86
- # # The contents of the configuration file
87
- # self.config_contents = {'Paths':
88
- # [
89
- # {
90
- # 'name': 'downloads',
91
- # 'desc': 'Path of the image files downloaded from the '
92
- # 'RAPI; if blank, files will be saved in the '
93
- # 'script folder under "downloads"',
94
- # 'default': '',
95
- # 'value': None
96
- # },
97
- # {
98
- # 'name': 'results',
99
- # 'desc': 'Path of the results CSV files from the '
100
- # 'script; if blank, files will be saved in the '
101
- # 'script folder under "results"',
102
- # 'default': '',
103
- # 'value': None
104
- # },
105
- # {
106
- # 'name': 'log',
107
- # 'desc': 'Path of the log files; if blank, log files '
108
- # 'will be saved in the script folder under '
109
- # '"log"',
110
- # 'default': '',
111
- # 'value': None
112
- # }
113
- # ],
114
- # 'Script':
115
- # [
116
- #
117
- # {
118
- # 'name': 'keep_results',
119
- # 'desc': 'The minimum date the CSV result files will be '
120
- # 'kept; all files prior to this date will be '
121
- # 'deleted (format: yyyy-mm-dd)',
122
- # 'default': '',
123
- # 'value': None
124
- # },
125
- # {
126
- # 'name': 'keep_downloads',
127
- # 'desc': 'The minimum date the download files will be kept; '
128
- # 'all files prior to this date will be deleted '
129
- # '(format: yyyy-mm-dd)',
130
- # 'default': '',
131
- # 'value': None
132
- # }
133
- # ],
134
- # 'Credentials':
135
- # [
136
- # {
137
- # 'name': 'username',
138
- # 'desc': 'Username of the EODMS account used to access '
139
- # 'the RAPI',
140
- # 'default': '',
141
- # 'value': None
142
- # },
143
- # {
144
- # 'name': 'password',
145
- # 'desc': 'Password of the EODMS account used to access '
146
- # 'the RAPI',
147
- # 'default': '',
148
- # 'value': None
149
- # },
150
- # ],
151
- # 'RAPI':
152
- # [
153
- # {
154
- # 'name': 'access_attempts',
155
- # 'desc': 'Number of attempts made to the rapi when a '
156
- # 'timeout occurs',
157
- # 'default': 4,
158
- # 'value': None
159
- # },
160
- # {
161
- # 'name': 'max_results',
162
- # 'desc': 'Maximum number of results to return from the '
163
- # 'RAPI',
164
- # 'default': 1000,
165
- # 'value': None
166
- # },
167
- # {
168
- # 'name': 'timeout_query',
169
- # 'desc': 'Number of seconds before a timeout occurs '
170
- # 'when querying the RAPI',
171
- # 'default': 120.0,
172
- # 'value': None
173
- # },
174
- # {
175
- # 'name': 'timeout_order',
176
- # 'desc': 'Number of seconds before a timeout occurs '
177
- # 'when ordering using the RAPI',
178
- # 'default': 180.0,
179
- # 'value': None
180
- # },
181
- # {
182
- # 'name': 'order_check_date',
183
- # 'desc': 'When checking for AVAILABLE_FOR_DOWNLOAD '
184
- # 'orders, this date is the earliest they '
185
- # 'will be checked. Can be hours, days, '
186
- # 'months or years',
187
- # 'default': '3 days',
188
- # 'value': None
189
- # },
190
- # ]
191
- # }
192
-
193
101
def _set_dict (self , dict_sect , sections , option ):
194
102
"""
195
103
Sets a value in the config_dict based on an option from the config_info
@@ -212,8 +120,10 @@ def _set_dict(self, dict_sect, sections, option):
212
120
213
121
for sec in sections :
214
122
if self .config_info .has_option (sec , option ):
215
- self .config_dict [dict_sect ][option ] = self .config_info .get (
216
- sec , option )
123
+ val = self .config_info .get (sec , option )
124
+ if val .lower () == 'true' or val .lower () == 'false' :
125
+ val = eval (val )
126
+ self .config_dict [dict_sect ][option ] = val
217
127
218
128
def _ask_input (self , section , in_opts ):
219
129
@@ -282,7 +192,7 @@ def ask_user(self, in_sect='all'):
282
192
if sect_title not in self .config_dict .keys ():
283
193
err = f"The section '{ sect_title } ' does not exist in the " \
284
194
f"configuration file."
285
- print ( f"WARNING: { err } " )
195
+ self . eod . print_msg ( err , heading = "warning " )
286
196
self .logger .warning (err )
287
197
return None
288
198
@@ -304,7 +214,7 @@ def ask_user(self, in_sect='all'):
304
214
if sect_opts is None :
305
215
err = f"The section '{ in_sect } ' does not exist in the " \
306
216
f"configuration file."
307
- print ( f"WARNING: { err } " )
217
+ self . eod . print_msg ( err , heading = "warning " )
308
218
self .logger .warning (err )
309
219
return None
310
220
@@ -369,6 +279,16 @@ def ask_user(self, in_sect='all'):
369
279
#
370
280
# return out_str
371
281
282
+ def get_filename (self ):
283
+ """
284
+ Gets the filename and path of the configuration file being used.
285
+
286
+ :return: The path of the configuration file.
287
+ :rtype: str
288
+ """
289
+
290
+ return self .config_fn
291
+
372
292
def get_info (self ):
373
293
"""
374
294
Gets the configuration parser object
@@ -434,6 +354,7 @@ def update_dict(self):
434
354
435
355
self ._set_dict ('Script' , 'Script' , 'keep_results' )
436
356
self ._set_dict ('Script' , 'Script' , 'keep_downloads' )
357
+ self ._set_dict ('Script' , 'Script' , 'colourize' )
437
358
438
359
cr = ['Credentials' , 'RAPI' ] # For backwards compatibility
439
360
self ._set_dict ('Credentials' , cr , 'username' )
0 commit comments