summaryrefslogtreecommitdiff
path: root/cloudinit/net/network_state.py
diff options
context:
space:
mode:
authorJoshua Harlow <harlowja@gmail.com>2016-05-05 16:38:53 -0700
committerJoshua Harlow <harlowja@gmail.com>2016-05-05 16:38:53 -0700
commitdb9d958a0e76c2c59298041a1355aba97fda000f (patch)
tree5e4e1a20c544fd6bca7388f31c0541da8ceeee40 /cloudinit/net/network_state.py
parentf5a1662e4a2cbbdee0766276fd516a25306545d6 (diff)
downloadvyos-cloud-init-db9d958a0e76c2c59298041a1355aba97fda000f.tar.gz
vyos-cloud-init-db9d958a0e76c2c59298041a1355aba97fda000f.zip
Add the bridge net type
Diffstat (limited to 'cloudinit/net/network_state.py')
-rw-r--r--cloudinit/net/network_state.py39
1 files changed, 24 insertions, 15 deletions
diff --git a/cloudinit/net/network_state.py b/cloudinit/net/network_state.py
index d08e94fe..27d35256 100644
--- a/cloudinit/net/network_state.py
+++ b/cloudinit/net/network_state.py
@@ -15,6 +15,8 @@
# You should have received a copy of the GNU Affero General Public License
# along with Curtin. If not, see <http://www.gnu.org/licenses/>.
+import six
+
from cloudinit import log as logging
from cloudinit import util
from cloudinit.util import yaml_dumps as dump_config
@@ -32,11 +34,31 @@ def from_state_file(state_file):
state = util.read_conf(state_file)
network_state = NetworkState()
network_state.load(state)
-
return network_state
-class NetworkState:
+class CommandHandlerMeta(type):
+ """Metaclass that dynamically creates a 'command_handlers' attribute.
+
+ This will scan the to-be-created class for methods that start with
+ 'handle_' and on finding those will populate a class attribute mapping
+ so that those methods can be quickly located and called.
+ """
+ def __new__(cls, name, parents, dct):
+ command_handlers = {}
+ for attr_name, attr in six.iteritems(dct):
+ if six.callable(attr) and attr_name.startswith('handle_'):
+ handles_what = attr_name[len('handle_'):]
+ if handles_what:
+ command_handlers[handles_what] = attr
+ dct['command_handlers'] = command_handlers
+ return super(CommandHandlerMeta, cls).__new__(cls, name,
+ parents, dct)
+
+
+@six.add_metaclass(CommandHandlerMeta)
+class NetworkState(object):
+
def __init__(self, version=NETWORK_STATE_VERSION, config=None):
self.version = version
self.config = config
@@ -48,18 +70,6 @@ class NetworkState:
'search': [],
}
}
- self.command_handlers = self.get_command_handlers()
-
- def get_command_handlers(self):
- METHOD_PREFIX = 'handle_'
- methods = filter(lambda x: callable(getattr(self, x)) and
- x.startswith(METHOD_PREFIX), dir(self))
- handlers = {}
- for m in methods:
- key = m.replace(METHOD_PREFIX, '')
- handlers[key] = getattr(self, m)
-
- return handlers
def dump(self):
state = {
@@ -83,7 +93,6 @@ class NetworkState:
# v1 - direct attr mapping, except version
for key in [k for k in required_keys if k not in ['version']]:
setattr(self, key, state[key])
- self.command_handlers = self.get_command_handlers()
def dump_network_state(self):
return dump_config(self.network_state)