## @file csfile.py # @brief Provide a module to read, write and treat with a csback chechsumfile. # # ----------------------------------------------------------------------------- # # $Id$ # @author Daniel Armbruster # \date 15/09/2011 # # Purpose: Provide a module to read, write and treat with a csback checksumfile. # file. # # ---- # This file is part of csback. # # csback is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # csback is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with csback. If not, see . # ---- # # Copyright (c) 2011 by Daniel Armbruster # # REVISIONS and CHANGES # 15/09/2011 V0.1 Daniel Armbruster # # ============================================================================= import os import re import sys import hashlib __version__ = "V0.1" __subversion__ = "Id: " __license__ = "GPLv2" __author__ = "Daniel Armbruster" __copyright__ = "Copyright (c) 2011 by Daniel Armbruster" # ----------------------------------------------------------------------------- class CsFileError(Exception): def __init__(self, msg): self.msg = msg def display(self): sys.stderr.write("csFile (ERROR): " + self.msg + "\n") # ----------------------------------------------------------------------------- class CsFile: def __init__(self, filename, hashfunc=hashlib.sha256): self.__filename = filename self.__cslines = [] self.__hashfunc = hashfunc def read(self): if not os.path.isfile(self.__filename): raise CsFileError("CSFILE is not a regular file.") if 0 == os.stat(self.__filename).st_size: raise CsFileError("CSFILE is an empty file.") csfile = open(self.__filename) try: self.__cslines = [CsLine(line.split()) for line in csfile \ if len(line.rstrip()) and line[0] != '#'] finally: csfile.close() def write(self): if 0 == len(self.__cslines): raise CsFileError("CSFILE does not contain any lines.") file = open(self.__filename, 'w') for csline in self.__cslines: file.write(str(csline) + '\n') file.close() def update(self, regex): pass def checkLines(self): if 0 == len(self.__cslines): raise CsFileError("CSFILE does not contain any lines.") for line in self.__cslines: line.check() def displayLines(self): if 0 == len(self.__cslines): raise CsFileError("CSFILE does not contain any lines.") for line in self.__cslines: sys.stdout.write(line) def addLine(self, csline): if isinstance(csline, CsLine): self.__cslines.append(csline) else: raise CsFileError("Argument must be of type CsLine.") def removeLine(self, index): if 0 <= index and len(self.__cslines) >= index: del self.__cslines[index] else: raise CsFileError("Index out of range.") # ----------------------------------------------------------------------------- class CsLine: def __init__(self, file, hashfunc = hashlib.sha256): pass # ----------------------------------------------------------------------------- if __name__ == '__main__': pass # ----- END OF csfile.py -----