From fb9370c366bef449d703239268e23243b37bdf61 Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Sat, 23 Jan 2021 17:31:38 -0600 Subject: [PATCH] Add a "trim" key to the bulk upload map --- src/pgwui_bulk_upload/exceptions.py | 9 +++++ src/pgwui_bulk_upload/views/bulk_upload.py | 43 +++++++++++++++++++--- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/pgwui_bulk_upload/exceptions.py b/src/pgwui_bulk_upload/exceptions.py index 3156a0a..7a06eae 100644 --- a/src/pgwui_bulk_upload/exceptions.py +++ b/src/pgwui_bulk_upload/exceptions.py @@ -169,6 +169,15 @@ class ExtraMapListTagError(BadMapFileError): f'number {count}') +class BadTrimValueError(BadMapFileError): + def __init__(self, filename, count, value): + super().__init__( + f'The "map_list" tag of map file ({filename}) contains a map ' + 'with a file_map value that has a non-boolean trim value', + f'The trim value must be a YAML boolean, ({value}) was supplied ' + f'for trim in map_list item number {count}') + + class MissingFileMapTagError(BadMapFileError): def __init__(self, filename, count, key): super().__init__( diff --git a/src/pgwui_bulk_upload/views/bulk_upload.py b/src/pgwui_bulk_upload/views/bulk_upload.py index 834285b..c456908 100644 --- a/src/pgwui_bulk_upload/views/bulk_upload.py +++ b/src/pgwui_bulk_upload/views/bulk_upload.py @@ -183,17 +183,19 @@ class UploadDir(): raise ex.BadMapfileError( self.uf['filename'], archive_path(yaml_file), exp) - def check_tag(self, errors, yaml_file, count, file_map, tag): + def check_tag(self, errors, yaml_file, count, file_map, tag, + required=True, string=True): '''Confirm that the tag exists and holds a string; remove the tag from the map ''' try: - if not isinstance(file_map[tag], str): + if not isinstance(file_map[tag], str) and string: errors.append(ex.MustBeStringError( archive_path(yaml_file), count, tag, file_map[tag])) except KeyError: - errors.append(ex.MissingFileMapTagError( - archive_path(yaml_file), count, tag)) + if required: + errors.append(ex.MissingFileMapTagError( + archive_path(yaml_file), count, tag)) else: del file_map[tag] @@ -203,16 +205,37 @@ class UploadDir(): for key in map_item: errors.append(execp(archive_path(yaml_file), count, key)) - def validate_file_map(self, errors, yaml_file, count, file_map): + def validate_key_existance(self, errors, yaml_file, count, file_map): '''Confirm that a file_map contains the right tags, if not add to errors ''' my_file_map = file_map.copy() self.check_tag(errors, yaml_file, count, my_file_map, 'file') self.check_tag(errors, yaml_file, count, my_file_map, 'relation') + self.check_tag( + errors, yaml_file, count, my_file_map, 'trim', + required=False, string=False) self.extra_tags(errors, yaml_file, count, my_file_map, ex.ExtraFileMapTagError) + def validate_values(self, errors, yaml_file, count, file_map): + '''Confirm that a file_map contains the right tag values, if not + add to errors + (Only used for values that are not a string.) + ''' + if 'trim' in file_map: + value = file_map['trim'] + if not isinstance(value, bool): + errors.append(ex.BadTrimValueError( + archive_path(yaml_file), count, value)) + + def validate_file_map(self, errors, yaml_file, count, file_map): + '''Confirm that a file_map contains the right tags, if not + add to errors + ''' + self.validate_key_existance(errors, yaml_file, count, file_map) + self.validate_values(errors, yaml_file, count, file_map) + def validate_map_item(self, errors, yaml_file, count, map_item): '''Confirm that a map_item is a dict with the right tags, if not add to errors @@ -308,6 +331,14 @@ class UploadDir(): if errors: raise core_ex.MultiError(errors) + def _trim(self, fmap): + '''Should the file be trimmed? + ''' + if 'trim' in fmap: + return fmap['trim'] + else: + return self.uf['trim_upload'] + def get_filedata(self, dir_name, file_map): '''Return a list of UploadData instances or raise an error @@ -331,7 +362,7 @@ class UploadDir(): uf['null_rep'], name, fmap['relation'], - trim=uf['trim_upload'])) + trim=self._trim(fmap))) except core_ex.PGWUIError as exp: relation = fmap['relation'] errors.append(exp.color(map_description(name, relation), -- 2.34.1