bin/lesson_initialize.py: Remove 'example-site' reference
[rnaseq-cwl-training.git] / bin / util.py
index 2341bdb006b9bfae5d85243cc5d13dfe60e3b652..0cc8de69d130481bdc354a2a0ff2ccf761cb52e7 100644 (file)
@@ -1,3 +1,4 @@
+from __future__ import print_function
 import sys
 import os
 import json
@@ -11,11 +12,21 @@ except ImportError:
     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):
@@ -52,26 +63,39 @@ class Reporter(object):
     def add(self, location, fmt, *args):
         """Append error unilaterally."""
 
-        if isinstance(location, type(None)):
-            coords = ''
-        elif isinstance(location, str):
-            coords = '{0}: '.format(location)
-        elif isinstance(location, tuple):
-            filename, line_number = location
-            coords = '{0}:{1}: '.format(*location)
-        else:
-            assert False, 'Unknown location "{0}"/{1}'.format(location, type(location))
-
-        self.messages.append(coords + fmt.format(*args))
+        self.messages.append((location, fmt.format(*args)))
 
 
     def report(self, stream=sys.stdout):
-        """Report all messages."""
+        """Report all messages in order."""
 
         if not self.messages:
             return
-        for m in sorted(self.messages):
-            print(m, file=stream)
+
+        def pretty(item):
+            location, message = item
+            if isinstance(location, type(None)):
+                return message
+            elif isinstance(location, str):
+                return location + ': ' + message
+            elif isinstance(location, tuple):
+                return '{0}:{1}: '.format(*location) + message
+            else:
+                assert False, 'Unknown item "{0}"'.format(item)
+
+        def key(item):
+            location, message = item
+            if isinstance(location, type(None)):
+                return ('', -1, message)
+            elif isinstance(location, str):
+                return (location, -1, message)
+            elif isinstance(location, tuple):
+                return (location[0], location[1], message)
+            else:
+                assert False, 'Unknown item "{0}"'.format(item)
+
+        for m in sorted(self.messages, key=key):
+            print(pretty(m), file=stream)
 
 
 def read_markdown(parser, path):
@@ -151,3 +175,11 @@ def check_unwanted_files(dir_path, reporter):
         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)