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
15 from PLC.Faults import *
16 from PLC.Debug import profile
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
22 myplc = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + \
27 Parse the bash/Python/PHP version of the configuration file. Very
28 fast but no type conversions.
31 def __init__(self, file = "/etc/planetlab/plc_config"):
34 execfile(file, self.__dict__)
38 execfile(myplc + os.sep + "plc_config", self.__dict__)
40 raise PLCAPIError("Could not find plc_config in " + \
42 myplc + os.sep + "plc_config")
46 Parse the XML configuration file directly. Takes longer but is
47 presumably more accurate.
50 def __init__(self, file = "/etc/planetlab/plc_config.xml"):
52 from plc_config import PLCConfiguration
54 sys.path.append(myplc)
55 from plc_config import PLCConfiguration
59 cfg = PLCConfiguration(file)
63 cfg = PLCConfiguration(myplc + os.sep + "plc_config.xml")
65 raise PLCAPIError("Could not find plc_config.xml in " + \
67 myplc + os.sep + "plc_config.xml")
69 for (category, variablelist) in cfg.variables().values():
70 for variable in variablelist.values():
71 # Try to cast each variable to an appropriate Python
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":
83 value = variable['value']
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
90 shell_name = category['id'].upper() + "_" + variable['id'].upper()
91 setattr(self, shell_name, value)
93 if __name__ == '__main__':
95 pprint = pprint.PrettyPrinter()
96 pprint.pprint(Config().__dict__.items())