conf.py
changeset 1 2223ade4f259
parent 0 257003279747
child 3 ff98fa9b84ce
equal deleted inserted replaced
0:257003279747 1:2223ade4f259
     1 """
     1 """
     2     Generic configuration file output
     2     Generic configuration file output
     3 """
     3 """
     4 
     4 
     5 import os, tempfile, shutil
     5 import os, os.path, tempfile, shutil
     6 
     6 
     7 class Object (object) :
     7 class ConfObject (object) :
     8     """
     8     """
     9         An object that can be written to a ConfFile, as multiple lines of text.
     9         An object that can be written to a ConfFile, as multiple lines of text.
    10     """
    10     """
    11 
    11 
    12     def fmt_lines (self) :
    12     def fmt_lines (self) :
    14             Yield a series of lines to be output in the file
    14             Yield a series of lines to be output in the file
    15         """
    15         """
    16         
    16         
    17         abstract
    17         abstract
    18 
    18 
    19 class File (object) :
    19 class File (ConfObject) :
    20     """
    20     """
    21         A single configuration file on the filesystem.
    21         A single configuration file on the filesystem.
    22 
    22 
    23         Configuration files are 
    23         Configuration files are themselves ConfObject's, although this must be implemented in the inheriting class.
    24     """
    24     """
    25 
    25 
    26     def __init__ (self, name, path, backup_suffix='.bak', mode=0644) :
    26     def __init__ (self, name, path, backup_suffix='.bak', mode=0644) :
    27         """
    27         """
    28             Initialize the config file, but don't open it yet
    28             Initialize the config file, but don't open it yet
    36         self.name = name
    36         self.name = name
    37         self.path = path
    37         self.path = path
    38         self.backup_suffix = backup_suffix
    38         self.backup_suffix = backup_suffix
    39         self.mode = mode
    39         self.mode = mode
    40     
    40     
    41     def write_file (self, file, objects) :
    41     def write_file (self, file) :
    42         """
    42         """
    43             Write out the given config objects into the given file
    43             Write out this config's stuff into the given file
    44         """
    44         """
    45 
    45 
    46         writer = Writer(file)
    46         writer = Writer(file)
    47         writer.write_objs(objects)
    47         writer.write_obj(self)
    48 
    48 
    49     def write (self, objects) :
    49     def write (self) :
    50         """
    50         """
    51             Write out a new config file with the given series of objects using the following procedure:
    51             Write out a new config file with this config's stuff using the following procedure:
    52                 * lock the real file
    52                 * lock the real file
    53                 * open a new temporary file, and write out the objects into that, closing it
    53                 * open a new temporary file, and write out the objects into that, closing it
    54                 * move the real file out of the way
    54                 * move the real file out of the way
    55                 * move the temporary file into the real file's place
    55                 * move the temporary file into the real file's place
    56                 * unlock the real file
    56                 * unlock the real file
    68         
    68         
    69         # fix the permissions
    69         # fix the permissions
    70         os.chmod(tmp_path, self.mode)
    70         os.chmod(tmp_path, self.mode)
    71 
    71 
    72         # write it
    72         # write it
    73         self.write_file(tmp_file, objects)
    73         self.write_file(tmp_file)
    74 
    74 
    75         # close it
    75         # close it
    76         tmp_file.close()
    76         tmp_file.close()
    77         del writer
       
    78         del tmp_file
       
    79         
    77         
    80         # move the old file out of the way
    78         # move the old file out of the way
    81         os.rename(self.path, self.path + self.backup_suffix)
    79         if os.path.exists(self.path) :
       
    80             os.rename(self.path, self.path + self.backup_suffix)
    82 
    81 
    83         # move the new file in
    82         # move the new file in
    84         shutil.move(tmp_path, self.path)
    83         shutil.move(tmp_path, self.path)
    85 
    84 
    86 class Writer (object) :
    85 class Writer (object) :
    93             @param file the temporary file object
    92             @param file the temporary file object
    94         """
    93         """
    95 
    94 
    96         self.file = file
    95         self.file = file
    97     
    96     
       
    97     def write_line (self, line) :
       
    98         """
       
    99             Write a single line to the file
       
   100         """
       
   101 
       
   102         self.file.write("%s\n" % (line, ))
       
   103     
       
   104     def write_lines (self, lines) :
       
   105         """
       
   106             Write a series of lines into the file
       
   107         """
       
   108 
       
   109         for line in lines :
       
   110             self.write_line(line)
       
   111 
    98     def write_obj (self, obj) :
   112     def write_obj (self, obj) :
    99         """
   113         """
   100             Write a single object to the file
   114             Write a single object to the file
   101         """
   115         """
   102         
   116         
   103         # just write out all the lines
   117         # just write out all the lines
   104         self.file.writelines(obj.fmt_lines())
   118         self.write_lines(obj.fmt_lines())
   105 
   119 
   106     def write_objs (self, objs) :
       
   107         """
       
   108             Write a series of objects to the file
       
   109         """
       
   110         
       
   111         # just write each in turn
       
   112         for obj in objs :
       
   113             self.write_obj(obj)
       
   114