Skip to content

Commit

Permalink
#225: start using pathlib
Browse files Browse the repository at this point in the history
  • Loading branch information
kmyk committed Jan 2, 2019
1 parent f8384d1 commit 7d69c93
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
6 changes: 5 additions & 1 deletion onlinejudge/implementation/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import sys
import os
import os.path
import pathlib
from typing import List, Optional


Expand All @@ -30,7 +31,10 @@ def version_check() -> None:
def get_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description='Tools for online judge services')
parser.add_argument('-v', '--verbose', action='store_true')
parser.add_argument('-c', '--cookie', help='path to cookie. (default: {})'.format(utils.default_cookie_path))
parser.add_argument('-c', '--cookie',
type=pathlib.Path,
default=utils.default_cookie_path,
help='path to cookie. (default: {})'.format(utils.default_cookie_path))
subparsers = parser.add_subparsers(dest='subcommand', help='for details, see "{} COMMAND --help"'.format(sys.argv[0]))

# download
Expand Down
21 changes: 10 additions & 11 deletions onlinejudge/implementation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
import ast
import time
import appdirs
import pathlib
from typing import *
from typing.io import *

config_dir = appdirs.user_config_dir(version.name)
data_dir = appdirs.user_data_dir(version.name)
cache_dir = appdirs.user_cache_dir(version.name)
config_dir = pathlib.Path(appdirs.user_config_dir(version.name))
data_dir = pathlib.Path(appdirs.user_data_dir(version.name))
cache_dir = pathlib.Path(appdirs.user_cache_dir(version.name))
html_parser = 'lxml'

def parcentformat(s: str, table: Dict[str, str]) -> str:
Expand Down Expand Up @@ -58,21 +59,19 @@ def new_default_session() -> requests.Session: # without setting cookiejar
session.headers['User-Agent'] += ' (+{})'.format(version.__url__)
return session

default_cookie_path = os.path.join(data_dir, 'cookie.jar')
default_cookie_path = data_dir / 'cookie.jar'

@contextlib.contextmanager
def with_cookiejar(session: requests.Session, path: str) -> Generator[requests.Session, None, None]:
path = path or default_cookie_path
session.cookies = http.cookiejar.LWPCookieJar(path) # type: ignore
if os.path.exists(path):
def with_cookiejar(session: requests.Session, path: pathlib.Path = default_cookie_path) -> Generator[requests.Session, None, None]:
session.cookies = http.cookiejar.LWPCookieJar(str(path)) # type: ignore
if path.exists():
log.status('load cookie from: %s', path)
session.cookies.load() # type: ignore
yield session
log.status('save cookie to: %s', path)
if os.path.dirname(path):
os.makedirs(os.path.dirname(path), exist_ok=True)
path.parent.mkdir(parents=True, exist_ok=True)
session.cookies.save() # type: ignore
os.chmod(path, 0o600) # NOTE: to make secure a little bit
path.chmod(0o600) # NOTE: to make secure a little bit


class SampleZipper(object):
Expand Down

0 comments on commit 7d69c93

Please sign in to comment.