Example #1
0
 def __init__(self, add_cb, charmstore_column, config, series):
     self.add_cb = add_cb
     self.charmstore_column = charmstore_column
     self.config = config
     self.api = CharmStoreAPI(series=series)
     self.search_delay_alarm = None
     self.search_text = ""
     self._search_future = None
     self._search_result = None
     self._popular_results = None
     w = self.build_widgets()
     super().__init__(w)
     # populate popular_results with an initial search
     self.really_search()
 def __init__(self, add_cb, charmstore_column, config, series):
     self.add_cb = add_cb
     self.charmstore_column = charmstore_column
     self.config = config
     self.api = CharmStoreAPI(series=series)
     self.search_delay_alarm = None
     self.search_text = ""
     self._search_future = None
     self._search_result = None
     self._popular_results = None
     w = self.build_widgets()
     super().__init__(w)
     # populate popular_results with an initial search
     self.really_search()
Example #3
0
class CharmStoreSearchWidget(WidgetWrap):
    def __init__(self, add_cb, charmstore_column, config, series):
        self.add_cb = add_cb
        self.charmstore_column = charmstore_column
        self.config = config
        self.api = CharmStoreAPI(series=series)
        self.search_delay_alarm = None
        self.search_text = ""
        self._search_future = None
        self._search_result = None
        self._popular_results = None
        w = self.build_widgets()
        super().__init__(w)
        # populate popular_results with an initial search
        self.really_search()

    def build_widgets(self):
        self.editbox = Edit(caption=('text', "Search Charm Store: "))

        connect_signal(self.editbox, 'change', self.handle_edit_changed)

        return Padding(AttrMap(self.editbox, 'filter', 'filter_focus'),
                       left=2,
                       right=2)

    def _handle_search_done(self, future):
        assert (future == self._search_future)
        self._search_result = self._search_future.result()
        self._search_future = None

        if self._popular_results is None:
            self._popular_results = self._search_result

        # result being None indicates an error, which was handled by
        # handle_search_error.
        if self._search_result:
            br, cr = self._search_result
            self.set_column(br, cr)
        self.charmstore_column.loading = False
        self.charmstore_column.update()

    def set_column(self, bundle_results, charm_results):
        self.charmstore_column.clear_search_results()
        self.charmstore_column.add_results(bundle_results, charm_results)
        self.charmstore_column.update()

    def handle_edit_changed(self, sender, userdata):
        self.search_text = userdata
        self.enqueue_search()
        self.charmstore_column.handle_search_change(userdata)

    def enqueue_search(self):
        if self.search_delay_alarm:
            EventLoop.remove_alarm(self.search_delay_alarm)
        self.search_delay_alarm = EventLoop.set_alarm_in(
            0.5, self.really_search)

    def really_search(self, *args, **kwargs):
        self.search_delay_alarm = None
        self._search_future = self.api.get_matches(self.search_text,
                                                   self.handle_search_error)
        self._search_future.add_done_callback(self._handle_search_done)

    def handle_search_error(self, e):
        self.charmstore_column.handle_error(e)

    def do_add_charm(self, sender):
        self.add_cb(self.search_text, self._search_result)
class CharmStoreSearchWidget(WidgetWrap):

    def __init__(self, add_cb, charmstore_column, config, series):
        self.add_cb = add_cb
        self.charmstore_column = charmstore_column
        self.config = config
        self.api = CharmStoreAPI(series=series)
        self.search_delay_alarm = None
        self.search_text = ""
        self._search_future = None
        self._search_result = None
        self._popular_results = None
        w = self.build_widgets()
        super().__init__(w)
        # populate popular_results with an initial search
        self.really_search()

    def build_widgets(self):
        self.editbox = Edit(caption=('text', "Search Charm Store: "))

        connect_signal(self.editbox, 'change',
                       self.handle_edit_changed)

        return Padding(AttrMap(self.editbox,
                               'filter', 'filter_focus'), left=2, right=2)

    def _handle_search_done(self, future):
        assert(future == self._search_future)
        self._search_result = self._search_future.result()
        self._search_future = None

        if self._popular_results is None:
            self._popular_results = self._search_result

        # result being None indicates an error, which was handled by
        # handle_search_error.
        if self._search_result:
            br, cr = self._search_result
            self.set_column(br, cr)
        self.charmstore_column.loading = False
        self.charmstore_column.update()

    def set_column(self, bundle_results, charm_results):
        self.charmstore_column.clear_search_results()
        self.charmstore_column.add_results(bundle_results, charm_results)
        self.charmstore_column.update()

    def handle_edit_changed(self, sender, userdata):
        self.search_text = userdata
        self.enqueue_search()
        self.charmstore_column.handle_search_change(userdata)

    def enqueue_search(self):
        if self.search_delay_alarm:
            EventLoop.remove_alarm(self.search_delay_alarm)
        self.search_delay_alarm = EventLoop.set_alarm_in(0.5,
                                                         self.really_search)

    def really_search(self, *args, **kwargs):
        self.search_delay_alarm = None
        self._search_future = self.api.get_matches(self.search_text,
                                                   self.handle_search_error)
        self._search_future.add_done_callback(self._handle_search_done)

    def handle_search_error(self, e):
        self.charmstore_column.handle_error(e)

    def do_add_charm(self, sender):
        self.add_cb(self.search_text, self._search_result)