Fix version output when missing.
[plcapi.git] / PLC / Config.py
1 #!/usr/bin/python
2 #
3 # PLCAPI configuration store. Supports XML-based configuration file
4 # format exported by MyPLC.
5 #
6 # Mark Huang <mlhuang@cs.princeton.edu>
7 # Copyright (C) 2004-2006 The Trustees of Princeton University
8 #
9 # $Id$
10 # $URL$
11 #
12
13 import os
14 import sys
15
16 from PLC.Faults import *
17 from PLC.Debug import profile
18
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
22 # in site-packages.
23 myplc = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + \
24         os.sep + "myplc"
25
26 class Config:
27     """
28     Parse the bash/Python/PHP version of the configuration file. Very
29     fast but no type conversions.
30     """
31
32     def __init__(self, file = "/etc/planetlab/plc_config"):
33         # Load plc_config
34         try:
35             execfile(file, self.__dict__)
36         except:
37             # Try myplc directory
38             try:
39                 execfile(myplc + os.sep + "plc_config", self.__dict__)
40             except:
41                 raise PLCAPIError("Could not find plc_config in " + \
42                                   file + ", " + \
43                                   myplc + os.sep + "plc_config")
44
45 class XMLConfig:
46     """
47     Parse the XML configuration file directly. Takes longer but is
48     presumably more accurate.
49     """
50
51     def __init__(self, file = "/etc/planetlab/plc_config.xml"):
52         try:
53             from plc_config import PLCConfiguration
54         except:
55             sys.path.append(myplc)
56             from plc_config import PLCConfiguration
57
58         # Load plc_config.xml
59         try:
60             cfg = PLCConfiguration(file)
61         except:
62             # Try myplc directory
63             try:
64                 cfg = PLCConfiguration(myplc + os.sep + "plc_config.xml")
65             except:
66                 raise PLCAPIError("Could not find plc_config.xml in " + \
67                                   file + ", " + \
68                                   myplc + os.sep + "plc_config.xml")
69
70         for (category, variablelist) in cfg.variables().values():
71             for variable in variablelist.values():
72                 # Try to cast each variable to an appropriate Python
73                 # type.
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":
80                         value = True
81                     else:
82                         value = False
83                 else:
84                     value = variable['value']
85
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
90                 # names.
91                 shell_name = category['id'].upper() + "_" + variable['id'].upper()
92                 setattr(self, shell_name, value)
93
94 if __name__ == '__main__':
95     import pprint
96     pprint = pprint.PrettyPrinter()
97     pprint.pprint(Config().__dict__.items())