Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/jarabe/controlpanel/cmd.py
blob: c4870c9996b443204a0c9a2e03d952712341b396 (plain)
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
# Copyright (C) 2007, 2008 One Laptop Per Child
#
# 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 St, Fifth Floor, Boston, MA  02110-1301  USA

import sys
import getopt
import os
from gettext import gettext as _
import logging

from jarabe import config


_RESTART = 1

_same_option_warning = _('sugar-control-panel: WARNING, found more than one'
                         ' option with the same name: %s module: %r')
_no_option_error = _('sugar-control-panel: key=%s not an available option')
_general_error = _('sugar-control-panel: %s')


def cmd_help():
    """Print the help to the screen"""
    # TRANS: Translators, there's a empty line at the end of this string,
    # which must appear in the translated string (msgstr) as well.
    print _('Usage: sugar-control-panel [ option ] key [ args ... ] \n\
    Control for the sugar environment. \n\
    Options: \n\
    -h           show this help message and exit \n\
    -l           list all the available options \n\
    -h key       show information about this key \n\
    -g key       get the current value of the key \n\
    -s key       set the current value for the key \n\
    -c key       clear the current value for the key \n\
    ')


def note_restart():
    """Instructions how to restart sugar"""
    print _('To apply your changes you have to restart Sugar.\n' +
            'Hit ctrl+alt+erase on the keyboard to trigger a restart.')


def load_modules():
    """Build a list of pointers to available modules and import them.
    """
    modules = []

    path = os.path.join(config.ext_path, 'cpsection')
    folder = os.listdir(path)

    for item in folder:
        if os.path.isdir(os.path.join(path, item)) and \
                os.path.exists(os.path.join(path, item, 'model.py')):
            try:
                module = __import__('.'.join(('cpsection', item, 'model')),
                                    globals(), locals(), ['model'])
            except Exception:
                logging.exception('Exception while loading extension:')
            else:
                modules.append(module)

    return modules


def main():
    try:
        options, args = getopt.getopt(sys.argv[1:], 'h:s:g:c:l', [])
    except getopt.GetoptError:
        cmd_help()
        sys.exit(2)

    if not options:
        cmd_help()
        sys.exit(2)

    modules = load_modules()

    for option, key in options:
        found = 0
        if option in ('-h'):
            for module in modules:
                method = getattr(module, 'set_' + key, None)
                if method:
                    found += 1
                    if found == 1:
                        print method.__doc__
                    else:
                        print _(_same_option_warning % (key, module))
            if found == 0:
                print _(_no_option_error % key)
        if option in ('-l'):
            for module in modules:
                methods = dir(module)
                print '%s:' % module.__name__.split('.')[1]
                for method in methods:
                    if method.startswith('get_'):
                        print '    %s' % method[4:]
                    elif method.startswith('clear_'):
                        print '    %s (use the -c argument with this option)' \
                                % method[6:]
        if option in ('-g'):
            for module in modules:
                method = getattr(module, 'print_' + key, None)
                if method:
                    found += 1
                    if found == 1:
                        try:
                            method()
                        except Exception, detail:
                            print _(_general_error % detail)
                    else:
                        print _(_same_option_warning % (key, module))
            if found == 0:
                print _(_no_option_error % key)
        if option in ('-s'):
            for module in modules:
                method = getattr(module, 'set_' + key, None)
                if method:
                    note = 0
                    found += 1
                    if found == 1:
                        try:
                            note = method(*args)
                        except Exception, detail:
                            print _(_general_error % detail)
                        if note == _RESTART:
                            note_restart()
                    else:
                        print _(_same_option_warning % (key, module))
            if found == 0:
                print _(_no_option_error % key)
        if option in ('-c'):
            for module in modules:
                method = getattr(module, 'clear_' + key, None)
                if method:
                    note = 0
                    found += 1
                    if found == 1:
                        try:
                            note = method(*args)
                        except Exception, detail:
                            print _(_general_error % detail)
                        if note == _RESTART:
                            note_restart()
                    else:
                        print _(_same_option_warning % (key, module))
            if found == 0:
                print _(_no_option_error % key)