pvl.hosts: implement support for domain-level include=...
authorTero Marttila <terom@paivola.fi>
Sun, 22 Dec 2013 18:09:08 +0200
changeset 333 7d2601368142
parent 332 bb8a18cffe8a
child 334 19da67d71506
pvl.hosts: implement support for domain-level include=...
pvl/hosts.py
--- a/pvl/hosts.py	Sun Dec 22 18:08:48 2013 +0200
+++ b/pvl/hosts.py	Sun Dec 22 18:09:08 2013 +0200
@@ -199,7 +199,33 @@
     def __str__ (self) :
         return str(self.host)
 
-def apply_hosts_config (options, config, name, defaults={}, parent=None) :
+def apply_host_includes (options, config_path, include) :
+    """
+        Yield files from a given config's include=... value
+    """
+
+    include_path = os.path.dirname(config_path)
+
+    for include in include.split() :
+        path = os.path.join(include_path, include)
+
+        if include.endswith('/') :
+            for name in os.listdir(path) :
+                file_path = os.path.join(path, name)
+
+                if name.startswith('.') :
+                    pass
+                elif not os.path.exists(file_path) :
+                    log.warn("%s: skip nonexistant include: %s", config_path, file_path)
+                else :
+                    log.info("%s: include: %s", config_path, file_path)
+                    yield open(file_path)
+
+        else :
+            log.info("%s: include: %s", config_path, path)
+            yield open(path)
+
+def apply_hosts_config (options, path, name, config, defaults={}, parent=None) :
     """
         Load hosts from a ConfigObj section.
     """
@@ -207,17 +233,27 @@
     scalars = dict((scalar, config[scalar]) for scalar in config.scalars)
 
     if config.sections :
+        # includes?
+        for file in apply_host_includes(options, path, scalars.pop('include', '')) :
+            # within our domain context
+            for host in apply_hosts_file(options, file, parent=name) :
+                yield host
+
         # recurse; this is a domain meta-section
         params = dict(defaults)
         params.update(**scalars) # override
+        
+        log.debug("%s: %s -> %s", path, parent, name)
 
         for section in config.sections :
-            for host in apply_hosts_config(options, config[section], section, params, name) :
+            for host in apply_hosts_config(options, path, section, config[section], params, name) :
                 yield host
 
     elif name :
         params = dict(defaults, **scalars)
 
+        log.debug("%s: %s: %s", path, parent, name)
+
         # this is a host section
         for host in Host.config(options, name, parent, **params) :
             yield host
@@ -226,7 +262,7 @@
         raise ValueError("No sections in config")
 
 
-def apply_hosts_file (options, file) :
+def apply_hosts_file (options, file, parent=None) :
     """
         Load Hosts from a file.
 
@@ -237,9 +273,10 @@
     )
     
     # use file basename as default
-    name = os.path.basename(file.name)
+    path = file.name
+    name = os.path.basename(path)
     
-    return apply_hosts_config(options, config, name)
+    return apply_hosts_config(options, path, name, config, parent=parent)
 
 def apply_hosts (options, files) :
     """