forked from PanDAWMS/pilot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventServiceFactory.py
53 lines (42 loc) · 1.71 KB
/
EventServiceFactory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Class definition:
# EventServiceFactory
# This class is used to generate EventService class objects corresponding to a given "experiment"
# Based the on Factory Design Pattern
# Note: not compatible with Singleton Design Pattern due to the subclassing
from types import TypeType
from EventService import EventService
from ATLASEventService import ATLASEventService
class EventServiceFactory(object):
def newEventService(self, experiment):
""" Generate a new site information object """
# get all classes
eventServiceClasses = [j for (i,j) in globals().iteritems() if isinstance(j, TypeType) and issubclass(j, EventService)]
print eventServiceClasses
# loop over all subclasses
for eventServiceClass in eventServiceClasses:
si = eventServiceClass()
# return the matching eventService class
if si.getEventService() == experiment:
return eventServiceClass
# if no class was found, raise an error
raise ValueError('EventServiceFactory: No such class: "%s"' % (eventServiceClass))
if __name__ == "__main__":
factory = EventServiceFactory()
print "\nAttempting to get ATLAS"
try:
eventServiceClass = factory.newEventService('ATLAS')
except Exception, e:
print e
else:
si = eventServiceClass()
print 'got eventService:',si.getEventService()
del eventServiceClass
print "\nAttempting to get Dummy"
try:
eventServiceClass = factory.newEventService('Dummy')
except Exception, e:
print e
else:
si = eventServiceClass()
print 'got eventService:',si.getEventService()
del eventServiceClass