Esempio n. 1
0
def initialize(args=None, **kwargs):
    """
    Initialize the style system. You may use the following arguments to
    configure the style system:

    ==============  ==============  ============
    Argument        Default         Description
    ==============  ==============  ============
    style                           The name of the style to load. If this isn't specified, the set style will be loaded from the current profile.
    default_style   ``"default"``   The name of the default style, to be used if a style isn't specified here, in the profile, or in the command line.
    ==============  ==============  ============

    In addition, you can provide a list of command line arguments to have
    siding load them automatically. Example::

        siding.style.initialize(sys.argv[1:])

    The following command line arguments are supported:

    ================  ============
    Argument          Description
    ================  ============
    ``--safe-mode``   When safe mode is enabled, add-ons, including styles, won't be loaded automatically.
    ``--style``       The name of the style to load.
    ================  ============
    """

    # Get the default style and start our list.
    default_style = kwargs.get('default_style', 'default')
    styles = [default_style]

    # Add the profile's current style to the list.
    style = profile.get('siding/style/current-style')
    if style:
        styles.insert(0, style)

    # Add the passed style to the list.
    style = kwargs.get('style')
    if style:
        styles.insert(0, style)

    # Parse any options we've got.
    if args:
        if args is True:
            args = sys.argv[1:]

        parser = argparse.ArgumentParser(add_help=False)
        parser.add_argument('--safe-mode', action='store_true')
        parser.add_argument('--style')
        options = parser.parse_known_args(args)[0]

        # Store those results.
        if options.safe_mode:
            addons.safe_mode = True

        if options.style:
            # Add the CLI style to the list.
            styles.insert(0, options.style)

    # Save the current widget style.
    widget_style = QApplication.instance().style().metaObject().className()
    widget_style = STYLE_KEYS.get(widget_style)
    if widget_style:
        profile.set('siding/style/widget-style', widget_style)

    # Discover our styles.
    addons.discover('style')

    # If safe-mode is enabled, just quit now.
    if addons.safe_mode:
        log.info('Not loading a style due to safe-mode.')
        return

    # Get the style.
    for style in styles:
        try:
            activate_style(addons.get('style', style))
            break
        except (IOError, ValueError), err:
            log.error('Error loading style %r: %s' % (style, err))
        except KeyError:
            log.error('No such style: %s' % style)
Esempio n. 2
0
def initialize(args=None, **kwargs):
    """
    Initialize the plugin system. You may use the following arguments to
    configure the plugin system:

    ==============  ==============  ============
    Argument        Default         Description
    ==============  ==============  ============
    discover        ``True``        If this is True, we'll search for plugins immediately.
    load            ``True``        If this is True, plugins will be loaded automatically after discovery.
    activate        ``True``        If this is True, plugins will be activated automatically after loading.
    paths           ``[]``          A list of paths to search for plugins.
    ==============  ==============  ============

    In addition, you can provide a list of command line arguments to have
    siding load them automatically. Example::

        siding.plugins.initialize(sys.argv[1:])

    The following command line arguments are supported:

    ==================  ============
    Argument            Description
    ==================  ============
    ``--safe-mode``     When safe mode is enabled, add-ons, including styles, won't be loaded automatically.
    ``--plugin-path``   Add the given path to the plugin search paths. This may be used more than once.
    ==================  ============
    """

    # First, get the paths in case we need it.
    paths = addons.manager._types['plugin'][2]

    if kwargs.has_key('paths'):
        # Clear out paths. But don't delete it, because we'd just lose our
        # reference.
        del paths[:]
        paths.extend(kwargs.get('paths'))

    # Now, parse our options.
    if args:
        if args is True:
            args = sys.argv[1:]

        parser = argparse.ArgumentParser(add_help=False)
        parser.add_argument('--safe-mode', action='store_true')
        parser.add_argument('--plugin-path', action='append')

        options = parser.parse_known_args(args)[0]

        # Do stuff.
        if options.safe_mode:
            addons.safe_mode = True

        if options.plugin_path:
            for path in reversed(options.plugin_path):
                paths.insert(0, path)

    # Now, for the discovery.
    if kwargs.get('discover', True):
        addons.discover('plugin')

    # And loading...
    if addons.safe_mode:
        log.info('Not loading plugins due to safe-mode.')
        return

    if kwargs.get('load', True):
        for info in addons.find('plugin', lambda info: not info.is_loaded):
            try:
                info.load()
            except (addons.DependencyError, ImportError), err:
                log.error('Error loading plugin %r: %s' %
                          (info.data['name'], err))