Skip to content

Commit

Permalink
Improve italy_yaml.py
Browse files Browse the repository at this point in the history
  • Loading branch information
wil93 committed Oct 5, 2024
1 parent 7339397 commit 7ee6946
Showing 1 changed file with 49 additions and 33 deletions.
82 changes: 49 additions & 33 deletions cmscontrib/loaders/italy_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,49 +356,65 @@ def get_task(self, get_statement=True):
logger.info("Loading parameters for task %s.", name)

if get_statement:
# language of testo.pdf / statement.pdf
# 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")]
statement_path = None
for path in paths:
primary_language = "it"

has_statement = os.path.exists(os.path.join(self.path, "statement"))
has_testo = os.path.exists(os.path.join(self.path, "testo"))

# Ensure that only one folder exists: either testo/ or statement/
if has_testo and has_statement:
logger.critical(
"Both testo/ and statement/ are present. This is likely an error."
)
sys.exit(1)

if has_testo:
statement_path = os.path.join(self.path, "testo")
else:
statement_path = os.path.join(self.path, "statement")

single_statement_path = None
for path in [
os.path.join(statement_path, "statement.pdf"),
os.path.join(statement_path, "testo.pdf"),
]:
if os.path.exists(path):
statement_path = path
break
single_statement_path = path

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

args["primary_statements"] = [primary_language]
if single_statement_path is not None and len(multi_statement_paths) > 0:
logger.critical(
f"A statement is present in {single_statement_path} but {len(multi_statement_paths)} more multi-language statements were found. This is likely an error."
)
sys.exit(1)

if single_statement_path is not None:
statements_to_import = {primary_language: single_statement_path}
else:
statements_to_import = multi_statement_paths

for (lang, lang_code) in LANGUAGE_MAP.items():
# <language>.pdf always overrides the corresponding language.
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

if primary_language not in args["statements"]:
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)

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)

args["primary_statements"] = [primary_language]

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

# Import the feedback level when explicitly set to full
Expand Down

0 comments on commit 7ee6946

Please sign in to comment.