+
+class Auth:
+ def __init__(self):
+ self.auth = {'AuthMethod': "anonymous"}
+
+# NOTE: this host is used by default when there are no auth files.
+XMLRPC_SERVER="https://boot.planet-lab.org/PLCAPI/"
+
+# NOTE: by default, use anonymous access, but if auth files are
+# configured, use them, with their auth definitions.
+auth = Auth()
+try:
+ from monitor import config
+ auth.auth = {'Username' : config.API_AUTH_USER,
+ 'AuthMethod' : 'password',
+ 'AuthString' : config.API_AUTH_PASSWORD}
+ auth.server = config.API_SERVER
+except:
+ try:
+ import auth
+ auth.server = auth.plc
+ except:
+ auth = Auth()
+ auth.server = XMLRPC_SERVER
+
+api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
+
+global_error_count = 0
+
+class PLC:
+ def __init__(self, auth, url):
+ self.auth = auth
+ self.url = url
+ self.api = xmlrpclib.Server(self.url, verbose=False, allow_none=True)
+
+ def __getattr__(self, name):
+ method = getattr(self.api, name)
+ if method is None:
+ raise AssertionError("method does not exist")
+
+ try:
+ return lambda *params : method(self.auth, *params)
+ except ProtocolError:
+ traceback.print_exc()
+ global_error_count += 1
+ if global_error_count >= 10:
+ print "maximum error count exceeded; exiting..."
+ sys.exit(1)
+ else:
+ print "%s errors have occurred" % global_error_count
+ raise Exception("ProtocolError continuing")
+
+ def __repr__(self):
+ return self.api.__repr__()
+
+def getAPI(url):
+ return xmlrpclib.Server(url, verbose=False, allow_none=True)
+
+def getAuthAPI():
+ return PLC(auth.auth, auth.server)