def get_saver(name="GenericSaver"): for saver in iter_plugins('vpltk.saver'): if saver.default_name == name: return saver # if required plugin has not been found raise TypeError('saver plugin not found: ' + str(name))
def instance(cls, **kwargs): """ This is a temporary service that will be replaced by more specialized service or services based on interface """ if 'class_args' in kwargs: class_args = kwargs['class_args'] else: class_args = {} instance = None err = 'Cannot find required applet' if 'identifier' in kwargs: identifier = kwargs['identifier'] err = 'No applet named %s' % identifier if identifier in cls._applets: instance = cls._applets[identifier] return instance else: for plugin in iter_plugins('oalab.applet'): if plugin.name == identifier: instance = plugin() applet = instance(**class_args) cls.register_applet(plugin.name, applet) instance = cls._applets[identifier] return instance raise NotImplementedError, err
def load_interfaces(): """ Need to load interface classes to auto register them (see :class:`openalea.core.interface.IInterfaceMetaClass`) """ for plugin in iter_plugins('oalab.interface'): plugin()()
def __init__(self, filename="", categories=None, dtypes=None, parent=None): super(SelectCategory, self).__init__(parent=parent) if categories is None: categories = Project.DEFAULT_CATEGORIES.keys() if dtypes is None: dtypes = [plugin.default_name for plugin in iter_plugins('oalab.paradigm_applet')] dtypes.append('Other') self.categories = categories layout = QtGui.QFormLayout(self) self.label = QtGui.QLabel("Select in which category you want to add this file: ") self.l_dtypes = QtGui.QLabel("Data type") self.label2 = QtGui.QLabel("New filename: ") self.combo = QtGui.QComboBox(self) self.combo.addItems(categories) if 'model' in categories: self.combo.setCurrentIndex(categories.index('model')) self.combo_dtypes = QtGui.QComboBox(self) self.combo_dtypes.addItems(dtypes) self.combo_dtypes.setCurrentIndex(0) self.line = QtGui.QLineEdit(filename) layout.addRow(self.label, self.combo) layout.addRow(self.l_dtypes, self.combo_dtypes) layout.addRow(self.label2, self.line) self.setLayout(layout)
def find_plugins(plugin_name='oalab.service.to_shape3d', debug=False): """ Find plugins defined as entry points. A Plugin return a registry of adapters. A registry is a mapping between a type or a tuple of types and a functor returning a 3D model. """ register = {} from openalea.core.plugin import iter_plugins for plugin in iter_plugins(plugin_name, debug=debug): register.update(plugin.registry) return register
def DataType(path=None, name=None, mimetype=None): if path: name = Path(path).ext[1:].lower() return name elif name: return Path(name).ext[1:].lower() elif mimetype: # for ModelClass in iter_plugins('oalab.model'): # if ModelClass.mimetype == mimetype: # return ModelClass.default_name for DataClass in iter_plugins('oalab.dataclass'): if DataClass.mimetype == mimetype: return DataClass.default_name else: return None
def __call__(self, mainwin=None): if mainwin is None: return self.__class__ from openalea.core.plugin import iter_plugins session = mainwin.session # 1. Load applets plugins = {} for plugin in iter_plugins('oalab.applet', debug=session.debug_plugins): if plugin.name in self.applets: plugins[plugin.name] = plugin() # 2. Place applet following given order, # this is important to order tabs correctly for name in self.applets: if name in plugins: mainwin.add_plugin(plugins[name]) # 3. Once all applets loaded, init them (link between applets, loading default values, ...) mainwin.initialize()
def search_path(): """ Return a list of all path containing projects """ repositories = set() # 1. Add default user project dir repositories.add(Path(settings.get_project_dir())) # 2. Add project repositories defined by packages for plugin in iter_plugins('oalab.project_repository'): for repository in plugin(): repositories.add(repository) # 3. Read repositories defined by users and saved in config config = settings.Settings() lst = list(repositories) try: s = config.get("ProjectManager", "Path") lst = eval(s, {"path": Path}) except NoSectionError: config.add_section("ProjectManager") config.add_option("ProjectManager", "Path", str([str(path) for path in lst])) except NoOptionError: config.add_option("ProjectManager", "Path", str([str(path) for path in lst])) for repo in lst: repositories.add(repo) # Remove all paths to directories that don't exist final_list = set() for p in repositories: p = Path(p).abspath() if not p.isdir(): continue final_list.add(p) return list(final_list)
def __init__(self, filename="", categories=None, dtypes=None, parent=None): super(SelectCategory, self).__init__(parent=parent) if categories is None: categories = Project.DEFAULT_CATEGORIES.keys() if dtypes is None: dtypes = [ plugin.default_name for plugin in iter_plugins('oalab.paradigm_applet') ] dtypes.append('Other') self.categories = categories layout = QtGui.QFormLayout(self) self.label = QtGui.QLabel( "Select in which category you want to add this file: ") self.l_dtypes = QtGui.QLabel("Data type") self.label2 = QtGui.QLabel("New filename: ") self.combo = QtGui.QComboBox(self) self.combo.addItems(categories) if 'model' in categories: self.combo.setCurrentIndex(categories.index('model')) self.combo_dtypes = QtGui.QComboBox(self) self.combo_dtypes.addItems(dtypes) self.combo_dtypes.setCurrentIndex(0) self.line = QtGui.QLineEdit(filename) layout.addRow(self.label, self.combo) layout.addRow(self.l_dtypes, self.combo_dtypes) layout.addRow(self.label2, self.line) self.setLayout(layout)
from openalea.core.path import path as Path from openalea.core.plugin import iter_plugins from openalea.core.model import Model import mimetypes __all__ = ["ModelFactory", "ModelClass", "ModelType"] REGISTERY_MIME_CLASS = {} for ModelClass in iter_plugins('oalab.modelclass'): REGISTERY_MIME_CLASS[ModelClass.mimetype] = ModelClass REGISTERY_DTYPE_MIME = {} for ModelClass in iter_plugins('oalab.modelclass'): REGISTERY_DTYPE_MIME[ModelClass.dtype.lower()] = ModelClass.mimetype def ModelClass(dtype=None, mimetype=None): """ Return class wich match dtype. For example, for 'python' dtype it return PythonModel class. Matching can be extended with plugins. if both dtype and mimetype is None, returns all available ModelClasses """ if dtype is None and mimetype is None: return set(REGISTERY_MIME_CLASS.values() + [Model]) if mimetype in REGISTERY_MIME_CLASS: return REGISTERY_MIME_CLASS[mimetype]
from openalea.core.path import path as Path from openalea.core.plugin import iter_plugins from openalea.core.data import Data import mimetypes __all__ = ["DataFactory", "DataClass", "MimeType", "DataType"] REGISTERY_MIME_CLASS = {} for DataClass in iter_plugins('oalab.dataclass'): REGISTERY_MIME_CLASS[DataClass.mimetype] = DataClass # for ModelClass in iter_plugins('oalab.model'): # REGISTERY_MIME_CLASS[ModelClass.mimetype] = ModelClass REGISTERY_NAME_MIME = {} for DataClass in iter_plugins('oalab.dataclass'): REGISTERY_NAME_MIME[DataClass.default_name.lower()] = DataClass.mimetype REGISTERY_NAME_MIME[DataClass.extension.lower()] = DataClass.mimetype # for ModelClass in iter_plugins('oalab.model'): # REGISTERY_NAME_MIME[ModelClass.default_name.lower()] = ModelClass.mimetype # REGISTERY_NAME_MIME[ModelClass.extension.lower()] = ModelClass.mimetype def MimeType(path=None, name=None): """ Return mimetype for path. First, try to find extension in registery filled by models. If datatype is not found, use builtin module "mimetypes". If it cannot guess, returns False.
def get_loader(name="GenericLoader"): for loader in iter_plugins('vpltk.loader'): if loader.default_name == name: return loader
def discover_qt_controls(): # session = Session() # debug=session.debug_plugins # TODO: use plugin instance manager return [plugin for plugin in iter_plugins('oalab.qt_control')]
MIME_MODEL_CONTROLLER = {} MIME_DATA_CONTROLLER = {} def _fill_registery(registery, mimetypes): if mimetypes in (None, unicode): return elif isinstance(mimetypes, basestring): registery.setdefault(mimetypes, []).append(plugin) elif isinstance(mimetypes, (list, tuple, set)): for mimetype in mimetypes: registery.setdefault(mimetype, []).append(plugin) for plugin in iter_plugins('oalab.paradigm_applet'): _fill_registery(MIME_MODEL_CONTROLLER, plugin.mimetype_model) for plugin in iter_plugins('oalab.paradigm_applet'): _fill_registery(MIME_DATA_CONTROLLER, plugin.mimetype_data) def paradigm_controller_class(mimetype): if mimetype in MIME_DATA_CONTROLLER: return MIME_DATA_CONTROLLER[mimetype][0]()() elif mimetype in MIME_MODEL_CONTROLLER: return MIME_MODEL_CONTROLLER[mimetype][0]()() else: return None def paradigm_controller(obj):
from openalea.oalab.service.applet import get_applet from openalea.oalab.gui.mainwindow import MainWindow from openalea.oalab.session.session import Session from openalea.core.plugin import iter_plugins from openalea.core.service.ipython import interpreter if __name__ == '__main__': instance = QtGui.QApplication.instance() if instance is None: app = QtGui.QApplication([]) else: app = instance session = Session() mainwin = MainWindow(session) for plugin in iter_plugins('oalab.applet'): if plugin.name in ('ProjectManager', 'EditorManager'): mainwin.add_plugin(plugin()) pm = session.project_manager pm.cproject = pm.default() pmw = get_applet(identifier='ProjectManager', class_args=dict(mainwindow=mainwin)) pcw = get_applet(identifier='EditorManager', class_args=dict(mainwindow=mainwin)) interp = interpreter() interp.locals['pmw'] = pmw interp.locals['pcw'] = pcw mainwin.show()