Singleton fixed
[myslice.git] / myslice / configengine.py
1 import os.path
2 from ConfigParser import RawConfigParser
3 from myslice.settings import ROOT
4
5 #
6 # DO NOT EDIT !!!
7 #
8 # This file does not contain any user-modifiable data
9 #
10 # te defaults here are, well, only default values, 
11 # and, you have the option to override them
12 # by writing a file myslice/myslice.ini
13 # that looks like this
14 #[manifold]
15 #url = http://manifold.pl.sophia.inria.fr:7080/
16 #admin_user = admin
17 #admin_password = admin
18 #[googlemap]
19 #api_key=theapikeyasprovidedbygoogle
20
21 # use a singleton instead of staticmethods
22 #-------------------------------------------------------------------------
23 # Class Singleton
24 #
25 # Classes that inherit from Singleton can be instanciated only once 
26 #-------------------------------------------------------------------------
27
28 class Singleton(type):
29     def __init__(cls, name, bases, dic):
30         super(Singleton,cls).__init__(name,bases,dic)
31         cls.instance=None
32
33     def __call__(cls, *args, **kw):
34         if cls.instance is None:
35             cls.instance=super(Singleton,cls).__call__(*args,**kw)
36         return cls.instance
37
38
39 # See also
40 # http://stackoverflow.com/questions/6760685/creating-a-singleton-in-python
41
42 class ConfigEngine(object):
43     __metaclass__ = Singleton
44
45     # the OpenLab-wide backend as managed by UPMC
46     # xxx production should probably use https of course
47     default_manifold_url = "https://test.myslice.info:7080/"
48     # the devel/unstable version runs on "https://dev.myslice.info:7080/"
49     # if you use a development backend running on this box, use "http://localhost:7080/"
50     # the INRIA setup is with "https://manifold.pl.sophia.inria.fr:7080/"
51
52     default_manifold_admin_user     = 'admin'
53     default_manifold_admin_password = 'demo'
54     default_myslice_theme           = 'onelab'
55
56     #iotlab dev url
57     default_iotlab_url = "https://devwww.iot-lab.info/rest/admin/users"
58     default_iotlab_admin_user = "xxx"
59     default_iotlab_admin_password= "yyy"
60
61     def __init__ (self):
62         parser = RawConfigParser ()
63         parser.add_section('manifold')
64         parser.set ('manifold', 'url', ConfigEngine.default_manifold_url)
65         parser.set ('manifold', 'admin_user', ConfigEngine.default_manifold_admin_user)
66         parser.set ('manifold', 'admin_password', ConfigEngine.default_manifold_admin_password)
67
68         parser.add_section('myslice')
69         parser.set ('myslice', 'theme', ConfigEngine.default_myslice_theme)
70
71         parser.add_section('iotlab')
72         parser.set ('iotlab', 'url', ConfigEngine.default_iotlab_url)
73         parser.set ('iotlab', 'admin_user', ConfigEngine.default_iotlab_admin_user)
74         parser.set ('iotlab', 'admin_password', ConfigEngine.default_iotlab_admin_password)
75
76         parser.add_section('googlemap')
77         parser.set ('googlemap','api_key', None)
78         parser.read (os.path.join(ROOT,'myslice/myslice.ini'))
79         self.config_parser=parser
80
81     def __getattr__(self, section):
82         if self.config_parser.has_section(section):
83             return ConfigSection(self.config_parser, section)
84         
85     def manifold_url (self):
86         return self.config_parser.get('manifold','url')
87
88     def manifold_admin_user_password(self):
89         return (self.config_parser.get('manifold','admin_user'),
90                 self.config_parser.get('manifold','admin_password'))
91
92     def iotlab_url (self):
93         return self.config_parser.get('iotlab','url')
94
95     def iotlab_admin_user(self):
96         return self.config_parser.get('iotlab','admin_user')
97
98     def iotlab_admin_password(self):
99         return self.config_parser.get('iotlab','admin_password')
100
101     def googlemap_api_key (self):
102         return self.config_parser.get('googlemap','api_key')
103
104     # exporting these details to js
105     def manifold_js_export (self):
106         return "var MANIFOLD_URL = '%s';\n"%self.manifold_url();
107
108 class ConfigSection(object) :
109     
110     def __init__(self, parser, section):
111         self._parser = parser
112         self._section = section
113     
114     def __getattr__(self, key):
115         if self._parser.has_option(self._section, key):
116             return self._parser.get(self._section, key)