Prettified submission code

This commit is contained in:
Boyan 2024-11-18 20:03:31 +01:00
parent 2fa3bfbad8
commit f6e6bc28d2

View File

@ -1,5 +1,7 @@
# submission.py
""" """
File to define the submission class File to define the Submission class
""" """
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
@ -7,11 +9,11 @@ from bs4 import BeautifulSoup
class Submission: class Submission:
""" """
Submission class Submission class
Methods: Methods:
test_cases: Get a dict of test cases status get_test_cases: Get a dict of test cases status
info: Submission information (in details) get_info: Submission information (in details)
files: Get a list of uploaded files(as names) get_files: Get a list of uploaded files (as names)
""" """
def __init__(self, url: str, session): def __init__(self, url: str, session):
self.url = "https://themis.housing.rug.nl" + url self.url = "https://themis.housing.rug.nl" + url
@ -24,23 +26,17 @@ class Submission:
"""Clean text""" """Clean text"""
clean = text.replace("\t", "").replace("\n", "") clean = text.replace("\t", "").replace("\n", "")
if value: if value:
return clean return clean.strip()
return clean.replace(" ", "_").replace(":", "").lower() return clean.replace(" ", "_").replace(":", "").lower().strip()
def get_test_cases(self) -> dict[str, str]: def get_test_cases(self) -> dict[str, str]:
"""Get a dict of test cases status""" """Get a dict of test cases status"""
# In the submission page, the test cases are in a div with class "sub-cases subsec round shade"
# print(self.__raw.prettify())
cases = self.__raw.find("div", class_=lambda x: x and "sub-cases" in x.split()) cases = self.__raw.find("div", class_=lambda x: x and "sub-cases" in x.split())
if not cases: if not cases:
return {} return {}
# The test cases are in a table in a div with class "cfg-container"
cases = cases.find("div", class_="cfg-container") cases = cases.find("div", class_="cfg-container")
cases = cases.find("table") cases = cases.find("table")
# For each test case, there is a tr with class sub-casetop, which contains 2 tds:
# * a td with class "sub-case name" which is a name
# * a td with a variable class, which is the status text
results = {} results = {}
for entry in cases.find_all("tr", class_="sub-casetop"): for entry in cases.find_all("tr", class_="sub-casetop"):
@ -48,52 +44,51 @@ class Submission:
status = entry.find( status = entry.find(
"td", class_=lambda x: x and "status-icon" in x.split() "td", class_=lambda x: x and "status-icon" in x.split()
).text ).text
results[name] = self.__clean(status) results[name.strip()] = self.__clean(status)
return results return results
def get_info(self) -> dict[str, str] | None: def get_info(self) -> dict[str, str] | None:
"""Submission information (in details)""" """Submission information (in details)"""
# in div with class subsec round shade where there is an h4 with class info
# The info is in a div with class "cfg-container"
if self.__info: if self.__info:
return self.__info return self.__info
for div in self.__raw.find_all("div", class_="subsec round shade"): for div in self.__raw.find_all("div", class_="subsec round shade"):
if h4 := div.find("h4", class_=lambda x: x and "info" in x.split()): h4 = div.find("h4", class_=lambda x: x and "info" in x.split())
if "Details" in h4.text: if h4 and "Details" in h4.text:
# The information is in divs with class "cfg-line" info = div.find("div", class_="cfg-container")
# With key in span with class "cfg-key" and value in span with class "cfg-value" info_lines = info.find_all("div", class_="cfg-line")
info = div.find("div", class_="cfg-container") self.__info = {
info = info.find_all("div", class_="cfg-line") self.__clean(
return { key := line.find("span", class_="cfg-key").text
self.__clean( ): (
key := line.find("span", class_="cfg-key").text self.__clean(line.find("span", class_="cfg-val").text, value=True)
): if "Files" not in key
self.__clean(line.find("span", class_="cfg-val").text, value=True) if "Files" not in key else else [
([(self.__clean(x.text), x["href"]) for x in line.find("span", class_="cfg-val").find_all("a")]) (self.__clean(a.text), a["href"])
for line in info for a in line.find("span", class_="cfg-val").find_all("a")
} ]
)
for line in info_lines
}
return self.__info
return None return None
def get_files(self) -> list[str] | None: def get_files(self) -> list[str] | None:
"""Get a list of uploaded files in the format [(name, url)]""" """Get a list of uploaded files in the format [(name, url)]"""
if not self.__info: if not self.__info:
self.__info = self.get_info() self.__info = self.get_info()
return self.__info.get("files", None) return self.__info.get("files", None)
# TODO: Remove deprecated methods # Deprecated methods
def info(self): def info(self):
print("This method is deprecated and will be deleted in v1.1.3. Use get_info instead.") print("This method is deprecated and will be deleted in v1.1.3. Use get_info instead.")
return self.get_info() return self.get_info()
def test_cases(self): def test_cases(self):
print("This method is deprecated and will be deleted in v1.1.3. Use get_test_cases instead.") print("This method is deprecated and will be deleted in v1.1.3. Use get_test_cases instead.")
return self.get_test_cases() return self.get_test_cases()
def files(self): def files(self):
print("This method is deprecated and will be deleted in v1.1.3. Use get_files instead.") print("This method is deprecated and will be deleted in v1.1.3. Use get_files instead.")
return self.get_files() return self.get_files()