pvl.dhcp.DHCPRule: change --dhcp-network/gateway to select matching by network/gateway
authorTero Marttila <terom@paivola.fi>
Sun, 10 Feb 2013 19:57:00 +0200
changeset 214 e314447c5621
parent 213 711f71e7328b
child 215 a0dedd0889f0
child 216 c0dbd44bd463
child 218 84d411df8a64
pvl.dhcp.DHCPRule: change --dhcp-network/gateway to select matching by network/gateway
pvl/dhcp/rule.py
--- a/pvl/dhcp/rule.py	Sun Feb 10 19:56:39 2013 +0200
+++ b/pvl/dhcp/rule.py	Sun Feb 10 19:57:00 2013 +0200
@@ -24,34 +24,23 @@
     parser.add_option('--dhcp-rules',               metavar='CONF',
             help="dhcp plugin instances by network/gateway")
 
-    parser.add_option('--dhcp-network',             metavar='NET', action='append',
+    parser.add_option('--dhcp-network',             action='store_true',
             help="dhcp plugin instance by network")
 
-    parser.add_option('--dhcp-gateway',             metavar='NET', action='append',
+    parser.add_option('--dhcp-gateway',             action='store_true',
             help="dhcp plugin instance by gateway")
    
     return parser
 
-def apply_rules (options) :
-    """
-        Yield DHCPRules from options.
-    """
-
-    for gateway in options.dhcp_gateway :
-        yield DHCPRule.rule(gateway, gateway=gateway)
-
-    for network in options.dhcp_network :
-        yield DHCPRule.rule(network, network=network)
-
-    if options.dhcp_rules :
-        yield DHCPRule.load(open(options.dhcp_rules))
-
 def apply (options) :
     """
         Return DHCPRule from options.
     """
 
-    return DHCPRule(None, tuple(apply_rules(options)))
+    if options.dhcp_rules :
+        return DHCPRule.load(options, open(options.dhcp_rules))
+    else :
+        return DHCPRule()
 
 class DHCPRule (object) :
     """
@@ -62,35 +51,35 @@
 
 
     @classmethod
-    def load (cls, file, name=None) :
+    def load (cls, options, file, name=None) :
         """
             Load from config file.
         """
         
         config = configobj.ConfigObj(file)
 
-        return cls.load_section(name, config)
+        return cls.load_section(options, name, config)
 
     @classmethod
-    def load_section (cls, name, section) :
+    def load_section (cls, options, name, section) :
         """
             Rule from sub-sections and section.
         """
 
         # recurse
-        rules = tuple(cls.load_section(subsection, section[subsection]) for subsection in section.sections)
+        rules = tuple(cls.load_section(options, subsection, section[subsection]) for subsection in section.sections)
 
         # rule
         attrs = dict((name, section[name]) for name in section.scalars)
 
         try :
-            return cls.config(name, rules, **attrs)
+            return cls.config(options, name, rules, **attrs)
 
         except ValueError as ex :
             raise ValueError("[%s] %s" % (name, ex))
 
     @classmethod
-    def config (cls, name, rules, gateway=None, network=None, interval=None) :
+    def config (cls, options, name, rules, gateway=None, network=None, interval=None) :
         """
             Rule from section.
         """
@@ -98,29 +87,22 @@
         if interval :
             log.warn("%s: interval: not implemented", name)
 
-        if network :
+        if network and options.dhcp_network :
             network = IPv4Network(network)
         else :
             network = None
 
+        if gateway and options.dhcp_gateway :
+            gateway = gateway
+        else :
+            gateway = None
+
         return cls(name, rules,
                 gateway     = gateway, 
                 network     = network,
         )
 
-    @classmethod
-    def rule (cls, name, gateway=None, network=None) :
-        if network :
-            network = IPv4Network(network)
-        else :
-            network = None
-
-        return cls(name, (),
-                gateway     = gateway, 
-                network     = network,
-        )
-
-    def __init__ (self, name, rules, gateway=None, network=None) :
+    def __init__ (self, name=None, rules=(), gateway=None, network=None) :
         """
             Match as name by gateway/network.
         """