Skip to content

Commit

Permalink
Reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
MasloMaslane committed Nov 30, 2024
1 parent 747532d commit c577483
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 97 deletions.
3 changes: 1 addition & 2 deletions src/sio3pack/packages/package/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
from sio3pack.graph.graph import Graph
from sio3pack.packages.exceptions import UnknownPackageType
from sio3pack.test.test import Test

from sio3pack.packages import all_packages
from sio3pack.utils.archive import Archive
from sio3pack.utils.classinit import RegisteredSubclassesBase

Expand All @@ -14,6 +12,7 @@ class Package(RegisteredSubclassesBase):
"""
Base class for all packages.
"""

abstract = True

def __init__(self, file: File):
Expand Down
6 changes: 3 additions & 3 deletions src/sio3pack/packages/sinolpack/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ class ModelSolutionKind(Enum):

@classmethod
def from_regex(cls, group):
if group == '':
if group == "":
return cls.NORMAL
if group == 's':
if group == "s":
return cls.SLOW
if group == 'b':
if group == "b":
return cls.INCORRECT
raise ValueError(f"Invalid model solution kind: {group}")
83 changes: 47 additions & 36 deletions src/sio3pack/packages/sinolpack/model.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import json
import os
import re
import yaml
import tempfile

import yaml

from sio3pack.files.file import File
from sio3pack.graph.graph import Graph
from sio3pack.graph.graph_manager import GraphManager
Expand All @@ -26,7 +26,7 @@ def _find_main_dir(cls, archive: Archive) -> str | None:
toplevel_dirs = list(set(f.split(os.sep)[0] for f in dirs))
problem_dirs = []
for dir in toplevel_dirs:
for required_subdir in ('in', 'out'):
for required_subdir in ("in", "out"):
if all(f.split(os.sep)[:2] != [dir, required_subdir] for f in dirs):
break
else:
Expand Down Expand Up @@ -68,20 +68,24 @@ def __init__(self, file: File, django_settings=None):
self.rootdir = file.path

try:
graph_file = self.get_in_root('graph.json')
graph_file = self.get_in_root("graph.json")
self.graph_manager = GraphManager.from_file(graph_file)
except FileNotFoundError:
self.has_custom_graph = False

self.django_settings = django_settings

def _default_graph_manager(self) -> GraphManager:
return GraphManager({
"unpack": Graph.from_dict({
"name": "unpack",
# ...
})
})
return GraphManager(
{
"unpack": Graph.from_dict(
{
"name": "unpack",
# ...
}
)
}
)

def _get_from_django_settings(self, key: str, default=None):
if self.django_settings is None:
Expand All @@ -92,7 +96,7 @@ def get_doc_dir(self) -> str:
"""
Returns the path to the directory containing the problem's documents.
"""
return os.path.join(self.rootdir, 'doc')
return os.path.join(self.rootdir, "doc")

def get_in_doc_dir(self, filename: str) -> File:
"""
Expand All @@ -110,7 +114,7 @@ def get_prog_dir(self) -> str:
"""
Returns the path to the directory containing the problem's program files.
"""
return os.path.join(self.rootdir, 'prog')
return os.path.join(self.rootdir, "prog")

def get_in_prog_dir(self, filename: str) -> File:
"""
Expand All @@ -122,7 +126,7 @@ def get_attachments_dir(self) -> str:
"""
Returns the path to the directory containing the problem's attachments.
"""
return os.path.join(self.rootdir, 'attachments')
return os.path.join(self.rootdir, "attachments")

def _process_package(self):
self._process_config_yml()
Expand All @@ -141,7 +145,7 @@ def _process_config_yml(self):
Process the config.yml file. If it exists, it will be loaded into the config attribute.
"""
try:
config = self.get_in_root('config.yml')
config = self.get_in_root("config.yml")
self.config = yaml.safe_load(config.read())
except FileNotFoundError:
self.config = {}
Expand All @@ -155,14 +159,14 @@ def _detect_full_name(self):
Example of how the ``title`` tag may look like:
\title{A problem}
"""
if 'title' in self.config:
self.full_name = self.config['title']
if "title" in self.config:
self.full_name = self.config["title"]
return

