]> git.ipfire.org Git - people/arne_f/ipfire-3.x.git/blob - pyfire/src/config.py
Move all packages to root.
[people/arne_f/ipfire-3.x.git] / pyfire / src / config.py
1 #
2 # simpleconifg.py - representation of a simple configuration file (sh-like)
3 #
4 # Matt Wilson <msw@redhat.com>
5 # Jeremy Katz <katzj@redhat.com>
6 #
7 # Copyright 1999-2002 Red Hat, Inc.
8 #
9 # This software may be freely redistributed under the terms of the GNU
10 # library public license.
11 #
12 # You should have received a copy of the GNU Library Public License
13 # along with this program; if not, write to the Free Software
14 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 #
16
17 import string
18 import os
19
20 # use our own ASCII only uppercase function to avoid locale issues
21 # not going to be fast but not important
22 def uppercase_ASCII_string(str):
23 newstr = ""
24 for i in range(0,len(str)):
25 if str[i] in string.lowercase:
26 newstr += chr(ord(str[i])-32)
27 else:
28 newstr += str[i]
29
30 return newstr
31
32 class ConfigFile:
33 def __str__ (self):
34 s = ""
35 keys = self.info.keys ()
36 keys.sort ()
37 for key in keys:
38 # FIXME - use proper escaping
39 if type(self.info[key]) == type(""):
40 s = s + key + "=\"" + self.info[key] + "\"\n"
41 return s
42
43 def __init__ (self, filename):
44 self.info = {}
45 self.filename = filename
46 self.read()
47
48 def write(self, filename=None):
49 if not filename:
50 filename = self.filename
51 f = open(filename, "w")
52 f.write(self.__str__())
53 f.close()
54
55 def read(self, filename=None):
56 if not filename:
57 filename = self.filename
58 if not os.access(filename, os.R_OK):
59 return
60
61 f = open(filename, "r")
62 lines = f.readlines()
63 f.close()
64
65 for line in lines:
66 fields = line[:-1].split('=', 2)
67 if len(fields) < 2:
68 # how am I supposed to know what to do here?
69 continue
70 key = uppercase_ASCII_string(fields[0])
71 value = fields[1]
72 # XXX hack
73 value = value.replace('"', '')
74 value = value.replace("'", '')
75 self.info[key] = value
76
77 def set(self, *args):
78 for (key, data) in args:
79 self.info[uppercase_ASCII_string(key)] = data
80
81 def unset(self, *keys):
82 for key in keys:
83 key = uppercase_ASCII_string(key)
84 if self.info.has_key (key):
85 del self.info[key]
86
87 def get(self, key):
88 key = uppercase_ASCII_string(key)
89 if self.info.has_key (key):
90 return self.info[key]
91 else:
92 return ""