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


def cmd_help():
    '''Print the help for 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(path):    
    '''Build a list of pointers to available modules in the model directory
    and load them.
    '''
    subpath = ['controlpanel', 'model']
    names = os.listdir(os.path.join(path, '/'.join(subpath)))
    
    modules = []
    for name in names:
        if name.endswith('.py') and name != '__init__.py':
            tmp = name.strip('.py')
            mod = __import__('.'.join(subpath) + '.' + 
                             tmp, globals(), locals(), [tmp])
            modules.append(mod)
    return modules        

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

    if not opts:
        cmd_help()
        sys.exit()

    modules = load_modules(path)

    for opt, key in opts:
        if opt in ("-h"):
            for module in modules:
                method = getattr(module, 'set_' + key, None)
                if method:
                    print method.__doc__                              
                    sys.exit()                    
            print _("sugar-control-panel: key=%s not an available option"
                    % key)                                                    
        if opt 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 opt in ("-g"):
            for module in modules:
                method = getattr(module, 'print_' + key, None)
                if method:
                    try:
                        method()
                    except Exception, detail:
                        print _("sugar-control-panel: %s"
                                % detail)                    
                    sys.exit()
            print _("sugar-control-panel: key=%s not an available option"
                    % key)
        if opt in ("-s"):
            for module in modules:
                method = getattr(module, 'set_' + key, None)
                if method:
                    note = 0
                    try:
                        note = method(*args)
                    except Exception, detail:
                        print _("sugar-control-panel: %s"
                                % detail)
                    if note == 'RESTART':
                        note_restart()
                    sys.exit()
            print _("sugar-control-panel: key=%s not an available option"
                    % key)