Commits

Henrique Romano  committed f92dcd6

Implement config.ensure_(one|all) to help callsites manage required options.

  • Participants
  • Parent commits 8aef409

Comments (0)

Files changed (2)

File djangotriager/config.py

 
 from ConfigParser import SafeConfigParser
 
+from djangotriager.exceptions import InvalidOptions
+
 class Configuration(object):
     # Specify which commands from namespace maps to what section/arg in configuration
     NAMESPACE_CONFIG_MAP = {
                 continue
             section, option = Configuration.NAMESPACE_CONFIG_MAP[key]
             self.parser.set(section, option, str(kwargs[key]))
+
+    def ensure_one(self, *options):
+        options_found = [option for option in options if self.get(option)]
+        if len(options_found) != 1:
+            raise InvalidOptions("You must specify one of the following "
+                                 "options: " + ", ".join(options))
+
+    def ensure_all(self, *options):
+        not_found_options = [
+            option for option in options if not self.get(option)]
+        if not_found_options:
+            raise InvalidOptions("The following options are required "
+                                 + ", ".join(not_found_options))

File djangotriager/exceptions.py

+
+class InvalidOptions(Exception):
+    """Options specified are not valid."""