csfile.py 3.5 KB
Newer Older
Daniel Armbruster's avatar
Daniel Armbruster committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
## @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 <http://www.gnu.org/licenses/>.
# ----
# 
# Copyright (c) 2011 by Daniel Armbruster
# 
# REVISIONS and CHANGES 
# 15/09/2011  V0.1    Daniel Armbruster
# 
# =============================================================================

Daniel Armbruster's avatar
Daniel Armbruster committed
37
38
import os
import re
39
import sys
Daniel Armbruster's avatar
Daniel Armbruster committed
40
41
import hashlib

42
43
44
45
46
__version__ = "V0.1"
__subversion__ = "Id: "
__license__ = "GPLv2"
__author__ = "Daniel Armbruster"
__copyright__ = "Copyright (c) 2011 by Daniel Armbruster"
Daniel Armbruster's avatar
Daniel Armbruster committed
47

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# -----------------------------------------------------------------------------
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()
Daniel Armbruster's avatar
Daniel Armbruster committed
76

77
78
79
80
81
82
83
  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()
Daniel Armbruster's avatar
Daniel Armbruster committed
84

85
86
  def update(self, regex):
    pass
Daniel Armbruster's avatar
Daniel Armbruster committed
87

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  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.")
Daniel Armbruster's avatar
Daniel Armbruster committed
105
    
106
107
108
109
110
111
112
113
  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:
Daniel Armbruster's avatar
Daniel Armbruster committed
114
  def __init__(self, file, hashfunc = hashlib.sha256):
115
116
117
118
119
    pass

# -----------------------------------------------------------------------------
if __name__ == '__main__':
  pass
Daniel Armbruster's avatar
Daniel Armbruster committed
120
121
  
  
Daniel Armbruster's avatar
Daniel Armbruster committed
122
# ----- END OF csfile.py -----