Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show a hint message when cookie.jar is broken #852

Merged
merged 1 commit into from
Feb 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions onlinejudge_command/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import contextlib
import datetime
import functools
import http.cookiejar
import os
import pathlib
import platform
Expand All @@ -24,6 +25,12 @@

logger = getLogger(__name__)

# These strings can control logging output.
NO_HEADER = 'NO_HEADER: '
HINT = 'HINT: '
SUCCESS = 'SUCCESS: '
FAILURE = 'FAILURE: '

user_data_dir = utils.user_data_dir
user_cache_dir = utils.user_cache_dir
default_cookie_path = utils.default_cookie_path
Expand All @@ -34,8 +41,12 @@ def new_session_with_our_user_agent(*, path: pathlib.Path) -> Iterator[requests.
session = requests.Session()
session.headers['User-Agent'] = '{}/{} (+{})'.format(version.__package_name__, version.__version__, version.__url__)
logger.debug('User-Agent: %s', session.headers['User-Agent'])
with utils.with_cookiejar(session, path=path) as session:
yield session
try:
with utils.with_cookiejar(session, path=path) as session:
yield session
except http.cookiejar.LoadError:
logger.info(HINT + 'You can delete the broken cookie.jar file: %s', str(path))
raise


def textfile(s: str) -> str: # should have trailing newline
Expand Down Expand Up @@ -107,13 +118,6 @@ def exec_command(command_str: str, *, stdin: Optional[BinaryIO] = None, input: O
return info, proc


# These strings can control logging output.
NO_HEADER = 'NO_HEADER: '
HINT = 'HINT: '
SUCCESS = 'SUCCESS: '
FAILURE = 'FAILURE: '


def green(s: str) -> str:
"""green(s) color s with green.

Expand Down