28
28
def __init__(self, servicename, must_order=False):
29
29
super(OrderedProviderHandler, self).__init__(servicename)
30
30
self.must_order = must_order
31
self._loading_plugins = False
32
# make sure to to rerun set_order if something changes
31
33
event.add_callback(self.changed, '%s_provider_added' % servicename)
32
34
event.add_callback(self.changed, '%s_provider_removed' % servicename)
35
event.add_callback(self.changed, 'done-loading')
36
# also hook on to start-loading-plugins and stop-loading-plugins
37
event.add_callback(self._start_loading_plugins,
38
'start-loading-plugins')
39
event.add_callback(self._done_loading_plugins,
40
'done-loading-plugins')
41
# grab the config path
33
42
config_path = os.path.join(paths.SYSTEM_CONF, '%s.conf' % servicename)
43
# create a ConfigObj config file
34
44
self.config = ConfigObj(config_path, unrepr=True)
35
logger.debug('getting value from config: %s' %
36
self.config.get('order', default=[]))
37
45
self.set_order(self.config.get('order', default=[]))
47
def _start_loading_plugins(self, *args):
48
self._loading_plugins = True
50
def _done_loading_plugins(self, *args):
51
self._loading_plugins = False
39
53
def changed(self, *args):
40
54
self.set_order(self.config.get('order', default=[]), save=False)
42
56
def set_order(self, order, save=True):
57
# don't bother while we're loading plugins
58
if self._loading_plugins: return
59
# should we save this new order?
44
61
self.config['order'] = order
45
62
self.config.write()
64
# make it easy to find providers by name
46
65
def find_by_name(name, listing):
47
66
for provider in listing:
48
67
if provider.name == name:
70
# grab a list of the providers we currently have
51
71
remaining = self.get_providers()
54
76
self.ordered_providers = []
77
# loop through the names in the order specified
79
# try and find the class with the correct name
56
80
classx = find_by_name(name, remaining)
82
# if we found it, stick it in the list
58
83
self.ordered_providers.append(classx)
84
# and remove it from remaining
59
85
remaining.remove(classx)
60
86
elif self.must_order:
87
# if we didn't find it and must_order is on
88
# then we need to stop
62
90
'must_order is True, '
63
91
'but not all providers could be found')
93
# if must_order isn't on, then we can continue
65
95
logger.warning('could not find provider %s',
99
# if there were left over providers, warn about them
69
100
logger.warning('not all providers for %s were ordered',
102
# and then tack them on the end
71
103
self.ordered_providers.extend(remaining)
73
105
logger.debug('providers for %s ordered: %s',
78
110
def get_preferred_provider(self):
111
# check to make sure they've been ordered
112
# and that there are actually providers
79
113
if hasattr(self, 'ordered_providers') and \
80
114
len(self.ordered_providers) > 0:
115
# if so, return the first one
81
116
return self.ordered_providers[0]
117
elif len(providers) == 0:
119
logger.warning('no providers available')
123
# providers haven't been ordered
83
124
if self.must_order:
85
126
'must_order is True, but providers are not ordered'
129
# get a list of the providers
88
130
providers = self.get_providers()
89
if len(providers) > 0:
90
logger.warning('returned random provider %s', providers[0])
91
# we haven't ordered them yet
94
logger.warning('no providers available')
131
# warn that they haven't been sorted
132
logger.warning('returned random provider %s', providers[0])
133
# return the first one (it's random)