get/set accessors should now work, e.g.:
[plcapi.git] / PLC / Config.py
1 #!/usr/bin/python
2 #
3 # PLCAPI configuration store. Supports XML-based configuration file
4 # format exported by MyPLC.
5 #
6 # Mark Huang <mlhuang@cs.princeton.edu>
7 # Copyright (C) 2004-2006 The Trustees of Princeton University
8 #
9 # $Id$
10 #
11
12 import os
13 import sys
14
15 from PLC.Faults import *
16 from PLC.Debug import profile
17
18 # If we have been checked out into a directory at the same
19 # level as myplc, where plc_config.py lives. If we are in a
20 # MyPLC environment, plc_config.py has already been installed
21 # in site-packages.
22 myplc = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + \
23         os.sep + "myplc"
24
25 class Config:
26     """
27     Parse the bash/Python/PHP version of the configuration file. Very
28     fast but no type conversions.
29     """
30
31     def __init__(self, file = "/etc/planetlab/plc_config"):
32         # Load plc_config
33         try:
34             execfile(file, self.__dict__)
35         except:
36             # Try myplc directory
37             try:
38                 execfile(myplc + os.sep + "plc_config", self.__dict__)
39             except:
40                 raise PLCAPIError("Could not find plc_config in " + \
41                                   file + ", " + \
42                                   myplc + os.sep + "plc_config")
43
44 class XMLConfig:
45     """
46     Parse the XML configuration file directly. Takes longer but is
47     presumably more accurate.
48     """
49
50     def __init__(self, file = "/etc/planetlab/plc_config.xml"):
51         try:
52             from plc_config import PLCConfiguration
53         except:
54             sys.path.append(myplc)
55             from plc_config import PLCConfiguration
56
57         # Load plc_config.xml
58         try:
59             cfg = PLCConfiguration(file)
60         except:
61             # Try myplc directory
62             try:
63                 cfg = PLCConfiguration(myplc + os.sep + "plc_config.xml")
64             except:
65                 raise PLCAPIError("Could not find plc_config.xml in " + \
66                                   file + ", " + \
67                                   myplc + os.sep + "plc_config.xml")
68
69         for (category, variablelist) in cfg.variables().values():
70             for variable in variablelist.values():
71                 # Try to cast each variable to an appropriate Python
72                 # type.
73                 if variable['type'] == "int":
74                     value = int(variable['value'])
75                 elif variable['type'] == "double":
76                     value = float(variable['value'])
77                 elif variable['type'] == "boolean":
78                     if variable['value'] == "true":
79                         value = True
80                     else:
81                         value = False
82                 else:
83                     value = variable['value']
84
85                 # Variables are split into categories such as
86                 # "plc_api", "plc_db", etc. Within each category are
87                 # variables such as "host", "port", etc. For backward
88                 # compatibility, refer to variables by their shell
89                 # names.
90                 shell_name = category['id'].upper() + "_" + variable['id'].upper()
91                 setattr(self, shell_name, value)
92
93 if __name__ == '__main__':
94     import pprint
95     pprint = pprint.PrettyPrinter()
96     pprint.pprint(Config().__dict__.items())