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
13 from PLC.Faults import *
14 from PLC.Debug import profile
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
20 myplc = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + \
25 Parse the bash/Python/PHP version of the configuration file. Very
26 fast but no type conversions.
29 def __init__(self, file = "/etc/planetlab/plc_config"):
32 execfile(file, self.__dict__)
36 execfile(myplc + os.sep + "plc_config", self.__dict__)
38 raise PLCAPIError("Could not find plc_config in " + \
40 myplc + os.sep + "plc_config")
44 Parse the XML configuration file directly. Takes longer but is
45 presumably more accurate.
48 def __init__(self, file = "/etc/planetlab/plc_config.xml"):
50 from plc_config import PLCConfiguration
52 sys.path.append(myplc)
53 from plc_config import PLCConfiguration
57 cfg = PLCConfiguration(file)
61 cfg = PLCConfiguration(myplc + os.sep + "plc_config.xml")
63 raise PLCAPIError("Could not find plc_config.xml in " + \
65 myplc + os.sep + "plc_config.xml")
67 for (category, variablelist) in cfg.variables().values():
68 for variable in variablelist.values():
69 # Try to cast each variable to an appropriate Python
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":
81 value = variable['value']
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
88 shell_name = category['id'].upper() + "_" + variable['id'].upper()
89 setattr(self, shell_name, value)
91 if __name__ == '__main__':
93 pprint = pprint.PrettyPrinter()
94 pprint.pprint(Config().__dict__.items())