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/device' % path
)
82 device
= self
.config
.Read('/%s/port' % path
)
83 connection
= self
.config
.Read('/%s/connection' % path
)
84 model
= self
.config
.Read('/%s/model' % path
)
85 name
= self
.config
.Read('/%s/name' % path
)
89 'Connection': connection
,
93 def SetConfig(self
, position
, device
, connection
,
94 name
= None, model
= None):
96 Set configuration at defined position.
102 path
= 'gammu%s' % position
103 for config
in self
.list:
104 if config
['Id'] == position
:
105 path
= config
['Path']
106 config
['Name'] = name
109 self
.config
.Write('/%s/port' % path
, device
)
110 self
.config
.Write('/%s/connection' % path
, connection
)
112 self
.config
.Write('/%s/name' % path
, name
)
113 if model
is not None:
114 self
.config
.Write('/%s/model' % path
, model
)
116 self
.list.append({'Id': position
, 'Name': name
, 'Path': path
})
121 Find first free entry in configuration file.
125 for config
in self
.list:
126 idxmap
[config
['Id']] = 1
127 for i
in range(1000):
128 if not idxmap
.has_key(i
):
131 if first_free
is None:
132 raise Exception('Could not find free configuration entry!')
135 def GetConfigList(self
, new
= False):
137 Returns list of available configurations as tuple of (details, verbose).
142 'Id': self
.FirstFree(),
144 'Name': _('Create new configuration')
150 # l10n: %(name)s is name of current configuration or 'Create new
151 # configuration', %(position) d is position of this config in .gammurc
152 choices
.append(_('%(name)s (position %(position)d)') %
153 {'name': config
['Name'], 'position': config
['Id']})
156 def SelectConfig(self
, parent
= None, force
= False, new
= False):
158 Shows dialog (if needed) to select configuration.
160 lst
, choices
= self
.GetConfigList(new
= new
)
162 if len(choices
) == 1 and not force
:
165 dlg
= wx
.SingleChoiceDialog(parent
,
166 _('Select which configration you want to modify.'),
167 _('Select configuration section'),
169 if dlg
.ShowModal() == wx
.ID_OK
:
170 return lst
[dlg
.GetSelection()]['Id']