Skip to content

Commit

Permalink
use StatementLocationMode
Browse files Browse the repository at this point in the history
  • Loading branch information
tsutaj committed Nov 5, 2023
1 parent cf0351b commit cf20135
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
5 changes: 3 additions & 2 deletions statements_manager/src/convert_task_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from statements_manager.src.execute_config import ProblemSetConfig
from statements_manager.src.params_maker.lang_to_class import lang_to_class
from statements_manager.src.renderer import Renderer
from statements_manager.src.statement_location_mode import StatementLocationMode
from statements_manager.src.utils import create_token, dict_merge

logger: Logger = getLogger(__name__)
Expand Down Expand Up @@ -103,9 +104,9 @@ def get_docs_contents(self, problem_id: str) -> Tuple[ContentsStatus, str]:
# ローカルまたは Google Docs から問題文のテキストファイルを取得
def get_contents(self, problem_id: str) -> Tuple[ContentsStatus, str]:
mode = self.problemset_config.get_problem(problem_id).statement.mode
if mode == "local":
if mode == StatementLocationMode.LOCAL:
return self.get_local_contents(problem_id)
elif mode == "docs":
elif mode == StatementLocationMode.DOCS:
return self.get_docs_contents(problem_id)
else:
logger.error(f"unknown mode: {mode}")
Expand Down
16 changes: 11 additions & 5 deletions statements_manager/src/execute_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

import toml

from statements_manager.src.recognize_mode import is_valid_url, recognize_mode
from statements_manager.src.statement_location_mode import (
StatementLocationMode,
is_valid_url,
recognize_mode,
)
from statements_manager.src.utils import read_text_file, resolve_path
from statements_manager.template import (
default_sample_template_html,
Expand Down Expand Up @@ -40,12 +44,14 @@ def __init__(self, filename: pathlib.Path, config: dict) -> None:
self.markdown_extensions: list[Any] = self.optional(
filename, config, "markdown_extensions", []
)
self.mode: str | None = self.optional(filename, config, "mode", None)
if self.mode is None:
self.mode = StatementLocationMode.read(
self.optional(filename, config, "mode", None)
)
if self.mode == StatementLocationMode.UNKNOWN:
dirname = filename.parent.resolve()
self.mode = recognize_mode(self.path, dirname)
logger.debug(f"recognize mode: file = {self.path}, mode = {self.mode}")
if self.mode == "docs" and is_valid_url(self.path):
if self.mode == StatementLocationMode.DOCS and is_valid_url(self.path):
self.path = urlparse(self.path).path.split("/")[3]

# below are used on rendering.
Expand Down Expand Up @@ -130,7 +136,7 @@ def __init__(
self.sample_path = resolve_path(dirname, self.sample_path) # type: ignore
self.params_path = resolve_path(dirname, self.params_path)
self.output_path: str = resolve_path(dirname, "ss-out") # type: ignore
if self.statement.mode == "local":
if self.statement.mode == StatementLocationMode.LOCAL:
self.statement.path = resolve_path(dirname, self.statement.path) # type: ignore


Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
from __future__ import annotations

import pathlib
from enum import Enum
from logging import Logger, getLogger
from urllib.parse import urlparse

logger: Logger = getLogger(__name__)


class StatementLocationMode(Enum):
UNKNOWN, LOCAL, DOCS = range(3)

@staticmethod
def read(mode: str | None):
if mode is None:
return StatementLocationMode.UNKNOWN
elif mode.lower() == "local":
return StatementLocationMode.LOCAL
elif mode.lower() == "docs":
return StatementLocationMode.DOCS
else:
raise NotImplementedError(f"unknown mode: {mode}")


def is_valid_url(url: str) -> bool:
"""
check whether given string is valid as url format.
Expand All @@ -18,16 +34,18 @@ def is_valid_url(url: str) -> bool:
return False


def recognize_mode(statement_path: str, base_path: pathlib.Path) -> str:
def recognize_mode(
statement_path: str, base_path: pathlib.Path
) -> StatementLocationMode:
"""
recognize execute mode. ("docs" or "local")
"""

# if "statement_path" is valid as local path, it may be "local"
if (base_path / pathlib.Path(statement_path)).exists():
return "local"
return StatementLocationMode.LOCAL
# otherwise, it may be "docs"
# it is either in URL-format or in ID-format.
# URL-format: https://docs.google.com/document/d/{DOCUMENT_ID}/...
else:
return "docs"
return StatementLocationMode.DOCS

0 comments on commit cf20135

Please sign in to comment.