def stop(self): GeneratorTask.stop(self) try: if hasattr(self, '_process'): self._process.kill() except OSError: pass
def refresh_index(self): def _refresh_index_finished(): self._view.set_items(self.list) def _refresh_index_add(item): self.list.append(item) def _refresh_index(): try: fp = self._file.open() except IOError: return data = '' zap = True for line in fp: line = line.rstrip('\n') data += line.strip(' ') + ' ' if line == '': t = data.split(' ', 1) if zap == False: if data != '' and t[1].strip(' ') != 'Not Issued.': yield RfcItem(number=t[0], data=t[1]) data = '' elif t[0] == '0001': zap = False elif zap == True: data = '' fp.close() self.list = [] self._view.clear() task = GeneratorTask(_refresh_index, _refresh_index_add, _refresh_index_finished) task.start()
def download_index(self): if self.task != None: self.task.stop() def _download_index_finished(): self._view.show_progressbar(False) self.get_action('rfc_downloadindex').set_sensitive(True) self.boss.cmd('notify', 'notify', title=_('RFC'), data=_('Index download completed')) gcall(self.refresh_index) self.task = GeneratorTask(self._download_index, _download_index_finished) self.task.start()
def create_ui(self): self.grepper_dir = '' self.matches_list.set_columns([ Column( 'linenumber', editable=False, title="#", ), Column('path', editable=False, use_markup=True, sorted=True), Column('line', expand=True, editable=False, use_markup=True), ]) # we should set this to the current project I think self.path_chooser.set_filename(os.path.expanduser('~/')) self._history = gtk.ListStore(gobject.TYPE_STRING) self.pattern_combo.set_model(self._history) self.pattern_combo.set_text_column(0) self.recursive.set_active(True) self.re_check.set_active(True) self.pattern_entry = self.pattern_combo.child self.pattern_entry.connect('activate', self._on_pattern_entry_activate) self.task = GeneratorTask(self.svc.grep, self.append_to_matches_list, self.grep_complete, pass_generator=True) self.running = False
def create_ui(self): # filter select self.filter_select.prefill(get_filters()) self.match_list.set_columns([ Column('icon_stock_id', use_stock=True, title=' '), #Column('state_markup', use_markup=True, title=' '), Column('markup', use_markup=True, title=_('Name')), Column('path', title=_('Path')) ]) # add standard filter self.new_filter(FileNameMatchesFilter) # task for asynchrounus searching # ``append_to_match_list`` is called if a match was found # ``search_finished`` is called at the end of search self.task = GeneratorTask(do_search, self.append_to_match_list, self.search_finished)
def update_to_path(self, new_path=None, select=None): if new_path is None: new_path = self.path else: self.path = check_or_home(new_path) self.file_list.clear() self.entries.clear() if self.svc.opt('show_parent'): parent = os.path.normpath(os.path.join(new_path, os.path.pardir)) # skip if we are already on the root if parent != new_path: self.add_or_update_file(os.pardir, parent, 'normal', parent_link=True) def work(basepath): dir_content = listdir(basepath) # add all files from vcs and remove the corresponding items # from dir_content for item in self.svc.boss.cmd('versioncontrol', 'list_file_states', path=self.path): if (item[1] == self.path): try: dir_content.remove(item[0]) except: pass yield item # handle remaining files for filename in dir_content: if (path.isdir(path.join(basepath, filename))): state = 'normal' else: state = 'unknown' yield filename, basepath, state # wrap add_or_update_file to set select accordingly def _add_or_update_file(name, basepath, state): self.add_or_update_file(name, basepath, state, select=(name == select)) GeneratorTask(work, _add_or_update_file).start(self.path) self.create_ancest_tree()
def set_outliner(self, outliner, document): # see comments on set_validator old = self.tasks.get(self.document, None) if old: old.priority = PRIO_DEFAULT self.document = document self.clear() if document in self.tasks: # set the priority of the current validator higher, so it feels # faster on the current view if self.svc.boss.window.paned.is_visible_pane(self.pane): prio = PRIO_FOREGROUND else: prio = PRIO_DEFAULT self.tasks[document].priorty = prio # when restart is set, the set_validator is run again so the # list gets updated from the validator cache. this happens when # the buffer switched to another file and back again self.restart = True self.svc.log.debug(_('Outliner task for {doc} already running'), doc=document) return self.restart = False if outliner: # if self.task: # self.task.stop() # self.task = GeneratorTask(outliner.get_outline_cached, self.add_node) # self.task.start() def wrap_add_node(document, *args): # we need this proxy function as a task may be still running in # background and the document already switched # this way we still can fill up the cache by letting the task run # sometimes args have a lengh of 0 so we have to catch this if self.document == document and args: self.add_node(*args) def on_complete(document, outliner): del self.tasks[document] outliner.sync() # fire refilter so the list is updated after buffer changed self.filter_model.refilter() # refire the task and hope the cache will just display stuff, # elsewise the task is run again if document == self.document and self.restart: self.set_outliner(outliner, document) radd = partial(wrap_add_node, document) rcomp = partial(on_complete, document, outliner) if self.svc.boss.window.paned.is_visible_pane(self.pane): prio = PRIO_FOREGROUND else: prio = PRIO_DEFAULT task = GeneratorTask(outliner.run_cached, radd, complete_callback=rcomp, priority=prio) self.tasks[document] = task task.start()
def set_validator(self, validator, document): # this is quite an act we have to do here because of the many cornercases # 1. Jobs once started run through. This is for caching purpuses as a validator # is supposed to cache results, somehow. # 2. buffers can switch quite often and n background jobs are still # running # set the old task job to default priorty again old = self.tasks.get(self.document, None) if old: old.priority = PRIO_LOW self.document = document self.clear() if self.tasks.has_key(document): # set the priority of the current validator higher, so it feels # faster on the current view if self.svc.boss.window.paned.is_visible_pane(self.pane): prio = PRIO_FOREGROUND else: prio = PRIO_DEFAULT self.tasks[document].priorty = prio # when restart is set, the set_validator is run again so the # list gets updated from the validator cache. this happens when # the buffer switched to another file and back again self.restart = True self.svc.log.debug(_('Validator task for {doc} already running'), doc=document) return self.restart = False if validator: def wrap_add_node(document, *args): # we need this proxy function as a task may be still running in # background and the document already switched # this way we still can fill up the cache by letting the task run # sometimes args have a lengh of 0 so we have to catch this if self.document == document and args: item = args[0] self.add_node(item) if self._last_selected: if self._last_selected[0] == self.document: if item.lineno == self._last_selected[1]: self.errors_ol.selected_item = item def on_complete(document, validator): del self.tasks[document] # refire the task and hope the cache will just display stuff, # elsewise the task is run again validator.sync() if document == self.document and self.restart: self.set_validator(validator, document) radd = partial(wrap_add_node, document) rcomp = partial(on_complete, document, validator) if self.svc.boss.window.paned.is_visible_pane(self. pane): prio = PRIO_FOREGROUND else: prio = PRIO_DEFAULT task = GeneratorTask(validator.run_cached, radd, complete_callback=rcomp, priority=prio) self.tasks[document] = task task.start()
def book_loaded(self, bookmarks): self.contents_tree.clear() task = GeneratorTask(bookmarks.get_subs, self.add_bookmark) task.start() self.view_book.set_current_page(1)
def fetch_books(self): self.books_list.clear() self.contents_tree.clear() task = GeneratorTask(fetch_books, self.add_book) task.start()
def __init__(self, stdout_callback, complete_callback=None): GeneratorTask.__init__(self, self.start_process, stdout_callback, complete_callback)
class Rfc(Service): """Fetch rfc list and show an rfc""" actions_config = RfcActions features_config = RfcFeaturesConfig url_rfcindex = 'http://www.ietf.org/download/rfc-index.txt' url_rfctmpl = 'http://tools.ietf.org/html/rfc' buffer_len = 16384 def start(self): self._file = home()/'rfc-index.txt' self._view = RfcView(self) self._has_loaded = False self.list = [] self.counter = 0 self.task = None self._filter_id = 0 self.is_refresh = False def show_rfc(self): self.boss.cmd('window', 'add_view', paned='Plugin', view=self._view) if not self._has_loaded: self._has_loaded = True if not self.is_refresh: gcall(self.refresh_index) self.is_refresh = True def hide_rfc(self): self.boss.cmd('window', 'remove_view', view=self._view) def download_index(self): if self.task != None: self.task.stop() def _download_index_finished(): self._view.show_progressbar(False) self.get_action('rfc_downloadindex').set_sensitive(True) self.boss.cmd('notify', 'notify', title=_('RFC'), data=_('Index download completed')) gcall(self.refresh_index) self.task = GeneratorTask(self._download_index, _download_index_finished) self.task.start() def refresh_index(self): def _refresh_index_finished(): self._view.set_items(self.list) def _refresh_index_add(item): self.list.append(item) def _refresh_index(): try: fp = self._file.open() except IOError: return data = '' zap = True for line in fp: line = line.rstrip('\n') data += line.strip(' ') + ' ' if line == '': t = data.split(' ', 1) if zap == False: if data != '' and t[1].strip(' ') != 'Not Issued.': yield RfcItem(number=t[0], data=t[1]) data = '' elif t[0] == '0001': zap = False elif zap == True: data = '' fp.close() self.list = [] self._view.clear() task = GeneratorTask(_refresh_index, _refresh_index_add, _refresh_index_finished) task.start() def filter(self, pattern): self._filter_id += 1 gcall(self._filter, pattern, self._filter_id) def _filter(self, pattern, id): if pattern == '': if self._filter_id == id: self._view.set_items(self.list) else: r = re.compile(pattern, re.IGNORECASE) list = [item for item in self.list if r.search(item.data)] if self._filter_id == id: self._view.set_items(list) def _download_index(self): self.get_action('rfc_downloadindex').set_sensitive(False) self._view.show_progressbar(True) sock = urllib.urlopen(self.url_rfcindex) fp = self._filename.open('w') progress_max = 0 progress_current = 0 if sock.headers.has_key('content-length'): progress_max = int(sock.headers.getheader('content-length')) try: while True: buffer = sock.read(self.buffer_len) if buffer == '': break fp.write(buffer) progress_current += len(buffer) gcall(self._view.update_progressbar, progress_current, progress_max) finally: sock.close() fp.close() yield None def browse(self, id): self.boss.cmd('browseweb', 'browse', url=(self.url_rfctmpl + id)) def stop(self): if self.task != None: self.task.stop() if self.get_action('show_rfc').get_active(): self.hide_rfc()
def set_current_document(self, document): self._current = document if self._current is not None: task = GeneratorTask(self.check_current, self.add_todo_item) task.start()
class SearchView(PidaView): key = 'filesearch.form' gladefile = 'search' locale = locale label_text = _('File Search') icon_name = 'search' filters = [] running = False entries = {} def create_ui(self): # filter select self.filter_select.prefill(get_filters()) self.match_list.set_columns([ Column('icon_stock_id', use_stock=True, title=' '), #Column('state_markup', use_markup=True, title=' '), Column('markup', use_markup=True, title=_('Name')), Column('path', title=_('Path')) ]) # add standard filter self.new_filter(FileNameMatchesFilter) # task for asynchrounus searching # ``append_to_match_list`` is called if a match was found # ``search_finished`` is called at the end of search self.task = GeneratorTask(do_search, self.append_to_match_list, self.search_finished) def on_add_button__clicked(self, btn): # get selected filter f = self.filter_select.read() self.new_filter(f) def on_search_button__clicked(self, btn): if not self.running: if self.validate(): self.start() else: self.stop() def on_match_list__row_activated(self, rowitem, search_match): self.svc.boss.cmd('buffer', 'open_file', file_name=path.join(search_match.path, search_match.name)) self.svc.boss.editor.cmd('grab_focus') def can_be_closed(self): self.stop() return True def start(self): """ Start the asynchrounus search task. """ self.running = True self.match_list.clear() self.entries = {} self.update_match_count(0) self.search_button.set_label(gtk.STOCK_STOP) self.task.start(self.get_search_folder(), self.filters) def stop(self): """ Stop the abort task. """ self.task.stop() self.search_finished() def new_filter(self, f): """ This function adds a new filter to the GUI and registers it in `self.filters``. """ entries = f.get_entries() new_filter = f(**entries) box = gtk.HBox(False, 5) box.pack_start(gtk.Label(f.description), expand=False) # filter entry objects for name, entry in entries.iteritems(): box.pack_start(entry) entry.connect('activate', self.on_search_button__clicked) # remove button def remove_btn_clicked(btn): btn.parent.destroy() self.filters.remove(new_filter) btn = gtk.Button() btn.connect('clicked', remove_btn_clicked) img = gtk.image_new_from_stock(gtk.STOCK_REMOVE, gtk.ICON_SIZE_MENU) btn.set_image(img) box.pack_start(btn, expand=False) self.filter_box.pack_start(box) box.show_all() self.filters.append(new_filter) def set_search_folder(self, folder): self.select_folder.set_current_folder(folder) def get_search_folder(self): """ Returns the last folder opened in the filemanager. If it's not available, it returns the path to the project root instead. """ folder = self.select_folder.get_current_folder() # XXX: Windows? if folder == '/': folder = self.svc.current_project.source_directory return folder def validate(self): """ Tell all filters to validate their input fields. If a filter raises a ``ValidationError`` the user is shown an error message. """ for f in self.filters: try: f.validate() except ValidationError, e: self.svc.error_dlg(e) return False return True
def set_validator(self, validator, document): # this is quite an act we have to do here because of the many cornercases # 1. Jobs once started run through. This is for caching purpuses as a validator # is supposed to cache results, somehow. # 2. buffers can switch quite often and n background jobs are still # running # set the old task job to default priorty again old = self.tasks.get(self.document, None) if old: old.priority = PRIO_LOW self.document = document self.clear() if self.tasks.has_key(document): # set the priority of the current validator higher, so it feels # faster on the current view if self.svc.boss.window.paned.is_visible_pane(self.pane): prio = PRIO_FOREGROUND else: prio = PRIO_DEFAULT self.tasks[document].priorty = prio # when restart is set, the set_validator is run again so the # list gets updated from the validator cache. this happens when # the buffer switched to another file and back again self.restart = True self.svc.log.debug(_('Validator task for {doc} already running'), doc=document) return self.restart = False if validator: def wrap_add_node(document, *args): # we need this proxy function as a task may be still running in # background and the document already switched # this way we still can fill up the cache by letting the task run # sometimes args have a lengh of 0 so we have to catch this if self.document == document and args: item = args[0] self.add_node(item) if self._last_selected: if self._last_selected[0] == self.document: if item.lineno == self._last_selected[1]: self.errors_ol.selected_item = item def on_complete(document, validator): del self.tasks[document] # refire the task and hope the cache will just display stuff, # elsewise the task is run again validator.sync() if document == self.document and self.restart: self.set_validator(validator, document) radd = partial(wrap_add_node, document) rcomp = partial(on_complete, document, validator) if self.svc.boss.window.paned.is_visible_pane(self.pane): prio = PRIO_FOREGROUND else: prio = PRIO_DEFAULT task = GeneratorTask(validator.run_cached, radd, complete_callback=rcomp, priority=prio) self.tasks[document] = task task.start()