Skip to content

Commit

Permalink
Complete "Make the yaml importer load multiple languages."
Browse files Browse the repository at this point in the history
  • Loading branch information
veluca93 committed Nov 3, 2024
1 parent a04b371 commit 078bbf6
Showing 1 changed file with 57 additions and 30 deletions.
87 changes: 57 additions & 30 deletions cmscontrib/loaders/italy_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ def getmtime(fname):

yaml_cache = {}


def load_yaml_from_path(path):
if path in yaml_cache:
return yaml_cache[path]
Expand Down Expand Up @@ -369,42 +368,70 @@ def get_task(self, get_statement=True):
logger.info("Loading parameters for task %s.", name)

if get_statement:
# The language of testo.pdf / statement.pdf, defaulting to 'it'
primary_language = load(conf, None, "primary_language")
if primary_language is None:
primary_language = 'it'
paths = [os.path.join(self.path, "statement", "statement.pdf"),
os.path.join(self.path, "testo", "testo.pdf")]
for path in paths:
primary_language = "it"

statement = None
for localized_statement in ["statement", "testo"]:
if os.path.exists(os.path.join(self.path, localized_statement)):
# Ensure that only one folder exists: either testo/ or statement/
if statement is not None:
logger.critical(
"Both testo/ and statement/ are present. This is likely an error."
)
sys.exit(1)
statement = localized_statement

if statement is None:
logger.critical("Statement folder not found.")
sys.exit(1)

single_statement_path = os.path.join(
self.path, statement, "%s.pdf" % statement)
if not os.path.exists(single_statement_path):
single_statement_path = None

multi_statement_paths = {}
for lang, lang_code in LANGUAGE_MAP.items():
path = os.path.join(self.path, statement, "%s.pdf" % lang)
if os.path.exists(path):
digest = self.file_cacher.put_file_from_path(
path,
"Statement for task %s (lang: %s)" %
(name, primary_language))
break
multi_statement_paths[lang_code] = path

if len(multi_statement_paths) > 0:
# Ensure that either a statement.pdf or testo.pdf is specified,
# or a list of <lang>.pdf files are specified, but not both,
# unless statement.pdf or testo.pdf is a symlink, in which case
# we let it slide.
if single_statement_path is not None and not os.path.islink(
single_statement_path
):
logger.warning(
f"A statement (not a symlink!) is present at {single_statement_path} "
f"but {len(multi_statement_paths)} more multi-language statements "
"were found. This is likely an error. Proceeding with "
"importing the multi-language files only."
)
statements_to_import = multi_statement_paths
else:
logger.critical("Couldn't find any task statement, aborting.")
statements_to_import = {
primary_language: single_statement_path}

if primary_language not in statements_to_import.keys():
logger.critical(
"Couldn't find statement for primary language %s, aborting." % primary_language)
sys.exit(1)

args["statements"] = {
primary_language: Statement(
primary_language, digest)
}
args["primary_statements"] = [primary_language]
args["statements"] = dict()
for lang_code, statement_path in statements_to_import.items():
digest = self.file_cacher.put_file_from_path(
statement_path,
"Statement for task %s (lang: %s)" % (name, lang_code),
)
args["statements"][lang_code] = Statement(lang_code, digest)

for (lang, lang_code) in LANGUAGE_MAP.items():
if lang_code == primary_language:
continue
paths = [os.path.join(self.path, "statement", "%s.pdf" % lang),
os.path.join(self.path, "testo", "%s.pdf" % lang)]
for path in paths:
if os.path.exists(path):
digest = self.file_cacher.put_file_from_path(
path,
"Statement for task %s (lang: %s)" %
(name, lang_code))
args["statements"][lang_code] = Statement(
lang_code, digest)
break
args["primary_statements"] = [primary_language]

args["submission_format"] = ["%s.%%l" % name]

Expand Down

0 comments on commit 078bbf6

Please sign in to comment.