Tighter checks for figures
[rnaseq-cwl-training.git] / bin / util.py
index 6af0a3317061d8663dafad9de41aec2156b7e597..92fd50d7522f4675d179e4df0b95e7d9488fd57a 100644 (file)
@@ -1,13 +1,33 @@
 import sys
+import os
 import json
 from subprocess import Popen, PIPE
 
+# Import this way to produce a more useful error message.
 try:
     import yaml
 except ImportError:
     print('Unable to import YAML module: please install PyYAML', file=sys.stderr)
     sys.exit(1)
 
+
+# Things an image file's name can end with.
+IMAGE_FILE_SUFFIX = {
+    '.gif',
+    '.jpg',
+    '.png',
+    '.svg'
+}
+
+# Files that shouldn't be present.
+UNWANTED_FILES = [
+    '.nojekyll'
+]
+
+# Marker to show that an expected value hasn't been provided.
+# (Can't use 'None' because that might be a legitimate value.)
+REPORTER_NOT_SET = []
+
 class Reporter(object):
     """Collect and report errors."""
 
@@ -18,11 +38,16 @@ class Reporter(object):
         self.messages = []
 
 
-    def check_field(self, filename, name, values, key, expected):
+    def check_field(self, filename, name, values, key, expected=REPORTER_NOT_SET):
         """Check that a dictionary has an expected value."""
 
         if key not in values:
             self.add(filename, '{0} does not contain {1}', name, key)
+        elif expected is REPORTER_NOT_SET:
+            pass
+        elif type(expected) in (tuple, set, list):
+            if values[key] not in expected:
+                self.add(filename, '{0} {1} value {2} is not in {3}', name, key, values[key], expected)
         elif values[key] != expected:
             self.add(filename, '{0} {1} is {2} not {3}', name, key, values[key], expected)
 
@@ -114,9 +139,33 @@ def split_metadata(path, text):
 
 def load_yaml(filename):
     """
-    Wrapper around YAML loading so that 'import yaml' and error
-    handling is only needed in one place.
+    Wrapper around YAML loading so that 'import yaml' is only needed
+    in one file.
+    """
+
+    try:
+        with open(filename, 'r') as reader:
+            return yaml.load(reader)
+    except (yaml.YAMLError, FileNotFoundError) as e:
+        print('Unable to load YAML file {0}:\n{1}'.format(filename, e), file=sys.stderr)
+        sys.exit(1)
+
+
+def check_unwanted_files(dir_path, reporter):
+    """
+    Check that unwanted files are not present.
     """
 
-    with open(filename, 'r') as reader:
-        return yaml.load(reader)
+    for filename in UNWANTED_FILES:
+        path = os.path.join(dir_path, filename)
+        reporter.check(not os.path.exists(path),
+                       path,
+                       "Unwanted file found")
+
+
+def require(condition, message):
+    """Fail if condition not met."""
+
+    if not condition:
+        print(message, file=sys.stderr)
+        sys.exit(1)