3 # PLCAPI configuration store. Supports XML-based configuration file
4 # format exported by MyPLC.
6 # Mark Huang <mlhuang@cs.princeton.edu>
7 # Copyright (C) 2004-2006 The Trustees of Princeton University
16 from PLC.Faults import *
17 from PLC.Debug import profile
19 # If we have been checked out into a directory at the same
20 # level as myplc, where plc_config.py lives. If we are in a
21 # MyPLC environment, plc_config.py has already been installed
23 myplc = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + \
28 Parse the bash/Python/PHP version of the configuration file. Very
29 fast but no type conversions.
32 def __init__(self, file = "/etc/planetlab/plc_config"):
35 execfile(file, self.__dict__)
39 execfile(myplc + os.sep + "plc_config", self.__dict__)
41 raise PLCAPIError("Could not find plc_config in " + \
43 myplc + os.sep + "plc_config")
47 Parse the XML configuration file directly. Takes longer but is
48 presumably more accurate.
51 def __init__(self, file = "/etc/planetlab/plc_config.xml"):
53 from plc_config import PLCConfiguration
55 sys.path.append(myplc)
56 from plc_config import PLCConfiguration
60 cfg = PLCConfiguration(file)
64 cfg = PLCConfiguration(myplc + os.sep + "plc_config.xml")
66 raise PLCAPIError("Could not find plc_config.xml in " + \
68 myplc + os.sep + "plc_config.xml")
70 for (category, variablelist) in cfg.variables().values():
71 for variable in variablelist.values():
72 # Try to cast each variable to an appropriate Python
74 if variable['type'] == "int":
75 value = int(variable['value'])
76 elif variable['type'] == "double":
77 value = float(variable['value'])
78 elif variable['type'] == "boolean":
79 if variable['value'] == "true":
84 value = variable['value']
86 # Variables are split into categories such as
87 # "plc_api", "plc_db", etc. Within each category are
88 # variables such as "host", "port", etc. For backward
89 # compatibility, refer to variables by their shell
91 shell_name = category['id'].upper() + "_" + variable['id'].upper()
92 setattr(self, shell_name, value)
94 if __name__ == '__main__':
96 pprint = pprint.PrettyPrinter()
97 pprint.pprint(Config().__dict__.items())