2 # -*- coding: utf-8 -*-
7 Copyright (c) 2013 Frédéric Massart - FMCorz.net
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 http://github.com/FMCorz/mdk
27 from ..command
import Command
28 from ..tools
import launchEditor
, yesOrNo
29 from ..config
import ConfigObject
, ConfigFileCouldNotBeLoaded
32 class ConfigCommand(Command
):
38 'help': 'the action to perform',
43 'help': 'opens the config file in an editor'
49 'help': 'flat list of the settings'
56 'help': 'filter results using regular expressions'
63 'help': 'list the settings'
69 'help': 'display one setting'
76 'help': 'setting to display'
83 'help': 'set the value of a setting'
90 'help': 'setting to edit'
99 'help': 'value to set'
108 _description
= 'Manage your configuration'
110 def dictDisplay(self
, data
, ident
=0):
111 for name
in sorted(data
.keys()):
113 if type(setting
) != dict:
114 print u
'{0:<20}: {1}'.format(u
' ' * ident
+ name
, setting
)
116 print u
' ' * ident
+ '[%s]' % name
117 self
.dictDisplay(setting
, ident
+ 2)
119 def flatDisplay(self
, data
, parent
='', regex
=None):
120 for name
in sorted(data
.keys()):
122 if type(setting
) != dict:
123 if regex
and not regex
.search(parent
+ name
):
125 print u
'%s: %s' %
(parent
+ name
, setting
)
127 self
.flatDisplay(setting
, parent
=parent
+ name
+ u
'.', regex
=regex
)
130 if args
.action
== 'list':
131 self
.dictDisplay(self
.C
.get(), 0)
133 elif args
.action
== 'edit':
138 tmpfile
= launchEditor(filepath
=f
, suffix
='.json')
141 co
.loadFromFile(tmpfile
)
142 except ConfigFileCouldNotBeLoaded
:
144 logging
.error('I could not load the file, you probably screwed up the JSON...')
145 if yesOrNo('Would you like to continue editing? If not the changes will be discarded.'):
155 with
open(tmpfile
, 'r') as new
:
156 with
open(self
.C
.userFile
, 'w') as current
:
157 current
.write(new
.read())
159 elif args
.action
== 'flatlist':
162 regex
= re
.compile(args
.grep
, re
.I
)
163 self
.flatDisplay(self
.C
.get(), regex
=regex
)
165 elif args
.action
== 'show':
166 setting
= self
.C
.get(args
.setting
)
168 if type(setting
) == dict:
169 self
.flatDisplay(setting
, args
.setting
+ u
'.')
173 elif args
.action
== 'set':
174 setting
= args
.setting
176 if val
.startswith('b:'):
177 val
= True if val
[2:].lower() in ['1', 'true'] else False
178 elif val
.startswith('i:'):
182 # Not a valid int, let's consider it a string.
184 self
.C
.set(setting
, val
)