~mars/+junk/leankitlpsync-fix-config-load

« back to all changes in this revision

Viewing changes to config.py

  • Committer: Maris Fogels
  • Date: 2012-05-08 18:55:34 UTC
  • Revision ID: maris.fogels@canonical.com-20120508185534-n54f0h7bdudfsvsv
Fixed the config loader code so that the app runs in the same directory as the config files.  Refactored the config loader code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from os.path import abspath, dirname, exists, join
 
2
 
1
3
from configglue.pyschema import (BoolConfigOption, ConfigSection,
2
4
                                 DictConfigOption, IntConfigOption, Schema,
3
5
                                 SchemaConfigParser, StringConfigOption,
5
7
from launchpadlib.launchpad import STAGING_SERVICE_ROOT
6
8
 
7
9
 
 
10
class SchemaExtensionError(Exception): pass
 
11
 
 
12
 
8
13
class LeanKitSchema(Schema):
9
14
    debug = BoolConfigOption(default=False)
10
15
    extend = StringConfigOption()
64
69
            op.error(reasons[0])
65
70
 
66
71
        return config
 
72
 
 
73
 
 
74
def load_app_config():
 
75
    """Load the application configuration files.
 
76
 
 
77
    Also handles the 'extend' schema option. Returns the loaded config.
 
78
    """
 
79
    configdir = abspath(dirname(__file__))
 
80
    config_file = join(configdir, 'config.ini')
 
81
    config = LeanKitSchema.get_config([config_file])
 
82
    load_config_extensions(config, configdir)
 
83
    return config
 
84
 
 
85
def load_config_extensions(config, configdir):
 
86
    extend = config.values('__main__')['extend']
 
87
    if extend is '':
 
88
        return
 
89
 
 
90
    extension = join(configdir, extend)
 
91
    if not exists(extension):
 
92
        raise SchemaExtensionError(
 
93
            "Unable to find config file extension '{0}'".format(extension))
 
94
    config.read(extension)