示例#1
0
def ensure_paths():
    """ Ensure profile_path is set, and that it's registered with path. """
    global profile_path
    global root_path

    # If we don't have root path, don't worry about it.
    if not root_path:
        root_path = path.root()

    # If we don't have profile_path, make it.
    if not profile_path:
        if portable:
            # Well, bah. Okay... *now* care about the root path.
            if not root_path:
                root_path = path.root()
            profile_path = root_path
        else:
            profile_path = path.appdata()

        # Add the "Profiles/<profile>" bit to the profile path and ensure it
        # exists.
        profile_path = os.path.join(profile_path, 'Profiles', name)
        if not os.path.exists(profile_path):
            os.makedirs(profile_path)

        path.add_source(profile_path)
示例#2
0
def initialize(args=None, **kwargs):
    """
    Initialize the profile system. You may use the following arguments to
    configure the profile system:

    =============  ============  ============
    Argument       Default       Description
    =============  ============  ============
    portable       ``False``     If True, the profile system will create a profile path within the root folder, allowing the application to work as a portable app.
    profile        ``default``   The name of the profile to load.
    sources        ``[]``        A list of additional sources for the path system to use. These will be fed into :func:`siding.path.add_source`, along with any sources from the command line.
    profile_path                 If this is set, load the profile from this path rather than building a path.
    root_path                    The application root directory. This is always the last source to be used by the path system.
    =============  ============  ============

    .. warning::
        ``root_path`` will *probably* not work as expected after your
        application is frozen into an executable, so be sure to test that it's
        working properly before distributing your application if you're using
        ``root_path``.

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

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

    The following command line arguments are supported:

    ===================  ============
    Argument             Description
    ===================  ============
    ``--portable``       If True, the profile system will create a profile path within the root folder, allowing the application to work as a portable app.
    ``--profile``        The name of the profile to load.
    ``--profile-path``   The path to load the profile from.
    ``--root-path``      The application root directory.
    ``--source``         An additional source for the path system. This can be used multiple times.
    ===================  ============
    """
    global name
    global portable
    global profile_path
    global root_path
    global settings

    # Set the defaults now.
    portable = kwargs.get('portable', False)
    name = kwargs.get('profile', 'default')

    # And load the paths if we've got them.
    root_path = kwargs.get('root_path', root_path)
    profile_path = kwargs.get('profile_path', profile_path)

    # Get the source list.
    sources = kwargs.get('sources', [])

    # Now, parse the options we've got.
    if args:
        if args is True:
            args = sys.argv[1:]

        parser = argparse.ArgumentParser(add_help=False)
        parser.add_argument('--portable', action='store_true', default=None)
        parser.add_argument('--profile')
        parser.add_argument('--profile-path')
        parser.add_argument('--root-path')
        parser.add_argument('--source', action='append')

        options = parser.parse_known_args(args)[0]

        # Let's set stuff up then.
        if options.portable is not None:
            portable = options.portable

        if options.profile:
            name = options.profile

        if options.profile_path:
            profile_path = options.profile_path
            if not os.path.exists(profile_path):
                os.makedirs(profile_path)

        if options.root_path:
            root_path = options.root_path
            if not os.path.exists(root_path):
                parser.error("The specified root path doesn't exist.")

        if options.source:
            for source in options.source:
                if not source in sources:
                    if not os.path.exists(source):
                        parser.error("The source %r doesn't exist." % source)
                    sources.append(source)

    # Now, do the path stuff.
    for source in sources:
        path.add_source(source)

    # Do we already have our paths?
    if profile_path or root_path:
        path.add_source(profile_path)

    # Make sure.
    ensure_paths()

    # Now, open the settings file with QSettings and we're done.
    file = os.path.join(profile_path, 'settings.ini')
    settings = QSettings(file, QSettings.IniFormat)

    log.info(u'Using profile: %s (%s)' % (name, profile_path))
    log.debug(u'settings.ini contains %d keys across %d groups.' % (
        len(settings.allKeys()), len(settings.childGroups())))