diff options
author | Nostalghia <b.vehikel@googlemail.com> | 2009-08-22 16:14:38 (GMT) |
---|---|---|
committer | Nostalghia <b.vehikel@googlemail.com> | 2009-08-22 16:14:38 (GMT) |
commit | bd288e7666cca83b06921ee32ddb30b64dadcb89 (patch) | |
tree | 56503c46a93d799695150cb512513dc427f752c0 /ka_extensionpoint.py |
initial commitv1
Diffstat (limited to 'ka_extensionpoint.py')
-rw-r--r-- | ka_extensionpoint.py | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/ka_extensionpoint.py b/ka_extensionpoint.py new file mode 100644 index 0000000..1f6067f --- /dev/null +++ b/ka_extensionpoint.py @@ -0,0 +1,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()
\ No newline at end of file |