1
2
3
4
5
6
7
8
9
10 """BitPim Config class based on ConfigParser
11 """
12
13
14 import ConfigParser
15 import os
16 import os.path
17
18
19 import guihelper
20
21
22
23
24 -class Config(ConfigParser.ConfigParser):
25 """Handle reading and writing various BitPim config values
26 """
27 _default_config_filename='.bitpim'
28
29 - def __init__(self, config_file_name=None):
30 """Constructor
31 @param config_file_name: (optional) config file name
32 """
33 ConfigParser.ConfigParser.__init__(self)
34
35 if config_file_name:
36 self._filename=os.path.abspath(config_file_name)
37 self._path=os.path.dirname(self._filename)
38 else:
39 self._path, self._filename=self._getdefaults()
40
41 if self._filename:
42 try:
43 self.read([self._filename])
44 except:
45
46 if __debug__:
47 raise
48 self.Write('path', self._path)
49 self.Write('config', self._filename)
50
52
53
54 if guihelper.IsMSWindows():
55
56 from win32com.shell import shell, shellcon
57 try:
58 path=shell.SHGetFolderPath(0, shellcon.CSIDL_PERSONAL, None, 0)
59 except:
60
61
62 path=r"c:\My BitPim Files"
63 path=os.path.join(path, "bitpim")
64 else:
65 path=os.path.expanduser("~/.bitpim-files")
66 return path,os.path.join(path, Config._default_config_filename)
67
69
70 _l=key.split('/')
71 return ('/'.join(_l[:-1]) if len(_l)>1 else 'DEFAULT', _l[-1])
72
74 if section and section!='DEFAULT' and not self.has_section(section):
75 self.add_section(section)
76
77 - def Read(self, key, default=''):
78 """Read the value of keyword key, if that keyword does not exist, return default
79 @param key: string key value.
80 @param default: default value if key does not exist.
81 @returns: the value of key
82 """
83 try:
84 return self.get(*self._expand(key))
85 except:
86 return default
87
89 """Read the value of keyword key, if that keyword does not exist, return default
90 @param key: string key value.
91 @param default: default value if key does not exist.
92 @returns: the value of key
93 """
94 _section,_option=self._expand(key)
95 try:
96
97 return self.getint(_section, _option)
98 except:
99 pass
100 try:
101
102 return self.getboolean(_section, _option)
103 except:
104
105 return default
106
108 """Read the value of keyword key, if that keyword does not exist, return default
109 @param key: string key value.
110 @param default: default value if key does not exist.
111 @returns: the value of key
112 """
113 try:
114 return self.getfloat(*self._expand(key))
115 except:
116 return default
117
118 - def Write(self, key, value):
119 """Write the value of keyword key.
120 @param key: string key value.
121 @param value: the value of the key.
122 """
123 try:
124 _section,_option=self._expand(key)
125 if not _section:
126 _section='DEFAULT'
127 self._check_section(_section)
128 self.set(_section, _option, str(value))
129 self.write(file(self._filename, 'wb'))
130 return True
131 except:
132 return False
133 WriteInt=Write
134 WriteFloat=Write
135
136 - def HasEntry(self, key):
137 """Check if the specified key exists.
138 @param key: key value
139 @returns: True if key exists, False otherwise.
140 """
141 return self.has_option(*self._expand(key))
144