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