- pickle.dump(self.vars, file('pickled/vars.pkl', 'wb'), 2)
- pickle.dump(self.hosts, file('pickled/hosts.pkl', 'wb'), 2)
- tpl = Template(filename='templates/dhcpd.conf')
- ctx = Context(file('generated/dhcpd.conf', 'w'),
- hosts=self.hosts.values(), **self.vars)
- tpl.render_context(ctx)
+ self._dump()
+ self._write_config()
+ self.reload()
+
+ @handler
+ def rollback(self):
+ r"rollback() -> None :: Discard the changes not yet commited."
+ self._load()
+
+ def _dump(self):
+ r"_dump() -> None :: Dump all persistent data to pickle files."
+ # XXX podría ir en una clase base
+ self._dump_var(self.vars, pickle_vars)
+ self._dump_var(self.hosts, pickle_hosts)
+
+ def _load(self):
+ r"_load() -> None :: Load all persistent data from pickle files."
+ # XXX podría ir en una clase base
+ self.vars = self._load_var(pickle_vars)
+ self.hosts = self._load_var(pickle_hosts)
+
+ def _pickle_filename(self, name):
+ r"_pickle_filename() -> string :: Construct a pickle filename."
+ # XXX podría ir en una clase base
+ return path.join(self.pickle_dir, name) + pickle_ext
+
+ def _dump_var(self, var, name):
+ r"_dump_var() -> None :: Dump a especific variable to a pickle file."
+ # XXX podría ir en una clase base
+ pickle.dump(var, file(self._pickle_filename(name), 'wb'), 2)
+
+ def _load_var(self, name):
+ r"_load_var() -> object :: Load a especific pickle file."
+ # XXX podría ir en una clase base
+ return pickle.load(file(self._pickle_filename(name)))
+
+ def _write_config(self):
+ r"_write_config() -> None :: Generate all the configuration files."
+ # XXX podría ir en una clase base, ver como generalizar variables a
+ # reemplazar en la template
+ out_file = file(path.join(self.config_dir, config_filename), 'w')
+ ctx = Context(out_file, hosts=self.hosts.values(), **self.vars)
+ self.template.render_context(ctx)