Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/controlpanel/cmd.py
blob: 7cb40da8097cb9b1733b953ddb2ede56db437e26 (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
# 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 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'''
    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\
    ')

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 in the model directory
    and load them.
    '''
    subpath = ['controlpanel', 'model']
    file_names = os.listdir(os.path.join(config.shell_path, '/'.join(subpath)))
    
    modules = []
    for file_name in file_names:
        if file_name.endswith('.py') and file_name != '__init__.py':
            module_name = os.path.splitext(file_name)[0]
            module = __import__('.'.join(subpath) + '.' + 
                                module_name, globals(), locals(), 
                                [module_name])
            modules.append(module)
    return modules        

def main():
    try:
        options, args = getopt.getopt(sys.argv[1:], "h:s:g: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:]
        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)