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