|
1 # |
|
2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 # All rights reserved. |
|
4 # This component and the accompanying materials are made available |
|
5 # under the terms of "Eclipse Public License v1.0" |
|
6 # which accompanies this distribution, and is available |
|
7 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 # |
|
9 # Initial Contributors: |
|
10 # Nokia Corporation - initial contribution. |
|
11 # |
|
12 # Contributors: |
|
13 # |
|
14 # Description: |
|
15 # |
|
16 import os |
|
17 import logging |
|
18 import ConfigParser |
|
19 |
|
20 DEFAULT_SECTION = 'DEFAULT' |
|
21 class ConeSettings(object): |
|
22 def __init__(self, parser, section=DEFAULT_SECTION): |
|
23 self.parser = parser |
|
24 self.section = section |
|
25 |
|
26 def get(self, property, default=None, vars=None): |
|
27 """ |
|
28 Try to get a single property from the ConeSettings section. The method support the |
|
29 ConfigParser interpolation of variables and deliver extra variables via the vars param. |
|
30 |
|
31 1. Try to get the property from configured section. |
|
32 2. If section is not found try to get it from DEFAULT section. |
|
33 3. Still if property is not found return the default values |
|
34 @param property: the name of the setting property get |
|
35 @param default: the default value to return if the property is not found from settings |
|
36 @param vars: a dictionary of variables that are given to the ConfigParser get |
|
37 for interpolation. e.g. {'output':'testing'}. |
|
38 |
|
39 """ |
|
40 try: |
|
41 return self.parser.get(self.section, property, False, vars) |
|
42 # if the section is not found try to use default |
|
43 except ConfigParser.NoSectionError: |
|
44 try: |
|
45 return self.parser.get(DEFAULT_SECTION, property, False, vars) |
|
46 except ConfigParser.NoOptionError: |
|
47 if default != None: |
|
48 return self.parser._interpolate(DEFAULT_SECTION,property,default,vars) |
|
49 else: |
|
50 return default |
|
51 # if the option is not found return the default value |
|
52 except ConfigParser.NoOptionError: |
|
53 if default != None: |
|
54 return self.parser._interpolate(DEFAULT_SECTION,property,default,vars) |
|
55 else: |
|
56 return default |
|
57 #return default |
|
58 |
|
59 |
|
60 class SettingsFactory(object): |
|
61 configsettings = None |
|
62 configpath = '' |
|
63 defaultconfig = 'cone_defaults.cfg' |
|
64 |
|
65 @classmethod |
|
66 def get_defaultconfig_path(cls): |
|
67 return os.path.join(cls.configpath,cls.defaultconfig) |
|
68 |
|
69 @classmethod |
|
70 def cone_parser(cls): |
|
71 """ |
|
72 Get a singleton instance of ConfigParser. |
|
73 """ |
|
74 if not cls.configsettings: |
|
75 cls.configsettings = ConfigParser.ConfigParser() |
|
76 |
|
77 try: |
|
78 cls.configsettings.readfp(open(cls.get_defaultconfig_path())) |
|
79 except IOError: |
|
80 logging.getLogger('cone').warning("Could not read default configuration file %s" % cls.get_defaultconfig_path()) |
|
81 return cls.configsettings |
|
82 |
|
83 @classmethod |
|
84 def clear(cls): |
|
85 """ |
|
86 Clear everything back to default so that the next time the settings are re-parsed. |
|
87 |
|
88 This method is provided for unit testing purposes. |
|
89 """ |
|
90 cls.configsettings = None |
|
91 cls.configpath = '' |
|
92 cls.defaultconfig = 'cone_defaults.cfg' |