diff -r 2223ade4f259 -r e66102ab7048 dhcp_conf.py --- a/dhcp_conf.py Thu Apr 02 20:19:18 2009 +0300 +++ b/dhcp_conf.py Thu Apr 02 20:54:37 2009 +0300 @@ -6,18 +6,43 @@ import itertools +class Comment (conf.ConfObject) : + """ + A comment, is, well, a comment :) + + Currently, comments are only one line, and look like the following: + "#" + """ + + def __init__ (self, comment) : + """ + @param comment the comment string + """ + + self.comment = comment + + def fmt_lines (self) : + """ + Yield a single line with the comment + """ + + yield "# %s" % (self.comment, ) + class Section (conf.ConfObject) : """ A section holds a list of params and a list of decls """ - def __init__ (self, params=None, decls=None) : + def __init__ (self, params=None, decls=None, comment=None) : """ If params/decls are given, those are the used as the initial contents of this section + + If a comment is given, then it will be formatted before the section's stuff """ self.params = params or [] self.decls = decls or [] + self.comment = comment def add_param (self, param) : """ @@ -41,11 +66,23 @@ for decl in decls : self.add_decl(decl) + def _fmt_comment (self) : + """ + Format our comment line + """ + + return "# %s" % (self.comment, ) + + def fmt_lines (self) : """ Format all of our params and decls, in that order """ + # comment? + if self.comment : + yield self._fmt_comment() + # then output each content line for stmt in itertools.chain(self.params, self.decls) : # skip Nones @@ -74,14 +111,11 @@ def __init__ (self, name, *args) : """ - The statement will be formatted like this: - [ [ ... ] ] ";" - Arguments given as None will be ignored. """ self.name = name - self.args = args + self.args = [arg for arg in args if arg is not None] def _fmt_arg (self, arg) : """ @@ -121,7 +155,7 @@ Formats the statement name/params as a single line, ignoring None """ - return "%s%s" % (self.name, (' ' + ' '.join(self._fmt_arg(a) for a in self.args if a is not None)) if self.args else '') + return "%s%s" % (self.name, (' ' + ' '.join(self._fmt_arg(a) for a in self.args)) if self.args else '') class Literal (Statement) : """ @@ -143,6 +177,9 @@ Parameters have a name, and optionally, a number of arguments, and are formatted as statements terminated with a semicolon. For convenience, params/decls that are None are ignored. + + The parameter will be formatted like this: + [ [ ... ] ] ";" """ def fmt_lines (self) :