Skip to content

Commit 43dc2ba

Browse files
committed
Fixed issue with case sensitive choices for filters
modified: eodms_cli.py modified: scripts/config_util.py modified: scripts/csv_util.py modified: scripts/field.py modified: scripts/spatial.py modified: scripts/utils.py
1 parent c4784c3 commit 43dc2ba

File tree

6 files changed

+165
-26
lines changed

6 files changed

+165
-26
lines changed

eodms_cli.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ def ask_aoi(self, input_fn):
174174
err_msg = "Cannot open a Shapefile without GDAL. " \
175175
"Please install the GDAL Python package if " \
176176
"you'd like to use a Shapefile for your AOI."
177+
self.eod.print_msg(err_msg, heading='warning')
177178
self.logger.warning(err_msg)
178179
return None
179180

@@ -193,13 +194,15 @@ def ask_aoi(self, input_fn):
193194
elif any(s in input_fn for s in self.eod.aoi_extensions):
194195
err_msg = f"Input file {os.path.abspath(input_fn)} does not exist."
195196
# self.eod.print_support(err_msg)
197+
self.eod.print_msg(err_msg, heading="warning")
196198
self.logger.warning(err_msg)
197199
return None
198200

199201
else:
200202
if not self.eod.eodms_geo.is_wkt(input_fn):
201203
err_msg = "Input feature is not a valid WKT."
202204
# self.eod.print_support(err_msg)
205+
self.eod.print_msg(err_msg, heading="warning")
203206
self.logger.warning(err_msg)
204207
return None
205208

@@ -422,8 +425,11 @@ def ask_filter(self, filters):
422425
# field_map = self.eod.get_fieldMap()[coll_id]
423426

424427
print(f"\nAvailable fields for '{coll}':")
425-
for f in coll_fields.get_eod_fieldnames():
426-
print(f" {f}")
428+
# for f in coll_fields.get_eod_fieldnames():
429+
# print(f" {f}")
430+
avail_fields = coll_fields.get_eod_fieldnames(True)
431+
fields_str = ', '.join(avail_fields)
432+
print(self.wrap_text(fields_str, init_indent=' '))
427433

