From 28761062d7acb038db845786b4cae73ce4842814 Mon Sep 17 00:00:00 2001 From: fukatani Date: Sat, 19 Oct 2019 22:56:49 +0900 Subject: [PATCH 1/6] remove old comment. --- rujaion/webview_widget.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/rujaion/webview_widget.py b/rujaion/webview_widget.py index 9082c68..cabd813 100644 --- a/rujaion/webview_widget.py +++ b/rujaion/webview_widget.py @@ -140,10 +140,6 @@ def viewGraph(self) -> None: url = "https://hello-world-494ec.firebaseapp.com/?format=true&directed=false&weighted={0}&indexed={1}&data={2}".format( weighted, indexed, graph_query ) - # import webbrowser - # webbrowser.open_new_tab(url) - # self.load(url) - # self.page().runJavaScript("window.scrollTo(-500, -500);") try: subprocess.Popen(["sensible-browser", url]) except subprocess.TimeoutExpired: From d454ebf9eec2b069ea1a154c0c9fc9bdedd446f7 Mon Sep 17 00:00:00 2001 From: fukatani Date: Sun, 20 Oct 2019 00:03:35 +0900 Subject: [PATCH 2/6] reimplement next prev --- rujaion/webview_widget.py | 78 ++++++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 33 deletions(-) diff --git a/rujaion/webview_widget.py b/rujaion/webview_widget.py index cabd813..552158e 100644 --- a/rujaion/webview_widget.py +++ b/rujaion/webview_widget.py @@ -7,12 +7,13 @@ from PyQt5.QtCore import QUrl from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineScript -from onlinejudge.dispatch import service_from_url +from onlinejudge import dispatch from onlinejudge._implementation.utils import ( default_cookie_path, with_cookiejar, new_session_with_our_user_agent, ) +from PyQt5.QtCore import QThread from PyQt5.QtNetwork import QNetworkCookie # Many of source is copied from https://qiita.com/montblanc18/items/88d0b639de86b7cac613 @@ -154,7 +155,6 @@ def __init__(self, parent=None, *args): self.browser.setWindowTitle("Task") self.url_edit = QtWidgets.QLineEdit() self.url_edit.returnPressed.connect(self.loadPage) - self.browser.urlChanged.connect(self.updateCurrentUrl) session = new_session_with_our_user_agent() self.browser.page().profile().setHttpUserAgent(session.headers["User-Agent"]) self.browser.page().profile().cookieStore().cookieAdded.connect( @@ -167,12 +167,17 @@ def __init__(self, parent=None, *args): grid.addWidget(self.browser, 1, 0, 5, 15) self.setLayout(grid) self.resize(800, 800) + self.next_prev_updater = NextPreviousProblemUpdater() def download_task(self): self.url_edit.setText(self.browser.url().toString()) self.parent().parent().download(self.url_edit.text()) + self.next_prev_updater.url = self.browser.url().toString() + self.next_prev_updater.start(QThread.LowPriority) def loadPage(self): + self.next_prev_updater.next = None + self.next_prev_updater.prev = None if self.url_edit.text(): self.browser.load(QUrl(self.url_edit.text())) @@ -185,44 +190,18 @@ def goNextTask(self): ex. https://atcoder.jp/contests/cpsco2019-s2/tasks/cpsco2019_s2_e -> https://atcoder.jp/contests/cpsco2019-s2/tasks/cpsco2019_s2_f - - This function is note tested except AtCoder. """ - current_url = self.browser.url().toString().split("?") - if current_url[0].split("/")[-1].isdecimal(): # yukicoder - words = current_url[0].split("/") - words[-1] = str(int(words[-1]) + 1) - current_url[0] = "/".join(words) - else: - if current_url[0][-1] == "z": - return - current_url[0] = current_url[0][:-1] + chr(ord(current_url[0][-1]) + 1) - self.changePage("?".join(current_url)) + if self.next_prev_updater.next is not None: + self.changePage(self.next_prev_updater.next) def goPreviousTask(self): """ Go previous contest task. ex. https://atcoder.jp/contests/cpsco2019-s2/tasks/cpsco2019_s2_b -> https://atcoder.jp/contests/cpsco2019-s2/tasks/cpsco2019_s2_a - - This function is note tested except AtCoder. - """ - current_url = self.browser.url().toString().split("?") - if current_url.split("/")[-1].isdecimal(): # yukicoder - words = current_url[0].split("/") - words[-1] = str(int(words[-1]) - 1) - current_url[0] = "/".join(words) - else: - if current_url[0][-1] == "a": - return - current_url[0] = current_url[0][:-1] + chr(ord(current_url[0][-1]) + 1) - self.changePage("?".join(current_url)) - - def updateCurrentUrl(self): - """ Rewriting url_edit when you move different web page. """ - self.url_edit.clear() - self.url_edit.insert(self.browser.url().toString()) + if self.next_prev_updater.prev is not None: + self.changePage(self.next_prev_updater.prev) def focusOnUrlEdit(self): self.url_edit.setFocus() @@ -230,7 +209,7 @@ def focusOnUrlEdit(self): def handleCookieAdded(self, cookie: QNetworkCookie): url = self.browser.url().toString() - if service_from_url(url): + if dispatch.service_from_url(url): py_cookie = toPyCookie(cookie) with with_cookiejar( new_session_with_our_user_agent(), path=default_cookie_path @@ -238,6 +217,39 @@ def handleCookieAdded(self, cookie: QNetworkCookie): sess.cookies.set_cookie(py_cookie) +class NextPreviousProblemUpdater(QThread): + def __init__(self): + super().__init__() + self.url = "" + self.next = None + self.prev = None + + def run(self): + cur_problem = dispatch.problem_from_url(self.url) + if cur_problem is None: + return + if cur_problem.get_service().get_name() == "yukicoder": + words = self.url.split("?") + words = words[0].split("/") + prev_words = words.copy() + prev_words[-1] = str(int(words[-1]) - 1) + next_words = words.copy() + next_words[-1] = str(int(words[-1]) + 1) + self.prev = "/".join(prev_words) + self.next = "/".join(next_words) + return + try: + contest = cur_problem.get_contest() + problems = contest.list_problems() + for i, problem in enumerate(problems): + if problem == cur_problem: + self.prev = problems[i - 1].get_url() + self.next = problems[i + 1].get_url() + break + except NotImplementedError: + pass + + def toPyCookie(qt_cookie: QNetworkCookie) -> Cookie: port = None port_specified = False From 6c14945767a34b14938a3f55238a526e6d96ac40 Mon Sep 17 00:00:00 2001 From: fukatani Date: Sun, 20 Oct 2019 00:11:32 +0900 Subject: [PATCH 3/6] change order for speeding up. --- rujaion/webview_widget.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/rujaion/webview_widget.py b/rujaion/webview_widget.py index 552158e..94235ac 100644 --- a/rujaion/webview_widget.py +++ b/rujaion/webview_widget.py @@ -93,8 +93,10 @@ def __init__(self, *args, **kwargs): def contextMenuEvent(self, a0: QtGui.QContextMenuEvent) -> None: menu = QtWidgets.QMenu() - menu.addAction(u"Go Next Task", self.parent().goNextTask) - menu.addAction(u"Go Previous Task", self.parent().goPreviousTask) + if self.parent().next_prev_updater.next is not None: + menu.addAction(u"Go Next Task", self.parent().goNextTask) + if self.parent().next_prev_updater.prev is not None: + menu.addAction(u"Go Previous Task", self.parent().goPreviousTask) if self.selectedText(): menu.addAction(u"View Graph", self.viewGraph) menu.addAction(u"Back", self.back) @@ -170,10 +172,10 @@ def __init__(self, parent=None, *args): self.next_prev_updater = NextPreviousProblemUpdater() def download_task(self): + self.next_prev_updater.url = self.browser.url().toString() + self.next_prev_updater.start() self.url_edit.setText(self.browser.url().toString()) self.parent().parent().download(self.url_edit.text()) - self.next_prev_updater.url = self.browser.url().toString() - self.next_prev_updater.start(QThread.LowPriority) def loadPage(self): self.next_prev_updater.next = None From 1a6ad4ded950015d57cb261e8b1eff484aba250e Mon Sep 17 00:00:00 2001 From: fukatani Date: Sun, 20 Oct 2019 00:45:47 +0900 Subject: [PATCH 4/6] implement go each tasks. --- rujaion/webview_widget.py | 63 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 3 deletions(-) diff --git a/rujaion/webview_widget.py b/rujaion/webview_widget.py index 94235ac..e25b9a5 100644 --- a/rujaion/webview_widget.py +++ b/rujaion/webview_widget.py @@ -100,6 +100,57 @@ def contextMenuEvent(self, a0: QtGui.QContextMenuEvent) -> None: if self.selectedText(): menu.addAction(u"View Graph", self.viewGraph) menu.addAction(u"Back", self.back) + menu.addSeparator() + if self.parent().next_prev_updater.problems is not None: + problems = self.parent().next_prev_updater.problems + for i, problem_url in enumerate(problems): + problem_id = problem_url.split("/")[-1] + # I know this is not good code. + # But commented out code does not works + # menu.addAction( + # u"Go to {}".format(problem_id), + # lambda: self.parent().changePage(problem) + # ) + if i == 0: + menu.addAction( + u"Go to {}".format(problem_id), + lambda: self.parent().changePage(problems[0]) + ) + elif i == 1: + menu.addAction( + u"Go to {}".format(problem_id), + lambda: self.parent().changePage(problems[1]) + ) + elif i == 2: + menu.addAction( + u"Go to {}".format(problem_id), + lambda: self.parent().changePage(problems[2]) + ) + elif i == 3: + menu.addAction( + u"Go to {}".format(problem_id), + lambda: self.parent().changePage(problems[3]) + ) + elif i == 4: + menu.addAction( + u"Go to {}".format(problem_id), + lambda: self.parent().changePage(problems[4]) + ) + elif i == 5: + menu.addAction( + u"Go to {}".format(problem_id), + lambda: self.parent().changePage(problems[5]) + ) + elif i == 6: + menu.addAction( + u"Go to {}".format(problem_id), + lambda: self.parent().changePage(problems[6]) + ) + elif i == 7: + menu.addAction( + u"Go to {}".format(problem_id), + lambda: self.parent().changePage(problems[7]) + ) menu.exec(a0.globalPos()) def runScript(self) -> None: @@ -178,8 +229,7 @@ def download_task(self): self.parent().parent().download(self.url_edit.text()) def loadPage(self): - self.next_prev_updater.next = None - self.next_prev_updater.prev = None + self.next_prev_updater.reset() if self.url_edit.text(): self.browser.load(QUrl(self.url_edit.text())) @@ -225,6 +275,12 @@ def __init__(self): self.url = "" self.next = None self.prev = None + self.problems = None + + def reset(self): + self.next = None + self.prev = None + # self.problems = None def run(self): cur_problem = dispatch.problem_from_url(self.url) @@ -246,8 +302,9 @@ def run(self): for i, problem in enumerate(problems): if problem == cur_problem: self.prev = problems[i - 1].get_url() - self.next = problems[i + 1].get_url() + self.next = problems[(i + 1) % len(problems)].get_url() break + self.problems = [problem_url.get_url() for problem_url in problems] except NotImplementedError: pass From d06a72f6bc658fd038967b6505308b4d37d97529 Mon Sep 17 00:00:00 2001 From: fukatani Date: Sun, 20 Oct 2019 20:15:11 +0900 Subject: [PATCH 5/6] except all kind Exception. --- rujaion/webview_widget.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rujaion/webview_widget.py b/rujaion/webview_widget.py index e25b9a5..ebd000e 100644 --- a/rujaion/webview_widget.py +++ b/rujaion/webview_widget.py @@ -305,7 +305,7 @@ def run(self): self.next = problems[(i + 1) % len(problems)].get_url() break self.problems = [problem_url.get_url() for problem_url in problems] - except NotImplementedError: + except Exception: pass From 635bae656e0410007f4f6dc06dd40a48cff15c44 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sun, 20 Oct 2019 23:12:29 +0900 Subject: [PATCH 6/6] black --- rujaion/webview_widget.py | 16 ++++++++-------- setup.py | 8 ++------ 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/rujaion/webview_widget.py b/rujaion/webview_widget.py index ebd000e..81a2b9c 100644 --- a/rujaion/webview_widget.py +++ b/rujaion/webview_widget.py @@ -114,42 +114,42 @@ def contextMenuEvent(self, a0: QtGui.QContextMenuEvent) -> None: if i == 0: menu.addAction( u"Go to {}".format(problem_id), - lambda: self.parent().changePage(problems[0]) + lambda: self.parent().changePage(problems[0]), ) elif i == 1: menu.addAction( u"Go to {}".format(problem_id), - lambda: self.parent().changePage(problems[1]) + lambda: self.parent().changePage(problems[1]), ) elif i == 2: menu.addAction( u"Go to {}".format(problem_id), - lambda: self.parent().changePage(problems[2]) + lambda: self.parent().changePage(problems[2]), ) elif i == 3: menu.addAction( u"Go to {}".format(problem_id), - lambda: self.parent().changePage(problems[3]) + lambda: self.parent().changePage(problems[3]), ) elif i == 4: menu.addAction( u"Go to {}".format(problem_id), - lambda: self.parent().changePage(problems[4]) + lambda: self.parent().changePage(problems[4]), ) elif i == 5: menu.addAction( u"Go to {}".format(problem_id), - lambda: self.parent().changePage(problems[5]) + lambda: self.parent().changePage(problems[5]), ) elif i == 6: menu.addAction( u"Go to {}".format(problem_id), - lambda: self.parent().changePage(problems[6]) + lambda: self.parent().changePage(problems[6]), ) elif i == 7: menu.addAction( u"Go to {}".format(problem_id), - lambda: self.parent().changePage(problems[7]) + lambda: self.parent().changePage(problems[7]), ) menu.exec(a0.globalPos()) diff --git a/setup.py b/setup.py index 95e787d..ecd42fa 100644 --- a/setup.py +++ b/setup.py @@ -25,10 +25,6 @@ def load_module(module_path): "pandas", ], packages=find_packages(exclude=("docker", "doc")), - package_data={ 'rujaion' : ['resources/*'], }, - entry_points={ - 'console_scripts': [ - 'rujaion = rujaion.rujaion_main:main', - ], - }, + package_data={"rujaion": ["resources/*"]}, + entry_points={"console_scripts": ["rujaion = rujaion.rujaion_main:main"]}, )