~nacl/wicd/aqua-curses

« back to all changes in this revision

Viewing changes to wicd/util/orderedproviderhandler.py

  • Committer: Adam Blackburn
  • Date: 2009-07-04 23:27:35 UTC
  • mto: This revision was merged to the branch mainline in revision 20.
  • Revision ID: compwiz18@gmail.com-20090704232735-8ho5040eyhseb8ur
moved dhcp service and config service to wicd.services

Show diffs side-by-side

added added

removed removed

Lines of Context:
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=[]))
38
46
 
 
47
    def _start_loading_plugins(self, *args):
 
48
        self._loading_plugins = True
 
49
 
 
50
    def _done_loading_plugins(self, *args):
 
51
        self._loading_plugins = False 
 
52
 
39
53
    def changed(self, *args):
40
54
        self.set_order(self.config.get('order', default=[]), save=False)
41
55
 
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?
43
60
        if save:
44
61
            self.config['order'] = order
45
62
            self.config.write()
 
63
 
 
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:
49
68
                    return provider
50
69
 
 
70
        # grab a list of the providers we currently have
51
71
        remaining = self.get_providers()
52
72
 
 
73
        # if there are any
53
74
        if remaining:
 
75
            # try and sort them
54
76
            self.ordered_providers = []
 
77
            # loop through the names in the order specified
55
78
            for name in order:
 
79
                # try and find the class with the correct name
56
80
                classx = find_by_name(name, remaining)
57
81
                if classx:
 
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
61
89
                    raise ValueError(
62
90
                        'must_order is True, '
63
91
                        'but not all providers could be found')
64
92
                else:
 
93
                    # if must_order isn't on, then we can continue
 
94
                    # but issue a warning
65
95
                    logger.warning('could not find provider %s',
66
96
                                   name)
67
97
 
68
98
            if remaining:
 
99
                # if there were left over providers, warn about them
69
100
                logger.warning('not all providers for %s were ordered',
70
101
                              self.servicename)
 
102
                # and then tack them on the end
71
103
                self.ordered_providers.extend(remaining)
72
104
 
73
105
            logger.debug('providers for %s ordered: %s',
76
108
 
77
109
        
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:
 
118
            # no providers
 
119
            logger.warning('no providers available')
 
120
            # return None
 
121
            return None
82
122
        else:
 
123
            # providers haven't been ordered
83
124
            if self.must_order:
84
125
                raise ValueError(
85
126
                    'must_order is True, but providers are not ordered'
86
127
                )
87
128
 
 
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
92
 
                return providers[0]
93
 
            else:
94
 
                logger.warning('no providers available')
95
 
                return None
 
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)
 
134
            return providers[0]