try:
source = self.get_in_doc_dir(self.short_name + 'zad.tex')
source = self.get_in_doc_dir(self.short_name + "zad.tex")
text = source.read()
r = re.search(r'^[^%]*\\title{(.+)}', text, re.MULTILINE)
r = re.search(r"^[^%]*\\title{(.+)}", text, re.MULTILINE)
if r is not None:
self.full_name = r.group(1)
except FileNotFoundError:
Expand All @@ -174,8 +178,8 @@ def _detect_full_name_translations(self):
two-letter language code defined in ``settings.py``), if any such key is given.
"""
self.lang_titles = {}
for lang_code, lang in self._get_from_django_settings('LANGUAGES', [('en', 'English')]):
key = 'title_%s' % lang_code
for lang_code, lang in self._get_from_django_settings("LANGUAGES", [("en", "English")]):
key = "title_%s" % lang_code
if key in self.config:
self.lang_titles[lang_code] = self.config[key]

Expand All @@ -184,16 +188,15 @@ def get_submittable_extensions(self):
Returns a list of extensions that are submittable.
"""
return self.config.get(
'submittable_langs',
self._get_from_django_settings('SUBMITTABLE_LANGUAGES', ['c', 'cpp', 'cxx', 'py'])
"submittable_langs", self._get_from_django_settings("SUBMITTABLE_LANGUAGES", ["c", "cpp", "cxx", "py"])
)

def get_model_solution_regex(self):
"""
Returns the regex used to determine model solutions.
"""
extensions = self.get_submittable_extensions()
return rf'^{self.short_name}[0-9]*([bs]?)[0-9]*(_.*)?\.(' + '|'.join(extensions) + ')'
return rf"^{self.short_name}[0-9]*([bs]?)[0-9]*(_.*)?\.(" + "|".join(extensions) + ")"

def _get_model_solutions(self) -> list[tuple[ModelSolutionKind, str]]:
"""
Expand All @@ -208,13 +211,17 @@ def _get_model_solutions(self) -> list[tuple[ModelSolutionKind, str]]:

return model_solutions

def sort_model_solutions(self, model_solutions: list[tuple[ModelSolutionKind, str]]) -> list[tuple[ModelSolutionKind, str]]:
def sort_model_solutions(
self, model_solutions: list[tuple[ModelSolutionKind, str]]
) -> list[tuple[ModelSolutionKind, str]]:
"""
Sorts model solutions by kind.
"""

def sort_key(model_solution):
kind, name = model_solution
return kind.value, naturalsort_key(name[: name.index(".")])

return list(sorted(model_solutions, key=sort_key))

def _process_prog_files(self):
Expand All @@ -233,13 +240,17 @@ def _process_prog_files(self):
self.additional_files = self.graph_manager.get_prog_files()
else:
self.additional_files = []
self.additional_files.extend(self.config.get('extra_compilation_files', []))
self.additional_files.extend(self.config.get('extra_execution_files', []))
self.additional_files.extend(self.config.get("extra_compilation_files", []))
self.additional_files.extend(self.config.get("extra_execution_files", []))
extensions = self.get_submittable_extensions()
self.special_files: dict[str, bool] = {}
for file in ('ingen', 'inwer', 'soc', 'chk'):
for file in ("ingen", "inwer", "soc", "chk"):
try:
self.additional_files.append(File.get_file_matching_extension(self.get_prog_dir(), self.short_name + file, extensions).filename)
self.additional_files.append(
File.get_file_matching_extension(
self.get_prog_dir(), self.short_name + file, extensions
).filename
)
self.special_files[file] = True
except FileNotFoundError:
self.special_files[file] = False
Expand All @@ -256,12 +267,14 @@ def _process_statements(self):
if not os.path.exists(docdir):
return

lang_prefs = [''] + ['-' + l[0] for l in self._get_from_django_settings('LANGUAGES', [('en', 'English'), ('pl', 'Polish')])]
lang_prefs = [""] + [
"-" + l[0] for l in self._get_from_django_settings("LANGUAGES", [("en", "English"), ("pl", "Polish")])
]

self.lang_statements = {}
for lang in lang_prefs:
try:
htmlzipfile = self.get_in_doc_dir(self.short_name + 'zad' + lang + '.html.zip')
htmlzipfile = self.get_in_doc_dir(self.short_name + "zad" + lang + ".html.zip")
# TODO: what to do with html?
# if self._html_disallowed():
# raise ProblemPackageError(
Expand All @@ -281,18 +294,16 @@ def _process_statements(self):
pass

try:
pdffile = self.get_in_doc_dir(self.short_name + 'zad' + lang + '.pdf')
if lang == '':
pdffile = self.get_in_doc_dir(self.short_name + "zad" + lang + ".pdf")
if lang == "":
self.statement = pdffile
else:
self.lang_statements[lang[1:]] = pdffile
except:
pass

def _process_attachments(self):
"""
"""
""" """
attachments_dir = self.get_attachments_dir()
if not os.path.isdir(attachments_dir):
return
Expand All @@ -305,7 +316,7 @@ def _process_attachments(self):
def get_unpack_graph(self) -> GraphOperation | None:
try:
return GraphOperation(
self.graph_manager.get('unpack'),
self.graph_manager.get("unpack"),
True,
self._unpack_return_data,
)
Expand Down
5 changes: 1 addition & 4 deletions src/sio3pack/util.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import re
import tarfile
import zipfile

from sio3pack.files.file import File

def naturalsort_key(key):
convert = lambda text: int(text) if text.isdigit() else text
return [convert(c) for c in re.split('([0-9]+)', key)]
return [convert(c) for c in re.split("([0-9]+)", key)]
55 changes: 19 additions & 36 deletions src/sio3pack/utils/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class UnsafeArchive(ArchiveException):
"""


