def run(self): """Функция восставливления жизней.""" logger.debug('Выполняю восстановление жизней.') if self.mw.current_hp() == self.mw.max_hp(): logger.info('Жизни полные!') return self.mw.click_tag(".life .plus-icon") self._timer.start() loop = QEventLoop() self._find_restore_hp_window_finded.connect(loop.quit) loop.exec_() logger.debug('Нажимаю на кнопку "Вылечиться".') button = self._restore_hp_window.findFirst('button') button.evaluateJavaScript('this.click()') # Ждем пока жизни полностью восстановятся -- иначе может случится так, что # бот нападет в тот момент, когда жизни еще регенятся, и тогда не получится напасть и # бот дальше фазы выбора противника не продвинется if self.mw.current_hp() < self.mw.max_hp(): logger.info('Жизни (%s/%s) не полные -- жду восстановления.', self.mw.current_hp(), self.mw.max_hp()) self._timer_check_max_hp.start() self._hp_maximum.connect(loop.quit) loop.exec_() logger.info('Жизни восстановлены.')
def run(self, url): """Функция выполняет парсинг страницы объявления""" self.url = url self.phone = None self.t = time.clock() # Загружаем страницу объявления self.web_page.mainFrame().load(url) logger.debug('Начало выполнения загрузки "{}" {:.3f} секунд'.format( url, time.clock() - self.t)) # Ждем тут, пока закончится парсинг объявления -- все из-за ассинхронности webpage и моей лени -- хочется # просто в цикле запустить обработку и url, но из-за асинхронности с сигналами это не сработает -- какая-то # лажа происходит -- падает приложение с ошибкой где-то в QtCore.dll loop = QEventLoop() self.parse_phone_finished.connect(loop.quit) loop.exec_() logger.debug( 'Время выполнения парсера {:.3f} секунд'.format(time.clock() - self.t)) self.t = None
def get_site_text(url='https://test.api.unistream.com/help/index.html'): """Функция возвращает содержимое по указанному url.""" import sys from PySide.QtGui import QApplication from PySide.QtCore import QEventLoop from PySide.QtWebKit import QWebSettings, QWebPage, QWebView from PySide.QtNetwork import QNetworkProxyFactory # Чтобы не было проблем запуска компов с прокси: QNetworkProxyFactory.setUseSystemConfiguration(True) QWebSettings.globalSettings().setAttribute( QWebSettings.DeveloperExtrasEnabled, True) class WebPage(QWebPage): def userAgentForUrl(self, url): return 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0' if QApplication.instance() is None: QApplication(sys.argv) view = QWebView() view.setPage(WebPage()) view.load(url) # Ждем пока прогрузится страница loop = QEventLoop() view.loadFinished.connect(loop.quit) loop.exec_() doc = view.page().mainFrame().documentElement() print(len(doc.toOuterXml()), len(doc.toPlainText())) return doc.toPlainText()
def wait_for_signal(signal, timeout=10000): """Waits the given signal to be emitted, or breaks after a timeout This context manager wraps a method of using a nested event loop to wait for a signal, but additionally adds a timeout in case the signal was prematurely emitted (in which case we will never catch it) or if there is some kind of error and the signal is never emitted. This context manager used here is inspired by code from a blob by John Reaver, of which an archive can be found here: https://web.archive.org/web/20160126155634/http://jdreaver.com/ posts/2014-07-03-waiting-for-signals-pyside-pyqt.html """ loop = QEventLoop() # When the signal is caught, the loop will break signal.connect(loop.quit) # The content in the context manager will now be executed # The timeout doesn't start until this block is finished, so make sure # there is no blocking calls in the with block. yield if timeout is not None: # Not False as possible 0ms timeout would be False QTimer.singleShot(timeout, loop.quit) loop.exec_()
def run(self, installSignalHandlers=True): if self._ownApp: self._blockApp = self.qApp else: self._blockApp = QEventLoop() self.runReturn() self._blockApp.exec_()
def play(file_name): audio.setCurrentSource(Phonon.MediaSource(file_name)) audio.play() loop = QEventLoop() audio.finished.connect(loop.quit) loop.exec_()
def main(): app = QApplication([]) webview = QWebView() loop = QEventLoop() # 创建QEventLoop对象,该对象用于创建本地时间循环 webview.loadFinished.connect(loop.quit) # QWebView对象的loadFinished回调连接了QEventLoop的quit方法,从而可以在网页加载完成之后停止事件循环 webview.load(QUrl('http://example.webscraping.com/places/default/search')) loop.exec_() # QwebView的加载方法是异步的,执行过程会在网页加载时立即传入下一行, # 但我们希望等待网页加载完成,因此需要在事件循环启动时调用loop.exec_() webview.show() # 调用QWebViewGUI的show()方法来显示渲染窗口,以方便调试 frame = webview.page().mainFrame() # 创建一个指代框架的变量 # 使用CSS模式在框架中定位元素,然后设置搜索参数 frame.findFirstElement('#search_term').setAttribute('Value', '.') frame.findFirstElement('#page_size option:checked').setPlainText('1000') # 使用evaluate JavaScript()方法提交事件,模拟点击事件 frame.findFirstElement('#search').evaluateJavaScript('this.click()') app.exec_() # 进入应用的事件循环,可以对表单操作进行复查,如果没有使用该方法,脚本就会直接结束 # 等待结果,三种方法处理: # 1、等待一定时间,期望AJAX事件能够在此时刻之前完成 # 2、重写Qt的网络管理器,跟踪URL请求完成的事件 # 3、轮询网页,等待特定内容出现,下面采用第三种方法 elements = None while not elements: app.processEvents() # 用于给Qt事件循环执行任务的事件,比如响应点击事件和更新GUI elements = frame.findAllElements('#results a') countries = [e.toPlainText().strip() for e in elements] print countries
def wait(self, element, max_number_attempts=30, interval=1000): """Функция завершается, когда element будет найден в вебдокументе и на это у нее есть max_number_attempts попыток. :param element: css путь поиск элемента :param max_number_attempts: максимальное количество попыток :param interval: интервал проверки :param wait_check_func: функция поиска элемента, ожидание прекращается, когда эта функция вернет True. Функция принимает 2 параметра QWebElement и css-путь к элементу, который проверяем. """ logger.debug('Ищу элемент: %s. Количество попыток: %s. Интервал: %s.', element, max_number_attempts, interval) self._element = element self._found = False self._attempts_remaining = max_number_attempts # # Если фукнция определена, используем пользовательскую, а не стандартную # if wait_check_func is not None: # self._wait_check_func = wait_check_func self._timer.start(interval) if not self._found: loop = QEventLoop() self._finished_event_loop.connect(loop.quit) loop.exec_()
def run(self, installSignalHandlers=True): _assert(in_main_thread()) if self._ownApp: self._blockApp = self.qApp else: self._blockApp = QEventLoop() self.runReturn() self._blockApp.exec_()
def open_toplevel(widget_type): widget = widget_type() widget.show() loop = QEventLoop() widget.destroyed.connect(loop.quit) loop.exec_()
def launch_list(self): """ Starts the execution of the queue in consecutive order, waiting for previous process to finish """ for task in self.tasks_pool: loop = QEventLoop() self.task_done.connect(loop.quit) self.__execute_task(task) loop.exec_()
def join(self): """Join thread. This method is provided for consistency with CLI executor, but it should not be of much use in GUI context. """ loop = QEventLoop() self.thread.finished.connect(loop.quit) # pylint: disable=no-member if not self.thread.isFinished(): loop.exec_(flags=QEventLoop.ExcludeUserInputEvents)
def load(self, url): logger.debug('Начата загрузка url: %s.', url) super().load(url) # Ждем пока прогрузится страница loop = QEventLoop() self.loadFinished.connect(loop.quit) loop.exec_() logger.debug('Загрузка url завершена.')
def testBlockingSignal(self): app = QCoreApplication.instance() or QCoreApplication([]) eventloop = QEventLoop() emitter = Emitter() receiver = Receiver(eventloop) emitter.connect(emitter, SIGNAL("signal(int)"), receiver.receive, Qt.BlockingQueuedConnection) emitter.start() retval = eventloop.exec_() emitter.wait() self.assertEqual(retval, 0)
def downlaod(self, url, timeout=60): loop = QEventLoop() timer = QTimer() timer.setSingleShot(True) timer.timeout.connect(loop.quit) self.loadFinished.connect(loop.quit) self.load(QUrl(url)) timer.start(timeout * 1000) loop.exec_() if timer.isActive(): timer.stop() return self.html() else: print 'Request timed out: ' + url
def _check_enemy(self): """Функция ищет противника на текущей странице и проверяет его тип и уровень. Возвращает True если нашелся подходящий противник, иначе False. """ self._timer_enemy_load.start() loop = QEventLoop() self._enemy_load_finished.connect(loop.quit) loop.exec_() enemy = self._mw.doc.findFirst('.fighter2') # Определим тип противника -- нам нужен горожанин (нпс) is_npc = enemy.findFirst('.npc') is_npc = not is_npc.isNull() # Узнаем уровень противника level = enemy.findFirst('.level') level = level.toPlainText() level = level.replace('[', '').replace(']', '') level = int(level) # Гиперссылка на профиль противника a = enemy.findFirst('a') # Имя противника name = a.toPlainText() # Адрес противника url = urljoin(self._mw.moswar_url, a.attribute('href')) my_level = self._mw.level() # TODO: добавить ограничение на количество попыток найти гражданина, перед тем как напасть на игрока # Проверяем, что уровень противника находится в пределе диапазона check_level = my_level - self.min_diff_levels <= level <= my_level + self.max_diff_levels found = is_npc and check_level if found: self.enemy_name = name self.enemy_level = level self.enemy_url = url return found
def open(self, url, timeout=60): """Wait for download to complete and return result""" loop = QEventLoop() timer = QTimer() timer.setSingleShot(True) timer.timeout.connect(loop.quit) self.loadFinished.connect(loop.quit) self.load(QUrl(url)) timer.start(timeout * 1000) loop.exec_() # delay here until download finished if timer.isActive(): # downloaded successfully timer.stop() return self.html() else: # timed out print 'Request timed out:', url
def main(): app = QApplication([]) webview = QWebView() loop = QEventLoop() webview.loadFinished.connect(loop.quit) webview.load(QUrl('http://example.webscraping.com/search')) loop.exec_() webview.show() frame = webview.page().mainFrame() frame.findFirstElement('#search_term').setAttribute('value', '.') frame.findFirstElement('#page_size option:checked').setPlainText('1000') frame.findFirstElement('#search').evaluateJavaScript('this.click()') elements = None while not elements: app.processEvents()
def open(self, url, timeout=60): """wait for download to complete and return result""" loop = QEventLoop() timer = QTimer() timer.setSingleShot(True) # True表示触发定时器后,仅执行事件一次 timer.timeout.connect(loop.quit) # 若超时,则连接loop.quit,退出事件循环 self.loadFinished.connect(loop.quit) self.load(url) timer.start(timeout * 1000) # 定时器以ms为单位,设置超时时间为60s loop.exec_() # 等待网页加载完成后,在执行后面的代码 if timer.isActive(): # downloaded successfully timer.stop() return self.html() else: # timed out print 'Request timed out:', url
def main(): app = QApplication([]) webview = QWebView() loop = QEventLoop() webview.loadFinished.connect(loop.quit) webview.load(QUrl('http://example.webscraping.com/places/default/search')) loop.exec_() webview.show() frame = webview.page().mainFrame() frame.findFirstElement('#search_term').setAttribute('value', '.') frame.findFirstElement('#page_size option').setPlainText('1000') #设置纯文本 frame.findFirstElement('#search').evaluateJavaScript('this.click()') app.exec_() elements = None while not elements: app.processEvents() elements = frame.findAllElements('#results a') countries = [e.toPlainText().strip() for e in elements] print countries
def main(): app = QApplication([]) webview = QWebView() loop = QEventLoop() webview.loadFinished.connect(loop.quit) webview.load(QUrl('http://example.webscraping.com/search')) loop.exec_() webview.show() frame = webview.page().mainFrame() frame.findFirstElement('#search_term').setAttribute('value', '.') frame.findFirstElement('#page_size option:checked').setPlainText('1000') frame.findFirstElement('#search').evaluateJavaScript('this.click()') elements = None while not elements: app.processEvents() elements = frame.findAllElements('#results a') countries = [e.toPlainText().strip() for e in elements] print countries
class BrowserRender(QWebView): def __init__(self, display=True): self.app = QApplication([]) self.view = QWebView() self.loop = QEventLoop() def open(self, url, timeout=60): self.view.loadFinished.connect(self.loop.quit) self.view.load(QUrl(url)) self.loop.exec_() self.view.show() def html(self): return self.view.page().mainFrame().toHtml() def find(self, pattern): return self.view.page().mainFrame().findAllElements(pattern) def attr(self, pattern, name, value): for e in self.find(pattern): e.setAttribute(name, value) def text(self, pattern, value): for e in self.find(pattern): e.setPlainText(value) def click(self, pattern): for e in self.find(pattern): e.evaluateJavaScript('this.click()') def wait_load(self, pattern, timeout=60): deadline = time.time() + timeout while time.time() < deadline: self.app.processEvents() matches = self.find(pattern) if matches: return matches print 'Wait load time out' def exec_(self): self.app.exec_()
def run(self, url): """Функция выполняет парсинг страницы объявления""" self.url = url self.phone = None self.t = time.clock() # Загружаем страницу объявления self.web_page.mainFrame().load(url) logger.debug('Начало выполнения загрузки "{}" {:.3f} секунд'.format(url, time.clock() - self.t)) # Ждем тут, пока закончится парсинг объявления -- все из-за ассинхронности webpage и моей лени -- хочется # просто в цикле запустить обработку и url, но из-за асинхронности с сигналами это не сработает -- какая-то # лажа происходит -- падает приложение с ошибкой где-то в QtCore.dll loop = QEventLoop() self.parse_phone_finished.connect(loop.quit) loop.exec_() logger.debug('Время выполнения парсера {:.3f} секунд'.format(time.clock() - self.t)) self.t = None
def main(): ''' 首先设置搜索参数和模拟动作事件,获取在此参数和动作下搜索后得到的网页 然后在这网页下,获取数据 ''' app = QApplication([]) webview = QWebView() loop = QEventLoop() webview.loadFinished.connect(loop.quit) webview.load(QUrl('http://example.webscraping.com/places/default/search')) loop.exec_() webview.show() ## 显示渲染窗口,,可以直接在这个窗口里面输入参数,执行动作,方便调试 frame = webview.page().mainFrame() ## 设置搜索参数 # frame.findAllElements('#search_term') ##寻找所有的search_term框,返回的是列表 # frame.findAllElements('#page_size option:checked') # ## 表单使用evaluateJavaScript()方法进行提交,模拟点击事件 # frame.findAllElements('#search') frame.findFirstElement('#search_term').setAttribute('value', '.') ##第一个search_term框 frame.findFirstElement('#page_size option:checked').setPlainText( '1000') ##第一个page_size框 ## 表单使用evaluateJavaScript()方法进行提交,模拟点击事件 frame.findFirstElement('#search').evaluateJavaScript( 'this.click()') ##第一个点击框 ## 轮询网页,等待特定内容出现 ## 下面不断循环,直到国家链接出现在results这个div元素中,每次循环都会调用app.processEvents() ##用于给QT事件执行任务的时间,比如响应事件和更新GUI elements = None while not elements: app.processEvents() elements = frame.findAllElements('#results a') ##查找下载网页内的所有a标签 countries = [e.toPlainText().strip() for e in elements] ##取出所有a标签内的文本内容 print countries
def true_init(self, project_folder, lossy_factor, color_map=None): self.project_folder = project_folder self.lossy_factor = lossy_factor # Use the first color map present in the project folder if not color_map: try: self.color_map = self.files_in_folder(self.project_folder, 'act')[0] except IndexError: error_message = 'Please put one color_palette.act in the folder' logger.warning(error_message) error_box = QMessageBox() # error_box.setStyleSheet(self.styleSheet()) error_box.setWindowTitle('File error') error_box.setText('There is .act file missing') error_box.setInformativeText(error_message) error_box.exec_() return else: self.color_map = color_map self.loop = QEventLoop() self.conversion1_done.connect(self.loop.quit) self.conversion1_done.connect(self.gifs2lossy) self.conversion2_done.connect(self.gifs2damaged) self.conversion3_done.connect(self.handbrake) self.conversion4_done.connect(self.cleanup) if __name__ == '__main__': self.conversion5_done.connect( lambda: QTimer.singleShot(1000, qapp.quit)) self.conversion1_done.connect(lambda: print('c1done')) self.conversion2_done.connect(lambda: print('c2done')) self.conversion3_done.connect(lambda: print('c3done')) self.conversion4_done.connect(lambda: print('c4done')) self.conversion5_done.connect(lambda: print('c5done')) self.avis2gif() self.loop.exec_()
def exec_(self): QEventLoop.exec_(self)
def __init__(self): QEventLoop.__init__(self)
def run(self): """Игра в наперстки.""" try: # Если в текущий бот в текущий момент занят и это не игра в Наперстки if self._mw._used and 'thimble' not in self._mw.current_url(): logger.warn('Бот в данный момент занят процессом "%s". Выхожу из функции.', self._mw._used_process) return self._mw._used_process = "Игра в Наперстки" logger.debug('Выполняю задание "%s".', self._mw._used_process) self._mw.metro() if 'metro' in self._mw.current_url(): # TODO: временное решение проблемы с закончившимися билетами, лучше через окно сделать holders = self._mw.doc.findFirst('.metro-thimble .holders') holders = holders.toPlainText().replace('Встреч с Моней на сегодня: ', '') if int(holders) == 0: logger.warn('Закончились билеты для игры в Наперстки.') # TODO: добавить is_ready # TODO: is_ready указывает на следующий день return self._mw._used = True t = time.clock() if 'thimble' in self._mw.current_url(): logger.info('Игра в Наперстки уже была начала, продолжу играть.') else: # Эмулируем клик на кнопку "Начать играть" self._mw.click_tag('.metro-thimble .button .c') # # TODO: не работает, т.к. окно не сразу появляется # for el in self._mw.doc.findAll('.alert'): # text = el.findFirst('#alert-text') # # print(el, text.toPlainText()) # if not text.isNull() and 'Вы сегодня уже играли в наперстки с Моней Шацом' in text.toPlainText(): # # TODO: добавить is_ready # # TODO: is_ready указывает на следующий день # logger.warn('Закончились билеты для игры в наперстки.') # self._mw._used = False # return self._ruda_count = 0 self._thimble_round_count = 0 self._timer_round_thimble.start() # Ждем пока закончится игра loop = QEventLoop() self.finished_thimble_game.connect(loop.quit) loop.exec_() logger.debug('Длительность игры {:.1f} секунд'.format(time.clock() - t)) logger.debug('Игра в наперстки закончилась за {} раундов'.format(self._thimble_round_count)) logger.debug('Угадано {} руды. Потрачено {} тугриков.'.format(self._ruda_count, self._thimble_round_count * 1500)) logger.debug('Удача {:.2f}%'.format(self._ruda_count / (self._thimble_round_count * 3) * 100)) # Эмулируем клик на кнопку "Я наигрался, хватит" self._mw.go('thimble/leave') except MoswarClosedError: raise except Exception as e: raise MoswarBotError(e) finally: self._mw._used = False
class QtReactor(posixbase.PosixReactorBase): implements(IReactorFDSet) def __init__(self): self._reads = {} self._writes = {} self._notifiers = {} self._timer = QTimer() self._timer.setSingleShot(True) QObject.connect(self._timer, SIGNAL("timeout()"), self.iterate) if QCoreApplication.instance() is None: # Application Object has not been started yet self.qApp=QCoreApplication([]) self._ownApp=True else: self.qApp = QCoreApplication.instance() self._ownApp=False self._blockApp = None posixbase.PosixReactorBase.__init__(self) def _add(self, xer, primary, type): """ Private method for adding a descriptor from the event loop. It takes care of adding it if new or modifying it if already added for another state (read -> read/write for example). """ if xer not in primary: primary[xer] = TwistedSocketNotifier(None, self, xer, type) def addReader(self, reader): """ Add a FileDescriptor for notification of data available to read. """ self._add(reader, self._reads, QSocketNotifier.Read) def addWriter(self, writer): """ Add a FileDescriptor for notification of data available to write. """ self._add(writer, self._writes, QSocketNotifier.Write) def _remove(self, xer, primary): """ Private method for removing a descriptor from the event loop. It does the inverse job of _add, and also add a check in case of the fd has gone away. """ if xer in primary: notifier = primary.pop(xer) notifier.shutdown() def removeReader(self, reader): """ Remove a Selectable for notification of data available to read. """ self._remove(reader, self._reads) def removeWriter(self, writer): """ Remove a Selectable for notification of data available to write. """ self._remove(writer, self._writes) def removeAll(self): """ Remove all selectables, and return a list of them. """ rv = self._removeAll(self._reads, self._writes) return rv def getReaders(self): return self._reads.keys() def getWriters(self): return self._writes.keys() def callLater(self,howlong, *args, **kargs): rval = super(QtReactor,self).callLater(howlong, *args, **kargs) self.reactorInvocation() return rval def reactorInvocation(self): self._timer.stop() self._timer.setInterval(0) self._timer.start() def _iterate(self, delay=None, fromqt=False): """See twisted.internet.interfaces.IReactorCore.iterate. """ self.runUntilCurrent() self.doIteration(delay, fromqt) iterate = _iterate def doIteration(self, delay=None, fromqt=False): 'This method is called by a Qt timer or by network activity on a file descriptor' if not self.running and self._blockApp: self._blockApp.quit() self._timer.stop() delay = max(delay, 1) if not fromqt: self.qApp.processEvents(QEventLoop.AllEvents, delay * 1000) if self.timeout() is None: timeout = 0.1 elif self.timeout() == 0: timeout = 0 else: timeout = self.timeout() self._timer.setInterval(timeout * 1000) self._timer.start() def runReturn(self, installSignalHandlers=True): self.startRunning(installSignalHandlers=installSignalHandlers) self.reactorInvocation() def run(self, installSignalHandlers=True): if self._ownApp: self._blockApp = self.qApp else: self._blockApp = QEventLoop() self.runReturn() self._blockApp.exec_()
def go(self): self.load('https://ru.stackoverflow.com/users/login') self.doc.findFirst('#email').setAttribute("value", self.login) self.doc.findFirst('#password').setAttribute("value", self.password) self.doc.findFirst('#submit-button').evaluateJavaScript('this.click()') # После клика на submit-button ждем пока прогрузится страница loop = QEventLoop() self.loadFinished.connect(loop.quit) loop.exec_() self.load(self.question_url) # Проверим, что наш тег не списке тегов tags = self.all_tags() logger.debug('Все тэги: %s.', tags) if self.our_tag in tags: logger.debug('Наш тэг "%s" уже есть в списке тэгов.', self.our_tag) return # TODO: Минимальная длина заголовка 15 символов. # TODO: Отсутствует тело сообщения. # TODO: проверяка на то, что править можно # Кликаем на "править" href = self.doc.findFirst('.question .suggest-edit-post').attribute('href') href = urljoin(self.url().toString(), href) logger.debug('Ссылка на страницу редактирования вопроса: %s.', href) self.load(href) tags = self.all_tags() logger.debug('Все тэги: %s.', tags) # Уже было замечен баг (http://meta.ru.stackoverflow.com/questions/2736), когда количество тегов # на странице вопроса и в редакторе отличалось, поэтому добавим еще проверку if self.our_tag in tags: logger.debug('Наш тэг "%s" уже есть в списке тэгов.', self.our_tag) return # Добавление нашего тега self.add_tag(self.our_tag) tags = self.all_tags() logger.debug('Все тэги: %s.', tags) # Паранойя: проверяем, что добавление тэга прошло удачно if self.our_tag not in tags: logger.warn('Нашего тэга "%s" нет в списке тэгов, так не должно быть.', self.our_tag) return # Добавление описания изменений logger.debug('Добавление описания изменений.') js = '$("#edit-comment")[0].value = "{}";'.format('Добавлена метка: {}.'.format(self.our_tag)) self.evaluate_java_script(js) quote_tags = '+'.join([quote(tag) for tag in self.all_tags()]) # TODO: не сработало # Имитация последствия ввода тега logger.debug('Имитация последствия ввода тега.') js = """ $.ajax({{ 'type': 'GET', 'url': 'api/tags/langdiv?tags={}&_={}' }}); """.format(quote_tags, int(datetime.now().timestamp() * 1000)) self.evaluate_java_script(js) # Ждем немного пока выполняется ajax-запрос loop = QEventLoop() QTimer.singleShot(3 * 1000, loop.quit) loop.exec_() logger.debug('Клик на кнопку "Сохранить изменения".') self.doc.findFirst('#submit-button').evaluateJavaScript('this.click()') # После клика на submit-button ждем пока прогрузится страница loop = QEventLoop() self.loadFinished.connect(loop.quit) loop.exec_()
def __init__(self, display=True): self.app = QApplication([]) self.view = QWebView() self.loop = QEventLoop()
def run(self): """Функция для нападения на игроков. Ищем слабого горожанина (заброшенного персонажа) -- не нужно привлекать внимание к боту. Уровень противника в пределах нашего +/- 1 """ try: if self._mw._used: logger.warn('Бот в данный момент занят процессом "%s". Выхожу из функции.', self._mw._used_process) return self._mw._used_process = "Нападение на игроков" logger.debug('Выполняю задание "%s".', self._mw._used_process) self._mw.alley() # TODO: оптимиизровать использование сникерсов -- если они есть, сразу использовать и нападать и так, # пока не будут потрачены все if not self.is_ready(): logger.debug('Нападать еще нельзя.') return self._mw._used = True # TODO: если есть тонус, использовать, чтобы сразу напасть # TODO: флаг на разрешение использования тонуса, чтобы сразу напасть # self.use_tonus() # Если не получилось съесть Сникерс, восстанавливаем по старинке if not self.eat_snickers(): if self._mw.current_hp() < self._mw.max_hp(): self._mw.restore_hp.run() logger.debug('Нажимаю на кнопку "Отнять у слабого".') # TODO: в одном из запусков дальше этой строки, похоже дело не пошло, возможно, страница с кнопкой # не прогрузилась # Кликаем на кнопку "Отнять у слабого" self._mw.click_tag(self._css_path_button_fight) # Если не нашли подходящего противника, смотрим следующего if not self._check_enemy(): self._timer_next_enemy.start() # Ожидаем пока противник не будет найден loop = QEventLoop() self._enemy_found.connect(loop.quit) loop.exec_() logger.debug('Нападаем на "%s" [%s]: %s.', self.enemy_name, self.enemy_level, self.enemy_url) # Кликаем на кнопку "Напасть" self._mw.click_tag('.button-fight a') # Перемотка битвы forward = '#controls-forward' # Ждем пока после клика прогрузится страница и появится элемент Waitable(self._mw).wait(forward) # Перематываем бой self._mw.click_tag(forward) # Обрабатываем результаты боя self.handle_results() except MoswarClosedError: raise except Exception as e: raise MoswarBotError(e) finally: self._mw._used = False
class Conversion(QObject): conversion1 = Signal(int, int) conversion2 = Signal(int, int) conversion3 = Signal(int, int) conversion4 = Signal(int, int) conversion5 = Signal(int, int) conversion1_done = Signal() conversion2_done = Signal() conversion3_done = Signal() conversion4_done = Signal() conversion5_done = Signal() error = Signal(str) def __init__(self): super(Conversion, self).__init__() # self.true_init(project_folder, lossy_factor, color_map=None) def true_init(self, project_folder, lossy_factor, color_map=None): self.project_folder = project_folder self.lossy_factor = lossy_factor # Use the first color map present in the project folder if not color_map: try: self.color_map = self.files_in_folder(self.project_folder, 'act')[0] except IndexError: error_message = 'Please put one color_palette.act in the folder' logger.warning(error_message) error_box = QMessageBox() # error_box.setStyleSheet(self.styleSheet()) error_box.setWindowTitle('File error') error_box.setText('There is .act file missing') error_box.setInformativeText(error_message) error_box.exec_() return else: self.color_map = color_map self.loop = QEventLoop() self.conversion1_done.connect(self.loop.quit) self.conversion1_done.connect(self.gifs2lossy) self.conversion2_done.connect(self.gifs2damaged) self.conversion3_done.connect(self.handbrake) self.conversion4_done.connect(self.cleanup) if __name__ == '__main__': self.conversion5_done.connect( lambda: QTimer.singleShot(1000, qapp.quit)) self.conversion1_done.connect(lambda: print('c1done')) self.conversion2_done.connect(lambda: print('c2done')) self.conversion3_done.connect(lambda: print('c3done')) self.conversion4_done.connect(lambda: print('c4done')) self.conversion5_done.connect(lambda: print('c5done')) self.avis2gif() self.loop.exec_() def avis2gif(self): emoji_dict = { Emoji(emoji).filename: Emoji(emoji) for emoji in self.files_in_folder(self.project_folder) if Emoji(emoji) } for index, item in enumerate(emoji_dict.keys()): # print(emoji_dict[item]) if not emoji_dict[item].has_gif or settings.overwrite_gifs: print(emoji_dict[item].name, 'gif file missing, creating one') FFmpeg(emoji_dict[item]) self.conversion1.emit(index + 1, len(emoji_dict) - 1) QTimer.singleShot(1, self.conversion1_done) def gifs2lossy(self): emoji_dict = { Emoji(emoji).filename: Emoji(emoji) for emoji in self.files_in_folder(self.project_folder) if Emoji(emoji) } for index, item in enumerate(emoji_dict.keys()): if not emoji_dict[item].has_lossy or settings.overwrite_gifs: print(emoji_dict[item].name, 'lossy file missing, creating one') # Get the proper lossy value for the gifsicle if '136' in emoji_dict[item].resolution: lossy_factor = self.lossy_factor['136'] elif '280' in emoji_dict[item].resolution: lossy_factor = self.lossy_factor['280'] GifSicle(emoji_dict[item], lossy_factor, self.color_map, to_lossy=True) self.conversion2.emit(index + 1, len(emoji_dict) - 1) else: print( 'Lossy file for {} already exists, skipping lossy creation' .format(emoji_dict[item].name)) QTimer.singleShot(1, self.conversion2_done) def gifs2damaged(self): emoji_dict = { Emoji(emoji).filename: Emoji(emoji) for emoji in self.files_in_folder(self.project_folder) if Emoji(emoji) } for index, item in enumerate(emoji_dict.keys()): if not emoji_dict[item].has_damaged or settings.overwrite_gifs: print(emoji_dict[item].name, 'damaged file missing, creating') # Get the proper lossy value for the gifsicle if '136' in emoji_dict[item].resolution: lossy_factor = self.lossy_factor['136'] elif '280' in emoji_dict[item].resolution: lossy_factor = self.lossy_factor['280'] GifSicle(emoji_dict[item], lossy_factor, self.color_map, to_damaged=True) self.conversion3.emit(index + 1, len(emoji_dict) - 1) QTimer.singleShot(1, self.conversion3_done) def handbrake(self): emoji_list = [ Emoji(emoji) for emoji in self.files_in_folder(self.project_folder) if Emoji(emoji) ] Handbrake(emoji_list[0]) self.conversion4.emit(1, 1) QTimer.singleShot(1, self.conversion4_done) def cleanup(self): all_temps = [ temps for temps in self.files_in_folder(self.project_folder, 'tmp') ] for temp_file in all_temps: remove(temp_file) all_gifs = [ gifs for gifs in self.files_in_folder(self.project_folder, 'gif') ] for temp_gif in all_gifs: if "LOSSY" not in temp_gif: try: remove(temp_gif) except PermissionError as e: logging.warning(e) error_message = str(e) error_box = QtGui.QMessageBox() error_box.setStyleSheet(stylesheet.houdini) error_box.setWindowTitle('File error') error_box.setText(error_message) error_box.exec_() self.conversion5.emit(1, 1) QTimer.singleShot(1, self.conversion5_done) def files_in_folder(self, folder='input', ext='avi'): return [ path.join(path.abspath(folder), file) for file in listdir(folder) if '.' + str(ext) == path.splitext(file)[1] ]
class QtReactor(posixbase.PosixReactorBase): implements(IReactorFDSet) def __init__(self): self._reads = {} self._writes = {} self._notifiers = {} self._timer = QTimer() self._timer.setSingleShot(True) QObject.connect(self._timer, SIGNAL("timeout()"), self.iterate) if QCoreApplication.instance() is None: # Application Object has not been started yet self.qApp = QCoreApplication([]) self._ownApp = True else: self.qApp = QCoreApplication.instance() self._ownApp = False self._blockApp = None posixbase.PosixReactorBase.__init__(self) def _add(self, xer, primary, type): """ Private method for adding a descriptor from the event loop. It takes care of adding it if new or modifying it if already added for another state (read -> read/write for example). """ if xer not in primary: primary[xer] = TwistedSocketNotifier(None, self, xer, type) def addReader(self, reader): """ Add a FileDescriptor for notification of data available to read. """ self._add(reader, self._reads, QSocketNotifier.Read) def addWriter(self, writer): """ Add a FileDescriptor for notification of data available to write. """ self._add(writer, self._writes, QSocketNotifier.Write) def _remove(self, xer, primary): """ Private method for removing a descriptor from the event loop. It does the inverse job of _add, and also add a check in case of the fd has gone away. """ if xer in primary: notifier = primary.pop(xer) notifier.shutdown() def removeReader(self, reader): """ Remove a Selectable for notification of data available to read. """ self._remove(reader, self._reads) def removeWriter(self, writer): """ Remove a Selectable for notification of data available to write. """ self._remove(writer, self._writes) def removeAll(self): """ Remove all selectables, and return a list of them. """ rv = self._removeAll(self._reads, self._writes) return rv def getReaders(self): return self._reads.keys() def getWriters(self): return self._writes.keys() def callLater(self, howlong, *args, **kargs): rval = super(QtReactor, self).callLater(howlong, *args, **kargs) self.reactorInvocation() return rval def reactorInvocation(self): self._timer.stop() self._timer.setInterval(0) self._timer.start() def _iterate(self, delay=None, fromqt=False): """See twisted.internet.interfaces.IReactorCore.iterate. """ self.runUntilCurrent() self.doIteration(delay, fromqt) iterate = _iterate def doIteration(self, delay=None, fromqt=False): 'This method is called by a Qt timer or by network activity on a file descriptor' if not self.running and self._blockApp: self._blockApp.quit() self._timer.stop() delay = max(delay, 1) if not fromqt: self.qApp.processEvents(QEventLoop.AllEvents, delay * 1000) if self.timeout() is None: timeout = 0.1 elif self.timeout() == 0: timeout = 0 else: timeout = self.timeout() self._timer.setInterval(timeout * 1000) self._timer.start() def runReturn(self, installSignalHandlers=True): self.startRunning(installSignalHandlers=installSignalHandlers) self.reactorInvocation() def run(self, installSignalHandlers=True): if self._ownApp: self._blockApp = self.qApp else: self._blockApp = QEventLoop() self.runReturn() self._blockApp.exec_()
class QtReactor(posixbase.PosixReactorBase): implements(IReactorFDSet) def __init__(self): _assert(in_main_thread()) self._reads = {} self._writes = {} self._notifiers = {} self._timer = QTimer() self._timer.setSingleShot(True) QObject.connect(self._timer, SIGNAL("timeout()"), self.iterate) self.qApp = QCoreApplication.instance() self._ownApp=False if self.qApp is None: self.qApp=QCoreApplication([]) self._ownApp=True self._blockApp = None posixbase.PosixReactorBase.__init__(self) def _add(self, xer, primary, type): _assert(in_main_thread()) """ Private method for adding a descriptor from the event loop. It takes care of adding it if new or modifying it if already added for another state (read -> read/write for example). """ if xer not in primary: primary[xer] = TwistedSocketNotifier(None, self, xer, type) def addReader(self, reader): _assert(in_main_thread()) """ Add a FileDescriptor for notification of data available to read. """ self._add(reader, self._reads, QSocketNotifier.Read) def addWriter(self, writer): _assert(in_main_thread()) """ Add a FileDescriptor for notification of data available to write. """ self._add(writer, self._writes, QSocketNotifier.Write) def _remove(self, xer, primary): _assert(in_main_thread()) """ Private method for removing a descriptor from the event loop. It does the inverse job of _add, and also add a check in case of the fd has gone away. """ if xer in primary: notifier = primary.pop(xer) notifier.shutdown() def removeReader(self, reader): _assert(in_main_thread()) """ Remove a Selectable for notification of data available to read. """ self._remove(reader, self._reads) def removeWriter(self, writer): _assert(in_main_thread()) """ Remove a Selectable for notification of data available to write. """ self._remove(writer, self._writes) def removeAll(self): _assert(in_main_thread()) """ Remove all selectables, and return a list of them. """ rv = self._removeAll(self._reads, self._writes) return rv def getReaders(self): _assert(in_main_thread()) return self._reads.keys() def getWriters(self): _assert(in_main_thread()) return self._writes.keys() def callLater(self, howlong, *args, **kargs): try: _assert(in_main_thread(), (args, kargs)) except AssertionError: for arg in args: if isinstance(arg, partial): logger.debug('in callLater: %r', arg.func) elif isinstance(arg, LoopingCall): if isinstance(arg.f, partial): logger.debug('in callLater: %r', arg.f.func) else: logger.debug('in callLater: %r', arg.f) sys.exit() rval = super(QtReactor, self).callLater(howlong, *args, **kargs) self.reactorInvocation() return rval def reactorInvocation(self): _assert(in_main_thread()) self._timer.stop() self._timer.setInterval(0) self._timer.start() def _iterate(self, delay=None, fromqt=False): _assert(in_main_thread()) """ See twisted.internet.interfaces.IReactorCore.iterate. """ self.runUntilCurrent() self.doIteration(delay, fromqt) iterate = _iterate def doIteration(self, delay=None, fromqt=False): _assert(in_main_thread()) """ This method is called by a Qt timer or by network activity on a file descriptor. If called becuase of network activiy then control should not be handed back to Qt as this would cause recursion. """ if not self.running and self._blockApp: self._blockApp.quit() self._timer.stop() delay = max(delay, 1) if not fromqt: self.qApp.processEvents(QEventLoop.AllEvents, delay * 1000) if self.timeout() is None: timeout = 0.1 elif self.timeout() == 0: timeout = 0 else: timeout = self.timeout() self._timer.setInterval(timeout * 1000) self._timer.start() def runReturn(self, installSignalHandlers=True): _assert(in_main_thread()) self.startRunning(installSignalHandlers=installSignalHandlers) self.reactorInvocation() def crash(self): _assert(in_main_thread()) log.msg('crash') super(QtReactor, self).crash() if self._blockApp: self._blockApp.quit() def run(self, installSignalHandlers=True): _assert(in_main_thread()) if self._ownApp: self._blockApp = self.qApp else: self._blockApp = QEventLoop() self.runReturn() self._blockApp.exec_()
def __init__(self, parent=None): ''' sets up the whole main window ''' #standard init super(MainWindow, self).__init__(parent) #set the window title self.setWindowTitle('Open Ephys Control GUI') self.window_height = 700 self.window_width = 1100 self.screen2 = QDesktopWidget().screenGeometry(0) self.move( self.screen2.left() + (self.screen2.width() - self.window_width) / 2., self.screen2.top() + (self.screen2.height() - self.window_height) / 2.) self.get_info() self.noinfo = True while self.noinfo: loop = QEventLoop() QTimer.singleShot(500., loop.quit) loop.exec_() subprocess.Popen('start %s' % open_ephys_path, shell=True) self.collect_frame.connect(self.update_frame) self.collect_threshed.connect(self.update_threshed) self.acquiring = False self.recording = False self.video_height = self.window_height * .52 self.video_width = self.window_width * .48 self.resize(self.window_width, self.window_height) #create QTextEdit window 'terminal' for receiving stdout and stderr self.terminal = QTextEdit(self) #set the geometry self.terminal.setGeometry( QRect(self.window_width * .02, self.window_height * .15 + self.video_height, self.video_width * .96, 150)) #make widgets self.setup_video_frames() self.setup_thresh_buttons() self.overlay = True #create thread and worker for video processing self.videoThread = QThread(self) self.videoThread.start() self.videoproc_worker = VideoWorker(self) self.videoproc_worker.moveToThread(self.videoThread) self.vt_file = None """""" """""" """""" """""" """""" """""" """""" """ set up menus """ """""" """""" """""" """""" """""" """""" """""" #create a QMenuBar and set geometry self.menubar = QMenuBar(self) self.menubar.setGeometry( QRect(0, 0, self.window_width * .5, self.window_height * .03)) #set the QMenuBar as menu bar for main window self.setMenuBar(self.menubar) #create a QStatusBar statusbar = QStatusBar(self) #set it as status bar for main window self.setStatusBar(statusbar) #create icon toolbar with default image iconToolBar = self.addToolBar("iconBar.png") #create a QAction for the acquire button self.action_Acq = QAction(self) #make it checkable self.action_Acq.setCheckable(True) #grab an icon for the button acq_icon = self.style().standardIcon(QStyle.SP_MediaPlay) #set the icon for the action self.action_Acq.setIcon(acq_icon) #when the button is pressed, call the Acquire function self.action_Acq.triggered.connect(self.Acquire) #create a QAction for the record button self.action_Record = QAction(self) #make it checkable self.action_Record.setCheckable(True) #grab an icon for the button record_icon = self.style().standardIcon(QStyle.SP_DialogYesButton) #set the icon for the action self.action_Record.setIcon(record_icon) #when the button is pressed, call advanced_settings function self.action_Record.triggered.connect(self.Record) #create QAction for stop button action_Stop = QAction(self) #grab close icon stop_icon = self.style().standardIcon(QStyle.SP_MediaStop) #set icon for action action_Stop.setIcon(stop_icon) #when button pressed, close window action_Stop.triggered.connect(self.Stop) #show tips for each action in the status bar self.action_Acq.setStatusTip("Start acquiring") self.action_Record.setStatusTip("Start recording") action_Stop.setStatusTip("Stop acquiring/recording") #add actions to icon toolbar iconToolBar.addAction(self.action_Acq) iconToolBar.addAction(self.action_Record) iconToolBar.addAction(action_Stop) # self.sort_button = QPushButton('Sort Now',self) # self.sort_button.setGeometry(QRect(self.window_width*.85,0,self.window_width*.15,self.window_height*.05)) # self.sort_button.clicked.connect(self.sort_now) #show the window if minimized by windows self.showMinimized() self.showNormal() """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" "" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" ""
# print(json.loads(html)) # parser = etree.XMLParser(recover=True) # tree = etree.fromstring(html,parser) # print(tree.cssselect('div#results a')) # html = get_results(url, headers=headers) # parser = etree.XMLParser(recover=True) # tree = etree.fromstring(html,parser) # print(tree.cssselect('#result')[0].text) #初始化一个QApplication对象, app = QApplication([]) #创建一个QWebView对象,用于web文档的容器 webview = QWebView() # 创建一个QEventLoop对象,用于创建本地时间循环 loop = QEventLoop() # loadFinished回调连接了QEventLoop的quit方法,可以在网页加载完成之后停止事件循环 webview.loadFinished.connect(loop.quit) #将要加载的url传给QWebView,PyQt将该url的字符串封装在QUrl对象中 webview.load(QUrl(url)) # 等待网页加载完成,在事件循环启动时调用loop.exec_ loop.exec_() # 网页加载完成后退出事件循环 # html = webview.page().mainFrame().toHtml() # # 对加载的网页产生的HTMl进行数据抽取 # parser = etree.XMLParser(recover=True) # tree = etree.fromstring(html,parser) # print(tree.cssselect('#result')[0].text)
def start(self): QThread.start(self) loop=QEventLoop() self._init.connect(loop.quit) loop.exec_()