428434
print(self.wrap_text(f"\nFilters must be entered in " \
429435
f"the format of {self.eod.var_colour}" \
@@ -537,6 +543,8 @@ def ask_filter(self, filters):
537543
coll_filters.append(f)
538544
filt_dict[coll_id] = coll_filters
539545

546+
# print(f"filt_dict: {filt_dict}")
547+
540548
return filt_dict
541549

542550
def ask_input_file(self, input_fn, msg):
@@ -888,14 +896,24 @@ def ask_record_ids(self, ids):
888896
if ids is None or ids == '':
889897

890898
if not self.eod.silent:
891-
self.print_header("Enter Record ID(s)")
899+
self.print_header("Enter Record Id(s)")
892900

893901
msg = "\nEnter a single or set of Record IDs. Include the " \
894902
"Collection ID next to each ID separated by a " \
895903
"colon. Separate each ID with a comma. " \
896-
"(Ex: RCMImageProducts:7625368,NAPL:3736869)\n"
904+
f"(Ex: {self.eod.var_colour}RCMImageProducts:7625368" \
905+
f",NAPL:3736869{self.eod.reset_colour})\n"
897906
ids = self.get_input(msg, required=False)
898907

908+
process = self.eod.validate_record_ids(ids)
909+
910+
if not process:
911+
err_msg = "Invalid entry for the Record Ids."
912+
# self.eod.print_support(True, err_msg)
913+
self.eod.print_msg(err_msg, heading='error')
914+
self.logger.error(err_msg)
915+
self.eod.exit_cli(1)
916+
899917
return ids
900918

901919
def build_syntax(self):
@@ -1054,7 +1072,7 @@ def add_arrow(self):
10541072

10551073
return arrow
10561074

1057-
def wrap_text(self, in_str, width=105, sub_indent=' '):
1075+
def wrap_text(self, in_str, width=105, sub_indent=' ', init_indent=''):
10581076
"""
10591077
Wraps a given text to a certain width.
10601078
@@ -1069,6 +1087,7 @@ def wrap_text(self, in_str, width=105, sub_indent=' '):
10691087

10701088
out_str = textwrap.fill(in_str, width=width,
10711089
replace_whitespace=False,
1090+
initial_indent=init_indent,
10721091
subsequent_indent=sub_indent)
10731092
return out_str
10741093

scripts/config_util.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def ask_user(self, in_sect='all'):
192192
if sect_title not in self.config_dict.keys():
193193
err = f"The section '{sect_title}' does not exist in the " \
194194
f"configuration file."
195-
print(f"WARNING: {err}")
195+
self.eod.print_msg(err, heading="warning")
196196
self.logger.warning(err)
197197
return None
198198

@@ -214,7 +214,7 @@ def ask_user(self, in_sect='all'):
214214
if sect_opts is None:
215215
err = f"The section '{in_sect}' does not exist in the " \
216216
f"configuration file."
217-
print(f"WARNING: {err}")
217+
self.eod.print_msg(err, heading="warning")
218218
self.logger.warning(err)
219219
return None
220220

scripts/csv_util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def determine_collection(self, rec):
8484
self.coll_id = self.eod.get_collid_by_name(satellite)
8585
msg = f"The satellite/collection '{self.coll_id}'' is " \
8686
f"not supported with this script at this time."
87-
print(f"\n{msg}")
87+
self.eod.print_msg(msg, heading="warning")
8888
self.logger.warning(msg)
8989
return None
9090

scripts/field.py

Lines changed: 85 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class Field:
2222
- rapi_id: The field ID used in the RAPI (ex: RSAT2.IMAGE_ID)
2323
- rapi_title: The English title of the field in the RAPI.
2424
- ui_label: The label of the field used on the EODMS UI.
25+
- choices: The list of available choices for the field.
26+
- datatype: The data type of the field.
27+
- description: The description (or English title) of the field.
2528
"""
2629

2730
def __init__(self, **kwargs):
@@ -38,11 +41,20 @@ def __init__(self, **kwargs):
3841
The field name in the RAPI.
3942
* *ui_label* (``str``) --
4043
The field name found on the EODMS UI.
44+
* *choices* (``list`` or None) --
45+
The list of available choices for the field.
46+
* *datatype* (``str``) --
47+
The data type of the field.
48+
* *description* (``str``) --
49+
The description (or English title) of the field.
4150
"""
4251
self.eod_name = kwargs.get('eod_name')
4352
self.rapi_id = kwargs.get('rapi_id')
4453
self.rapi_title = kwargs.get('rapi_title')
4554
self.ui_label = kwargs.get('ui_label')
55+
self.choices = kwargs.get('choices')
56+
self.datatype = kwargs.get('datatype')
57+
self.description = kwargs.get('description')
4658

4759
def get_eod_name(self):
4860
"""
@@ -79,6 +91,54 @@ def get_ui_label(self):
7991
:rtype: str
8092
"""
8193
return self.ui_label
94+
95+
def get_choices(self, values_only=False):
96+
"""
97+
Gets the available choices for the field.
98+
99+
:return: A list of choices.
100+
:rtype: list or None
101+
"""
102+
if values_only:
103+
return [c.get('value') for c in self.choices]
104+
105+
return self.choices
106+
107+
def get_datatype(self):
108+
"""
109+
Gets the data type for the field.
110+
111+
:return: A list of choices.
112+
:rtype: str
113+
"""
114+
return self.datatype
115+
116+
def get_description(self):
117+
"""
118+
Gets the description for the field.
119+
120+
:return: A list of choices.
121+
:rtype: str
122+
"""
123+
return self.description
124+
125+
def verify_choices(self, in_val):
126+
"""
127+
Verifies a value based on the field's choices.
128+
129+
:param in_val: The value to check against the choices.
130+
:type in_val: str
131+
132+
:return: Either the proper choice or None.
133+
:rtype: str or None
134+
"""
135+
136+
choices_lw = [c.lower() for c in self.get_choices(True)]
137+
try:
138+
idx = choices_lw.index(in_val.lower())
139+
return self.get_choices(True)[idx]
140+
except ValueError:
141+
return None
82142

83143

84144
class CollFields:
@@ -102,12 +162,20 @@ def add_field(self, **kwargs):
102162
The field name in the RAPI.
103163
* *ui_label* (``str``) --
104164
The field name found on the EODMS UI.
165+
* *choices* (``list`` or None) --
166+
The list of available choices for the field.
167+
* *datatype* (``str``) --
168+
The data type of the field.
169+
* *description* (``str``) --
170+
The description (or English title) of the field.
105171
"""
106172

107-
self.fields.append(Field(eod_name=kwargs.get('eod_name'),
108-
rapi_id=kwargs.get('rapi_id'),
109-
rapi_title=kwargs.get('rapi_title'),
110-
ui_label=kwargs.get('ui_label')))
173+
# self.fields.append(Field(eod_name=kwargs.get('eod_name'),
174+
# rapi_id=kwargs.get('rapi_id'),
175+
# rapi_title=kwargs.get('rapi_title'),
176+
# ui_label=kwargs.get('ui_label'),
177+
# choices=kwargs.get('choices')))
178+
self.fields.append(Field(**kwargs))
111179

112180
# def add_general_fields(self):
113181
# """
@@ -139,14 +207,18 @@ def add_field(self, **kwargs):
139207
# rapi_title='Spatial Resolution',
140208
# ui_label='Pixel Spacing (Metres)')
141209

142-
def get_eod_fieldnames(self):
210+
def get_eod_fieldnames(self, sort=False):
143211
"""
144212
Gets the list of EOD fieldnames.
145213
146214
:return: A list of EOD filenames.
147215
:rtype: list
148216
"""
149-
return [f.get_eod_name() for f in self.fields]
217+
218+
if sort:
219+
return sorted([f.get_eod_name() for f in self.fields])
220+
else:
221+
return [f.get_eod_name() for f in self.fields]
150222

151223
def get_field(self, eod_name):
152224
"""
@@ -190,6 +262,10 @@ def map_fields(self):
190262

191263
if not vals.get('displayed'): continue
192264

265+
choices = vals.get('choices')
266+
datatype = vals.get('datatype')
267+
description = vals.get('description')
268+
193269
rapi_id = vals['id']
194270
rapi_title = key
195271
ui_label = rapi_title
@@ -241,7 +317,9 @@ def map_fields(self):
241317
eod_name = eod_name.strip().upper().replace(' ', '_')
242318

243319
coll_fields.add_field(eod_name=eod_name, rapi_id=rapi_id,
244-
rapi_title=rapi_title, ui_label=ui_label)
320+
rapi_title=rapi_title, ui_label=ui_label,
321+
choices=choices, datatype=datatype,
322+
description=description)
245323

246324
if coll_id == 'Radarsat1':
247325
for key in ['Radarsat1', 'R1', 'RS1']:

scripts/spatial.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ def _check_ogr(self):
6565
if ogr.__doc__ is not None and \
6666
ogr.__doc__.find("Module providing one api for multiple git "
6767
"services") > -1:
68-
print("Another package named 'ogr' is installed.")
68+
self.eod.print_msg("Another package named 'ogr' is installed.",
69+
heading='warning')
6970
return False
7071

7172
return True
@@ -189,7 +190,7 @@ def export_results(self, img_lst, out_fn='results.geojson'):
189190
warn_msg = "The format type for the output geospatial file " \
190191
"could not be determined. No geospatial " \
191192
"output will be created."
192-
print(f"\n{warn_msg}")
193+
self.eod.print_msg(warn_msg, heading='warning')
193194
return None
194195

195196
# Create the output Driver
@@ -246,7 +247,8 @@ def export_results(self, img_lst, out_fn='results.geojson'):
246247
f"'{ext_str}' format. Exporting results as a " \
247248
f"GeoJSON."
248249

249-
print(f"\n{warn_msg}")
250+
# print(f"\n{warn_msg}")
251+
self.eod.print_msg(warn_msg, heading='warning')
250252
self.logger.warning(warn_msg)
251253

252254
out_fn = out_fn.replace(ext, '.geojson')

0 commit comments

Comments
 (0)