1 # -*- coding: UTF-8 -*-
2 # vim: expandtab sw=4 ts=4 sts=4:
5 Wrapper for Gammu configuration
7 __author__
= 'Michal Čihař'
8 __email__
= 'michal@cihar.com'
10 Copyright © 2003 - 2010 Michal Čihař
12 This program is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License version 2 as published by
14 the Free Software Foundation.
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 Class wrapping gammu configuration file for reading and writing.
33 def __init__(self
, wammu_cfg
, path
= None):
35 Reads gammu configuration and prepares it for use.
37 self
.wammu_cfg
= wammu_cfg
41 self
.filename
= self
.wammu_cfg
.Read('/Gammu/Gammurc')
42 self
.config
= wx
.FileConfig(
43 localFilename
= self
.filename
,
44 style
= wx
.CONFIG_USE_LOCAL_FILE
48 cont
, val
, idx
= self
.config
.GetFirstGroup()
49 matcher
= re
.compile('gammu(\d*)')
51 match
= matcher
.match(val
)
53 index
= match
.groups(1)[0]
58 name
= self
.config
.Read('%s/name' % val
, val
)
59 self
.list.append({'Id': index
, 'Name': name
, 'Path': val
})
60 cont
, val
, idx
= self
.config
.GetNextGroup(idx
)
64 Returns copy of list of configuration settings.
66 # we need deep copy here
68 for config
in self
.list:
72 def GetConfig(self
, position
):
74 Returns complete configuration.
79 path
= 'gammu%s' % position
80 device
= self
.config
.Read('/%s/port' % path
)
81 connection
= self
.config
.Read('/%s/connection' % path
)
82 model
= self
.config
.Read('/%s/model' % path
)
83 name
= self
.config
.Read('/%s/name' % path
)
87 'Connection': connection
,
91 def SetConfig(self
, position
, device
, connection
,
92 name
= None, model
= None):
94 Set configuration at defined position.
100 path
= 'gammu%s' % position
101 for config
in self
.list:
102 if config
['Id'] == position
:
103 path
= config
['Path']
104 config
['Name'] = name
107 self
.config
.Write('/%s/port' % path
, device
)
108 self
.config
.Write('/%s/connection' % path
, connection
)
110 self
.config
.Write('/%s/name' % path
, name
)
111 if model
is not None:
112 self
.config
.Write('/%s/model' % path
, model
)
114 self
.list.append({'Id': position
, 'Name': name
, 'Path': path
})
119 Find first free entry in configuration file.
123 for config
in self
.list:
124 idxmap
[config
['Id']] = 1
125 for i
in range(1000):
126 if not idxmap
.has_key(i
):
129 if first_free
is None:
130 raise Exception('Could not find free configuration entry!')
133 def GetConfigList(self
, new
= False):
135 Returns list of available configurations as tuple of (details, verbose).
140 'Id': self
.FirstFree(),
142 'Name': _('Create new configuration')
148 # l10n: %(name)s is name of current configuration or 'Create new
149 # configuration', %(position) d is position of this config in .gammurc
150 choices
.append(_('%(name)s (position %(position)d)') %
151 {'name': config
['Name'], 'position': config
['Id']})
154 def SelectConfig(self
, parent
= None, force
= False, new
= False):
156 Shows dialog (if needed) to select configuration.
158 lst
, choices
= self
.GetConfigList(new
= new
)
160 if len(choices
) == 1 and not force
:
163 dlg
= wx
.SingleChoiceDialog(parent
,
164 _('Select which configration you want to modify.'),
165 _('Select configuration section'),
167 if dlg
.ShowModal() == wx
.ID_OK
:
168 return lst
[dlg
.GetSelection()]['Id']