File: configfile.py

package info (click to toggle)
backintime 0.9.26-4
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,992 kB
  • ctags: 447
  • sloc: python: 4,542; xml: 526; sh: 185; makefile: 20
file content (229 lines) | stat: -rw-r--r-- 6,328 bytes parent folder | download
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
37
38
39
40
41
42
43
44
45
46
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#    Back In Time
#    Copyright (C) 2008-2009 Oprea Dan
#
#    This program 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 2 of the License, or
#    (at your option) any later version.
#
#    This program 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 this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


import os.path
import os


class ConfigFile:
	def __init__( self ):
		self.dict = {}

	def save( self, filename ):
		try:
			file = open( filename, 'w' )
			keys = self.dict.keys()
			keys.sort()
			for key in keys:
				file.write( "%s=%s\n" % ( key, self.dict[key] ) )
			file.close()
		except:
			pass

	def load( self, filename ):
		self.dict = {}
		self.append( filename )

	def append( self, filename ):
		lines = []

		try:
			file = open( filename, 'r' )
			lines = file.readlines()
			file.close()
		except:
			pass

		for line in lines:
			items = line.split( '=' )
			if len( items ) == 2:
				self.dict[ items[ 0 ] ] = items[ 1 ][ : -1 ]

	def remap_key( self, old_key, new_key ):
		if old_key != new_key:
			if self.dict.has_key( old_key ):
				if not self.dict.has_key( new_key ):
					self.dict[ new_key ] = self.dict[ old_key ]
				del self.dict[ old_key ]

	def has_value( self, key ):
		return self.dict.has_key( key )

	def get_str_value( self, key, default_value = '' ):
		if self.dict.has_key( key ):
			return self.dict[ key ]
		else:
			return default_value

	def set_str_value( self, key, value ):
		self.dict[ key ] = value

	def get_int_value( self, key, default_value = 0 ):
		try:
			return int( self.dict[ key ] )
		except:
			return default_value

	def set_int_value( self, key, value ):
		self.set_str_value( key, str( value ) )

	def get_bool_value( self, key, default_value = False ):
		try:
			val = self.dict[ key ]
			if "1" == val or "TRUE" == val.upper():
				return True
			return False
		except:
			return default_value

	def set_bool_value( self, key, value ):
		if value:
			self.set_str_value( key, 'true' )
		else:
			self.set_str_value( key, 'false' )

	def remove_key( self, key ):
		if self.dict.has_key( key ):
			del self.dict[ key ]

	def remove_keys_starts_with( self, prefix ):
		remove_keys = []

		for key in self.dict.iterkeys():
			if key.startswith( prefix ):
				remove_keys.append( key )

		for key in remove_keys:
			del self.dict[ key ]


class ConfigFileWithProfiles( ConfigFile ):
	def __init__( self, default_profile_name ):
		ConfigFile.__init__( self )

		self.default_profile_name = default_profile_name
		self.current_profile_id = '0'

	def _get_profile_key_( self, key, profile_id = None ):
		if profile_id is None:
			profile_id = self.current_profile_id
		return 'profile.' + profile_id + '.' + key

	def remove_profile_key( self, key, profile_id = None ):
		self.remove_key( self._get_profile_key_( key, profile_id ) )

	def remove_profile_keys_starts_with( self, prefix, profile_id = None ):
		self.remove_keys_starts_with( self._get_profile_key_( prefix, profile_id ) )

	def has_profile_value( self, key, profile_id = None ):
		return self.dict.has_key( self._get_profile_key_( key, profile_id ) )

	def get_profile_str_value( self, key, default_value = '', profile_id = None ):
		return self.get_str_value( self._get_profile_key_( key, profile_id ), default_value )

	def set_profile_str_value( self, key, value, profile_id = None ):
		self.set_str_value( self._get_profile_key_( key, profile_id ), value )

	def get_profile_int_value( self, key, default_value = 0, profile_id = None ):
		return self.get_int_value( self._get_profile_key_( key, profile_id ), default_value )

	def set_profile_int_value( self, key, value, profile_id = None ):
		self.set_int_value( self._get_profile_key_( key, profile_id ), value )

	def get_profile_bool_value( self, key, default_value = False, profile_id = None ):
		return self.get_bool_value( self._get_profile_key_( key, profile_id ), default_value )

	def set_profile_bool_value( self, key, value, profile_id = None ):
		self.set_bool_value( self._get_profile_key_( key, profile_id ), value )

	def get_current_profile( self ):
		return self.current_profile_id

	def get_profiles( self ):
		return self.get_str_value( 'profiles', '0' ).split(':')

	def get_profile_name( self, profile_id = None ):
		return self.get_profile_str_value( 'profile_name', self.default_profile_name, profile_id )

	def add_profile( self, name ):
		profiles = self.get_profiles()

		for profile in profiles:
			if self.get_profile_name( profile ) == name:
				return _('Profile %s already exists !') % name

		new_id = 1
		while True:
			ok = True

			for profile in profiles:
				if profile == str(new_id):
					ok = False
					break

			if ok:
				break

			new_id = new_id + 1

		new_id = str( new_id )

		self.profiles.append( new_id )
		self.set_str_value( 'profiles', profiles.join(':') )

		self.current_profile_id = new_id
		self.set_profile_str_value( 'profile_name', name, new_id )
		return None

	def remove_profile( self, profile_id = None ):
		if profile_id == None:
			profile_id = self.current_profile_id

		if profile_id == '0':
			return

		profiles = self.get_profiles()
		index = 0
		for profile in profiles:
			if profile == profile_id:
				self.remove_keys_starts_with( self._get_profile_key_( '', profile_id ) )
				del profiles[index]
				self.set_str_value( 'profiles', profiles.join(':') )
				break
			index = index + 1

		if self.current_profile == profile_id:
			self.current_profile = '0'

	def rename_profile( self, name, profile_id = None ):
		if profile_id == None:
			profile_id = self.current_profile_id

		if profile_id == 0:
			return _('You can\'t rename "Main" profile')

		profiles = self.get_profiles()

		for profile in profiles:
			if self.get_profile_name( profile ) == name:
				if profile[0] == profile_id:
					return None #
				return _('Profile %s already exists !') % name

		return None