Skip to content

Commit

Permalink
Merge pull request #484 from kmyk/issue/464
Browse files Browse the repository at this point in the history
#464: breaking changes
  • Loading branch information
kmyk authored Aug 21, 2019
2 parents e5d78d3 + d00b893 commit 734f3de
Show file tree
Hide file tree
Showing 16 changed files with 585 additions and 442 deletions.
15 changes: 14 additions & 1 deletion onlinejudge/dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from typing import List, Optional, Type

import onlinejudge._implementation.logging as log
from onlinejudge.type import Problem, Service, Submission
from onlinejudge.type import Contest, Problem, Service, Submission

submissions = [] # type: List[Type['Submission']]

Expand Down Expand Up @@ -61,6 +61,19 @@ def problem_from_url(url: str) -> Optional[Problem]:
return None


contests = [] # type: List[Type['Contest']]


def contest_from_url(url: str) -> Optional[Contest]:
for cls in contests:
contest = cls.from_url(url)
if contest is not None:
log.status('contest recognized: %s: %s', str(contest), url)
return contest
log.failure('unknown contest: %s', url)
return None


services = [] # type: List[Type['Service']]


Expand Down
6 changes: 3 additions & 3 deletions onlinejudge/service/anarchygolf.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ def from_url(cls, url: str) -> Optional['AnarchyGolfService']:


class AnarchyGolfProblem(onlinejudge.type.Problem):
def __init__(self, problem_id: str):
def __init__(self, *, problem_id: str):
self.problem_id = problem_id

def download_sample_cases(self, session: Optional[requests.Session] = None) -> List[onlinejudge.type.TestCase]:
def download_sample_cases(self, *, session: Optional[requests.Session] = None) -> List[onlinejudge.type.TestCase]:
session = session or utils.get_default_session()
# get
resp = utils.request('GET', self.get_url(), session=session)
Expand Down Expand Up @@ -81,7 +81,7 @@ def from_url(cls, url: str) -> Optional['AnarchyGolfProblem']:
and result.netloc == 'golf.shinh.org' \
and utils.normpath(result.path) == '/p.rb' \
and result.query:
return cls(result.query)
return cls(problem_id=result.query)
return None


Expand Down
30 changes: 18 additions & 12 deletions onlinejudge/service/aoj.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ class AOJProblem(onlinejudge.type.Problem):
"""
:ivar problem_id: :py:class:`str` like `DSL_1_A` or `2256`
"""
def __init__(self, problem_id):
def __init__(self, *, problem_id):
self.problem_id = problem_id

def download_sample_cases(self, session: Optional[requests.Session] = None) -> List[TestCase]:
def download_sample_cases(self, *, session: Optional[requests.Session] = None) -> List[TestCase]:
session = session or utils.get_default_session()
# get samples via the official API
# reference: http://developers.u-aizu.ac.jp/api?key=judgedat%2Ftestcases%2Fsamples%2F%7BproblemId%7D_GET
Expand All @@ -62,7 +62,7 @@ def download_sample_cases(self, session: Optional[requests.Session] = None) -> L
)]
return samples

def download_system_cases(self, session: Optional[requests.Session] = None) -> List[TestCase]:
def download_system_cases(self, *, session: Optional[requests.Session] = None) -> List[TestCase]:
session = session or utils.get_default_session()

# get header
Expand Down Expand Up @@ -103,7 +103,7 @@ def from_url(cls, url: str) -> Optional['AOJProblem']:
and querystring.get('id') \
and len(querystring['id']) == 1:
n, = querystring['id']
return cls(n)
return cls(problem_id=n)

# example: https://onlinejudge.u-aizu.ac.jp/challenges/sources/JAG/Prelim/2881
# example: https://onlinejudge.u-aizu.ac.jp/courses/library/4/CGL/3/CGL_3_B
Expand All @@ -112,7 +112,7 @@ def from_url(cls, url: str) -> Optional['AOJProblem']:
and result.netloc == 'onlinejudge.u-aizu.ac.jp' \
and m:
n = m.group(5)
return cls(n)
return cls(problem_id=n)

return None

Expand All @@ -127,14 +127,14 @@ class AOJArenaProblem(onlinejudge.type.Problem):
.. versionadded:: 6.1.0
"""
def __init__(self, arena_id, alphabet):
def __init__(self, *, arena_id, alphabet):
assert alphabet in string.ascii_uppercase
self.arena_id = arena_id
self.alphabet = alphabet

self._problem_id = None # Optional[str]

def get_problem_id(self, session: Optional[requests.Session] = None) -> str:
def get_problem_id(self, *, session: Optional[requests.Session] = None) -> str:
"""
:note: use http://developers.u-aizu.ac.jp/api?key=judgeapi%2Farenas%2F%7BarenaId%7D%2Fproblems_GET
"""
Expand All @@ -151,12 +151,18 @@ def get_problem_id(self, session: Optional[requests.Session] = None) -> str:
break
return self._problem_id

def download_sample_cases(self, session: Optional[requests.Session] = None) -> List[TestCase]:
def download_sample_cases(self, *, session: Optional[requests.Session] = None) -> List[TestCase]:
log.warning("most of problems in arena have no registered sample cases.")
return AOJProblem(self.get_problem_id()).download_sample_cases(session=session)
return AOJProblem(problem_id=self.get_problem_id()).download_sample_cases(session=session)

def download_system_cases(self, session: Optional[requests.Session] = None) -> List[TestCase]:
return AOJProblem(self.get_problem_id()).download_system_cases(session=session)
def download_system_cases(self, *, session: Optional[requests.Session] = None) -> List[TestCase]:
return AOJProblem(problem_id=self.get_problem_id()).download_system_cases(session=session)

def download_content(self, *, session: Optional[requests.Session] = None):
"""
:raise NotImplementedError:
"""
raise NotImplementedError

def get_url(self) -> str:
return 'https://onlinejudge.u-aizu.ac.jp/services/room.html#{}/problems/{}'.format(self.arena_id, self.alphabet)
Expand All @@ -170,7 +176,7 @@ def from_url(cls, url: str) -> Optional['AOJArenaProblem']:
and utils.normpath(result.path) == '/services/room.html':
fragment = result.fragment.split('/')
if len(fragment) == 3 and fragment[1] == 'problems':
return cls(fragment[0], fragment[2].upper())
return cls(arena_id=fragment[0], alphabet=fragment[2].upper())
return None

def get_service(self) -> AOJService:
Expand Down
Loading

0 comments on commit 734f3de

Please sign in to comment.