Add a "trim" key to the bulk upload map
authorKarl O. Pinc <kop@karlpinc.com>
Sat, 23 Jan 2021 23:31:38 +0000 (17:31 -0600)
committerKarl O. Pinc <kop@karlpinc.com>
Sat, 23 Jan 2021 23:31:38 +0000 (17:31 -0600)
src/pgwui_bulk_upload/exceptions.py
src/pgwui_bulk_upload/views/bulk_upload.py

index 3156a0ad8a5adb4e34bd618208a95c19a83e4f0b..7a06eaea6bd49d8da35c92b256b3220c7d03ca7f 100644 (file)
@@ -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__(
index 834285bfcfaf9ed695d1fda582f2119a772164e2..c456908df2ee09cc1a691d811d4eb9ad468b4beb 100644 (file)
@@ -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),