Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tutorius/addon.py
blob: 6e3d8b99f693bce4130d028f002da1302215828c (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
# Copyright (C) 2009, Tutorius.org
# Copyright (C) 2009, Simon Poirier <simpoir@gmail.com>
#
#
# 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
"""
This module manages the loading and listing of tutorius addons.
Addons are modular actions and events that are package in such a way that they
can be autodetected and can integrate with Tutorius components (the editor)
without any configuration or explicit dependencies (python import).

An action addon is expected to have a metadata dict such as this one:
__action__ = {
    "name" : "HelloWorld",
    "display_name" : "Hello World!",
    "icon" : "hello",
    "class" : HelloAction,
    "mandatory_props" : ["text"],
    "test" : true,   (OPTIONAL)
}
"""

import os
import re
import logging

PREFIX = __name__+"s"
PATH = re.sub("addon\\.py[c]$", "", __file__)+"addons"

TYPE_ACTION = 'action'
TYPE_EVENT = 'event'

_cache = None

def _reload_addons():
    global _cache
    _cache = {}
    for addon in filter(lambda x: x.endswith("py"), os.listdir(PATH)):
        mod = __import__(PREFIX+'.'+re.sub("\\.py$", "", addon), {}, {}, [""])
        if hasattr(mod, "__action__"):
            _cache[mod.__action__['name']] = mod.__action__
            mod.__action__['type'] = TYPE_ACTION
            continue
        if hasattr(mod, "__event__"):
            _cache[mod.__event__['name']] = mod.__event__
            mod.__event__['type'] = TYPE_EVENT

def create(name, *args, **kwargs):
    global _cache
    if not _cache:
        _reload_addons()
    try:
        comp_metadata = _cache[name]
        try:
            return comp_metadata['class'](*args, **kwargs)
        except:
            logging.debug("Could not instantiate %s with parameters %s, %s"%(comp_metadata['name'],str(args), str(kwargs)))
            return None 
    except KeyError:
        logging.debug("Addon not found for class '%s'", name)
        return None

def list_addons():
    global _cache
    if not _cache:
        _reload_addons()
    return [k for k, v in _cache.items() if 'test' not in v]

def get_addon_meta(name):
    global _cache
    if not _cache:
        _reload_addons()
    return _cache[name]

def get_name_from_type(typ):
    global _cache
    if not _cache:
        _reload_addons()
    for addon in _cache.keys():
        if typ == _cache[addon]['class']:
            return addon
    return None

# vim:set ts=4 sts=4 sw=4 et: