Repository: RockefellerArchiveCenter/DACSspace

Issue:
Address filename conventions/extension
@daremiyo discovered that troublesome filenames (filenames with spaces and no extensions provided) will pass tests. We might want to consider building out a solution to this:

- Enhance the TestReporter class to include testing for specific filenames/characters/extensions
- Have the code fudge a troublesome filename into a better filename (remove spaces, add extension)
- Remove the option to include a filename. Instead, always use the same filename and always save the file in the same location with a standard filename or a filename based on the timestamp.

For additional information, see @daremiyo comment on PR #39 

> One thing to note for us to consider is how we should handle filename conventions for the file to which the results are being written. Do we want to specify a file extension like .csv; specify what characters shouldn’t be included like spaces, etc.

Discussion:

Patch:
diff --git a/dacsspace/dacsspace.py b/dacsspace/dacsspace.py
index b953a5b..35118e7 100644
--- a/dacsspace/dacsspace.py
+++ b/dacsspace/dacsspace.py
@@ -1,3 +1,5 @@
+import re
+
 from .client import ArchivesSpaceClient
 from .reporter import CSVReporter
 from .validator import Validator
@@ -6,6 +8,14 @@ from .validator import Validator
 class DACSspace:
     """Base DACSspace class. Fetches data from AS, validates and reports results."""
 
+    def __init__(self, csv_filepath):
+        """Checks csv filepath to make sure it has the proper extension and characters."""
+        if not csv_filepath.endswith(".csv"):
+            raise ValueError("File must have .csv extension")
+        if re.search(r'[*?:"<>|]', csv_filepath):
+            raise ValueError(
+                'File name cannot contain the following characters: * ? : " < > | ')
+
     def run(self, published_only, invalid_only,
             schema_identifier='single_level_required.json', schema_filepath=None):
         client = ArchivesSpaceClient()


Test patch:
diff --git a/tests/test_dacsspace.py b/tests/test_dacsspace.py
new file mode 100644
index 0000000..87de2f0
--- /dev/null
+++ b/tests/test_dacsspace.py
@@ -0,0 +1,22 @@
+from unittest import TestCase
+
+from dacsspace import DACSspace
+
+
+class TestDACSspace(TestCase):
+
+    def test_csv_filepath(self):
+        """Asserts that CSV filepath is handled as expected.
+
+        Filepaths are checked to ensure they end with the appropriate file
+        extension (.csv) and don't contain any illegal characters.
+        """
+        DACSspace("csv_filepath.csv")
+        with self.assertRaises(ValueError) as err:
+            DACSspace("my*file.csv")
+        self.assertEqual(str(err.exception),
+                         'File name cannot contain the following characters: * ? : " < > | ')
+        with self.assertRaises(ValueError) as err:
+            DACSspace("myfile")
+        self.assertEqual(str(err.exception),
+                         "File must have .csv extension")