Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ka_extensionpoint.py
blob: 1f6067fbb6df7fbd9cf3fc126dcf5cdb30d63091 (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
# coding: UTF8
# Copyright 2009 Thomas Jourdan
#
# 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 3 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 os
#import types
import ka_debug

_PREFIX = 'ep_'
_PYEXT = '.py'
_SEP = '_'

_extension_types = []
_extensions = []

def list_extensions(extension_type):
    """
    pre: extension_type in _extension_types
    """
    e_list = []
    for extension_class in _extensions:
        if extension_class.startswith(extension_type + _SEP):
            e_list.append(extension_class)
    e_list.sort()
    return e_list

def list_extension_types():
    return _extension_types

def create(extension_key, *params):
    """
    pre: extension_key in _extensions
    post: __return__ is not None
    """
    a_module = __import__('ep_' + extension_key)
    for key, value in a_module.__dict__.iteritems():
#        print '-- ', str(type(value)), key
        if str(type(value)) == "<type 'type'>":
            a_class = getattr(a_module, key)
            return a_class(*params)
#            if len(params) == 0:
#                return a_class()
#            elif len(params) == 1:
#                return a_class(params[0])

def _add(extension_type, extension_class):
    if extension_type not in _extension_types:
        _extension_types.append(extension_type)
    e_key = extension_type + _SEP + extension_class
    if e_key not in _extensions:
        _extensions.append(e_key)

def scann():
    bundle_path = ka_debug.DEBUG_PATH
    if 'SUGAR_BUNDLE_PATH' in os.environ:
        from sugar.activity import activity
        bundle_path = activity.get_bundle_path()
    ka_debug.info('searching for extensions in ' + bundle_path)
    for element in os.listdir(bundle_path):
        if element.startswith(_PREFIX) and element.endswith(_PYEXT) \
           and os.path.isfile(os.path.join(bundle_path, element)):
            name_parts = element.split(_SEP)
            if len(name_parts) == 3:
                _add(name_parts[1], name_parts[2].replace(_PYEXT, ''))
    _extension_types.sort()
    _extensions.sort()

scann()