def extract(path, to_path='', ext='', **kwargs):
def extract(path, to_path="", ext="", **kwargs):
"""
Unpack the tar or zip file at the specified path to the directory
specified by to_path.
Expand All @@ -54,7 +54,7 @@ class Archive(object):
The external API class that encapsulates an archive implementation.
"""

def __init__(self, file, ext=''):
def __init__(self, file, ext=""):
"""
Arguments:
* 'file' can be a string path to a file or a file-like object.
Expand All @@ -66,7 +66,7 @@ def __init__(self, file, ext=''):
self._archive = self._archive_cls(self.filename, ext=ext)(self.filename)

@staticmethod
def _archive_cls(file, ext=''):
def _archive_cls(file, ext=""):
"""
Return the proper Archive implementation class, based on the file type.
"""
Expand All @@ -78,19 +78,15 @@ def _archive_cls(file, ext=''):
try:
filename = file.name
except AttributeError:
raise UnrecognizedArchiveFormat(
"File object not a recognized archive format."
)
raise UnrecognizedArchiveFormat("File object not a recognized archive format.")
lookup_filename = filename + ext
base, tail_ext = os.path.splitext(lookup_filename.lower())
cls = extension_map.get(tail_ext)
if not cls:
base, ext = os.path.splitext(base)
cls = extension_map.get(ext)
if not cls:
raise UnrecognizedArchiveFormat(
"Path not a recognized archive format: %s" % filename
)
raise UnrecognizedArchiveFormat("Path not a recognized archive format: %s" % filename)
return cls

@classmethod
Expand Down Expand Up @@ -152,10 +148,10 @@ def _extract(self, to_path):
"""
self._archive.extractall(to_path)

def extract(self, to_path='', method='safe'):
if method == 'safe':
def extract(self, to_path="", method="safe"):
if method == "safe":
self.check_files(to_path)
elif method == 'insecure':
elif method == "insecure":
pass
else:
raise ValueError("Invalid method option")
Expand All @@ -175,8 +171,7 @@ def check_files(self, to_path=None):
extract_path = os.path.normpath(os.path.realpath(extract_path))
if not extract_path.startswith(target_path):
raise UnsafeArchive(
"Archive member destination is outside the target"
" directory. member: %s" % filename
"Archive member destination is outside the target" " directory. member: %s" % filename
)


Expand All @@ -189,14 +184,10 @@ def __init__(self, file):
self._archive = tarfile.open(fileobj=file)

def filenames(self):
return [
tarinfo.name for tarinfo in self._archive.getmembers() if tarinfo.isfile()
]
return [tarinfo.name for tarinfo in self._archive.getmembers() if tarinfo.isfile()]

def dirnames(self):
return [
tarinfo.name for tarinfo in self._archive.getmembers() if tarinfo.isdir()
]
return [tarinfo.name for tarinfo in self._archive.getmembers() if tarinfo.isdir()]

def extracted_size(self):
total = 0
Expand Down Expand Up @@ -226,25 +217,17 @@ def extracted_size(self):
return total

def filenames(self):
return [
zipinfo.filename
for zipinfo in self._archive.infolist()
if not zipinfo.is_dir()
]
return [zipinfo.filename for zipinfo in self._archive.infolist() if not zipinfo.is_dir()]

def dirnames(self):
return [
zipinfo.filename
for zipinfo in self._archive.infolist()
if zipinfo.is_dir()
]
return [zipinfo.filename for zipinfo in self._archive.infolist() if zipinfo.is_dir()]


extension_map = {
'.tar': TarArchive,
'.tar.bz2': TarArchive,
'.tar.gz': TarArchive,
'.tgz': TarArchive,
'.tz2': TarArchive,
'.zip': ZipArchive,
".tar": TarArchive,
".tar.bz2": TarArchive,
".tar.gz": TarArchive,
".tgz": TarArchive,
".tz2": TarArchive,
".zip": ZipArchive,
}
Loading

0 comments on commit c577483

Please sign in to comment.