2 # PLCAPI configuration store. Supports XML-based configuration file
3 # format exported by MyPLC.
5 # Mark Huang <mlhuang@cs.princeton.edu>
6 # Copyright (C) 2004-2006 The Trustees of Princeton University
12 from PLC.Faults import *
13 from PLC.Debug import profile
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
19 myplc = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + \
24 Parse the bash/Python/PHP version of the configuration file. Very
25 fast but no type conversions.
28 def __init__(self, file = "/etc/planetlab/plc_config"):
31 exec(compile(open(file).read(), file, 'exec'), self.__dict__)
35 exec(compile(open(myplc + os.sep + "plc_config").read(), myplc + os.sep + "plc_config", 'exec'), self.__dict__)
37 raise PLCAPIError("Could not find plc_config in " + \
39 myplc + os.sep + "plc_config")
43 Parse the XML configuration file directly. Takes longer but is
44 presumably more accurate.
47 def __init__(self, file = "/etc/planetlab/plc_config.xml"):
49 from plc_config import PLCConfiguration
51 sys.path.append(myplc)
52 from plc_config import PLCConfiguration
56 cfg = PLCConfiguration(file)
60 cfg = PLCConfiguration(myplc + os.sep + "plc_config.xml")
62 raise PLCAPIError("Could not find plc_config.xml in " + \
64 myplc + os.sep + "plc_config.xml")
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
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":
80 value = variable['value']
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
87 shell_name = category['id'].upper() + "_" + variable['id'].upper()
88 setattr(self, shell_name, value)
90 if __name__ == '__main__':
92 pprint = pprint.PrettyPrinter()
93 pprint.pprint(list(Config().__dict__.items()))