blob: 69dad295a0761edbc81fd9695ded0fa109608fdc [file] [log] [blame]
Vince Lehman3b8bc652015-06-18 15:01:47 -05001#!/usr/bin/python
2
3import os
4
5class _ExperimentManager:
6
7 class Error(Exception):
8 def __init__(self, what):
9 self.what = what
10 def __str__(self):
11 return repr(self.what)
12
13 instance = None
14
15 def __init__(self):
16 self.experiments = {}
17
18 def loadModules(self):
19 currentDir = os.path.dirname(__file__)
20 experimentDir = "%s/%s" % (currentDir, "experiments")
21 experimentModule = "ndn.experiments"
22
23 # Import and register experiments
24 for root, dirs, files in os.walk(experimentDir):
25 for filename in files:
26 if filename.endswith(".py") and filename != "__init__.py":
27 module = filename.replace(".py", "")
28 __import__("%s.%s" % (experimentModule, module))
29
30 def register(self, name, experimentClass):
31 if name not in self.experiments:
32 self.experiments[name] = experimentClass
33 else:
34 raise _ExperimentManager.Error("Experiment '%s' has already been registered" % name)
35
36 def create(self, name, args):
37 if name in self.experiments:
38 return self.experiments[name](args)
39 else:
40 return None
41
42def __getInstance():
43 if _ExperimentManager.instance is None:
44 _ExperimentManager.instance = _ExperimentManager()
45 _ExperimentManager.instance.loadModules()
46
47 return _ExperimentManager.instance
48
49def register(name, experimentClass):
50 manager = __getInstance()
51 manager.register(name, experimentClass)
52
53def create(name, args):
54 manager = __getInstance()
55 return manager.create(name, args)
56
57def getExperimentNames():
58 manager = __getInstance()
59
60 experimentNames = []
61
62 for key in manager.experiments:
63 experimentNames.append(key)
64
65 return experimentNames