Compare commits

..

No commits in common. "213a4d547125a3ff510c157c4c6866194dc83fad" and "6a781ad2381e642801f77173748dc74537dd5df5" have entirely different histories.

10 changed files with 80 additions and 211 deletions

View File

@ -1,25 +0,0 @@
name: Pylint
on: [push]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12"]
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pylint
pip install .
- name: Analyzing the code with pylint
run: |
pylint temmies

View File

@ -1,58 +0,0 @@
name: Upload Python Package
on:
release:
types: [published]
permissions:
contents: read
jobs:
release-build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.x"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install setuptools wheel twine
- name: Build release distributions
run: |
python setup.py bdist_wheel
- name: Upload distributions
uses: actions/upload-artifact@v4
with:
name: release-dists
path: dist/
pypi-publish:
runs-on: ubuntu-latest
needs:
- release-build
permissions:
id-token: write
environment:
name: pypi
steps:
- name: Retrieve release distributions
uses: actions/download-artifact@v4
with:
name: release-dists
path: dist/
- name: Publish release distributions to PyPI
env:
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
run: |
python -m pip install twine
twine upload dist/*

View File

@ -5,7 +5,7 @@ with open("README.md", "r") as f:
setup(
name="temmies",
version="1.2.121",
version="1.2.1",
packages=find_packages(),
description="A wrapper for the Themis website",
long_description=l_description,
@ -21,7 +21,6 @@ setup(
"Programming Language :: Python :: 3.9",
],
install_requires=[
"urllib3",
"requests",
"lxml",
"beautifulsoup4",

View File

@ -1,8 +1,5 @@
"""
Entry point for the temmies package.
"""
import urllib3
from .themis import Themis
import urllib3
__all__ = ["Themis"]
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

View File

@ -1,10 +1,5 @@
"""
Represents a course.
A course is a group that contains exercises or other groups.
"""
from .group import Group
from .exercise_group import ExerciseGroup
class Course(Group):
"""
@ -30,11 +25,11 @@ class Course(Group):
self,
item_data.get("submitable", False),
)
return Group(
self.session,
item_data["path"],
item_data["title"],
self,
item_data.get("submitable", False),
)
else:
return Group(
self.session,
item_data["path"],
item_data["title"],
self,
item_data.get("submitable", False),
)

View File

@ -1,24 +1,23 @@
"""
Represents a submittable exercise.
"""
from bs4 import BeautifulSoup
from .group import Group
from .submission import Submission
from bs4 import BeautifulSoup
class ExerciseGroup(Group):
"""
Represents a submittable exercise.
"""
# pylint: disable=too-many-arguments, too-many-positional-arguments
def __init__(self, session, path: str, title: str, parent, submitable: bool = True):
super().__init__(session, path, title, parent, submitable=submitable)
self.submit_url = f"{self.base_url}/api/submit{self.path}"
self.__find_name()
def __find_name(self):
"""
Find the name of the exercise group.
"""
if self.title == "":
# Find using beautiful soup (it is the last a with class 'fill accent large')
response = self.session.get(self.base_url + self.path)
soup = BeautifulSoup(response.text, "lxml")
title_elements = soup.find_all("a", class_="fill accent large")
@ -26,6 +25,27 @@ class ExerciseGroup(Group):
self.title = title_elements[-1].get_text(strip=True)
else:
self.title = self.path.split("/")[-1]
def submit(self, files: list[str]) -> Submission:
"""
Submit files to this exercise.
"""
if not self.submitable:
raise ValueError(f"Cannot submit to non-submittable item '{self.title}'.")
# Prepare the files and data for submission
files_payload = {}
for idx, file_path in enumerate(files):
file_key = f"file{idx}"
with open(file_path, "rb") as f:
files_payload[file_key] = (file_path, f.read())
response = self.session.post(self.submit_url, files=files_payload)
if response.status_code != 200:
raise ConnectionError(f"Failed to submit to '{self.title}'.")
submission_data = response.json()
return Submission(self.session, submission_data)
def __str__(self):
return f"ExerciseGroup({self.title})"

View File

@ -1,22 +1,15 @@
"""
Abstract-ish Group class for Themis API.
"""
from bs4 import BeautifulSoup
from requests import Session
import os
from typing import Optional, Union, Dict
from json import loads
from time import sleep
from bs4 import BeautifulSoup
from .exceptions.illegal_action import IllegalAction
from .submission import Submission
class Group:
"""
Represents an item in Themis.
Can be either a folder (non-submittable) or an assignment (submittable).
Represents an item in Themis, which can be either a folder (non-submittable) or an assignment (submittable).
"""
# pylint: disable=too-many-instance-attributes, too-many-arguments, too-many-positional-arguments
def __init__(self, session, path: str, title: str, parent=None, submitable: bool = False):
self.session = session
self.path = path # e.g., '/2023-2024/adinc-ai/labs'
@ -36,9 +29,10 @@ class Group:
# Fetch the page and parse it
response = self.session.get(group_url)
if response.status_code != 200:
raise ConnectionError(f"Failed to retrieve page for '{self.title}'.")
raise ConnectionError(f"Failed to retrieve page for '{self.title}'. Tried {group_url}")
self._raw = BeautifulSoup(response.text, "lxml")
def get_items(self) -> list:
"""
Get all items (groups and assignments) under this group.
@ -74,20 +68,19 @@ class Group:
return item
raise ValueError(f"Item '{title}' not found under {self.title}.")
def get_status(self, text: bool = False) -> Union[Dict[str, Union[str, 'Submission']], None]:
"""
Get the status of the current group, if available.
"""
status_link = self._raw.find("a", text="Status")
if not status_link:
raise ValueError(
"Status information is not available for this group.")
raise ValueError("Status information is not available for this group.")
status_url = f"{self.base_url}{status_link['href']}"
response = self.session.get(status_url)
if response.status_code != 200:
raise ConnectionError(
f"Failed to retrieve status page for '{self.title}'.")
raise ConnectionError(f"Failed to retrieve status page for '{self.title}'.")
soup = BeautifulSoup(response.text, "lxml")
section = soup.find("div", class_="cfg-container")
@ -97,10 +90,7 @@ class Group:
return self.__parse_status_section(section, text)
def __parse_status_section(
self, section: BeautifulSoup,
text: bool
) -> Dict[str, Union[str, 'Submission']]:
def __parse_status_section(self, section: BeautifulSoup, text: bool) -> Dict[str, Union[str, 'Submission']]:
"""
Parse the status section of the group and clean up keys.
"""
@ -121,10 +111,8 @@ class Group:
continue
# Normalize key
raw_key = " ".join(key_element.get_text(
separator=" ").strip().replace(":", "").lower().split())
# Use mapped key if available
key = key_mapping.get(raw_key, raw_key)
raw_key = " ".join(key_element.get_text(separator=" ").strip().replace(":", "").lower().split())
key = key_mapping.get(raw_key, raw_key) # Use mapped key if available
# Process value
link = value_element.find("a", href=True)
@ -134,8 +122,7 @@ class Group:
if href.startswith("/"):
submission_url = href
elif href.startswith("http"):
submission_url = href.replace(
"https://themis.housing.rug.nl", "")
submission_url = href.replace("https://themis.housing.rug.nl", "")
else:
print(f"Invalid href '{href}' found in status page.")
continue # Skip this entry if href is invalid
@ -148,13 +135,13 @@ class Group:
return parsed
def get_test_cases(self) -> list[Dict[str, str]]:
"""
Get all test cases for this assignment.
"""
if not self.submitable:
raise ValueError(
f"No test cases for non-submittable item '{self.title}'.")
raise ValueError(f"No test cases for non-submittable item '{self.title}'.")
sections = self._raw.find_all("div", class_="subsec round shade")
tcs = []
@ -193,8 +180,7 @@ class Group:
"""
Get all downloadable files for this assignment.
"""
details = self._raw.find(
"div", id=lambda x: x and x.startswith("details"))
details = self._raw.find("div", id=lambda x: x and x.startswith("details"))
if not details:
return []
@ -233,21 +219,13 @@ class Group:
print(f"Failed to download file '{file['title']}'")
return downloaded
# pylint: disable=too-many-locals
def submit(
self,
files: list[str],
judge: bool = True,
wait: bool = True,
silent: bool = True
) -> Optional[dict]:
def submit(self, files: list[str], judge: bool = True, wait: bool = True, silent: bool = True) -> Optional[dict]:
"""
Submit files to this assignment.
Returns a dictionary of test case results or None if wait is False.
"""
if not self.submitable:
raise ValueError(
f"Cannot submit to non-submittable item '{self.title}'.")
raise ValueError(f"Cannot submit to non-submittable item '{self.title}'.")
form = self._raw.find("form")
if not form:
@ -302,22 +280,14 @@ class Group:
def __parse_table(self, soup: BeautifulSoup, url: str, verbose: bool, __printed: list) -> dict:
"""
Parse the results table from the submission result page.
Wait until all queued status-icons disappear before parsing.
"""
cases = soup.find_all("tr", class_="sub-casetop")
fail_pass = {}
any_queued = False
for case in cases:
name = case.find("td", class_="sub-casename").text.strip()
name = case.find("td", class_="sub-casename").text
status = case.find("td", class_="status-icon")
status_classes = status.get("class", [])
if "queued" in status_classes:
any_queued = True
break
if "pending" in status_classes:
if "pending" in status.get("class"):
sleep(1)
return self.__wait_for_result(url, verbose, __printed)
@ -329,28 +299,19 @@ class Group:
}
found = False
for key, (symbol, value) in statuses.items():
if key.lower() in status.text.lower():
for k, v in statuses.items():
if k in status.text:
found = True
case_number = int(name)
if verbose and case_number not in __printed:
print(f"Case {case_number}: {symbol}")
fail_pass[case_number] = value
if verbose and int(name) not in __printed:
print(f"{name}: {v[0]}")
fail_pass[int(name)] = v[1]
break
if not found:
case_number = int(name)
fail_pass[case_number] = None
if verbose and case_number not in __printed:
print(f"{case_number}: Unrecognized status: {status.text.strip()}")
__printed.append(case_number)
# Polling (fix, use ws)
if any_queued:
sleep(1)
return self.__wait_for_result(url, verbose, __printed)
fail_pass[int(name)] = None
if verbose and int(name) not in __printed:
print(f"{name}: Unrecognized status: {status.text}")
__printed.append(int(name))
return fail_pass
def __str__(self):

View File

@ -4,7 +4,6 @@
File to define the Submission class
"""
from typing import Optional
from bs4 import BeautifulSoup
class Submission:
@ -49,7 +48,7 @@ class Submission:
return results
def get_info(self) -> Optional[dict[str, str]]:
def get_info(self) -> dict[str, str] | None:
"""Submission information (in details)"""
if self.__info:
return self.__info
@ -75,7 +74,7 @@ class Submission:
return self.__info
return None
def get_files(self) -> Optional[list[str]]:
def get_files(self) -> list[str] | None:
"""Get a list of uploaded files in the format [(name, url)]"""
if not self.__info:
self.__info = self.get_info()
@ -83,22 +82,13 @@ class Submission:
# Deprecated methods
def info(self):
"""
Deprecated method. Use get_info instead.
"""
print("This method is deprecated and will be deleted soon. Use get_info instead.")
return self.get_info()
def test_cases(self):
"""
Deprecated method. Use get_test_cases instead.
"""
print("This method is deprecated and will be deleted in soon. Use get_test_cases instead.")
return self.get_test_cases()
def files(self):
"""
Deprecated method. Use get_files instead.
"""
print("This method is deprecated and will be deleted in soon. Use get_files instead.")
return self.get_files()

View File

@ -2,12 +2,12 @@
Main class for the Themis API using the new JSON endpoints.
"""
import getpass
import keyring
import getpass
from requests import Session
from bs4 import BeautifulSoup
from .year import Year
from .exceptions.illegal_action import IllegalAction
class Themis:
"""
@ -34,7 +34,6 @@ class Themis:
self.password = self.__get_password()
self.base_url = "https://themis.housing.rug.nl"
self.session = self.login(self.user, self.password)
def __get_password(self) -> str:
"""
Retrieve the password from the keyring, prompting the user if not found.
@ -42,8 +41,7 @@ class Themis:
password = keyring.get_password(f"{self.user}-temmies", self.user)
if not password:
print(f"Password for user '{self.user}' not found in keyring.")
password = getpass.getpass(
prompt=f"Enter password for {self.user}: ")
password = getpass.getpass(prompt=f"Enter password for {self.user}: ")
keyring.set_password(f"{self.user}-temmies", self.user, password)
print("Password saved securely in keyring.")
return password
@ -87,8 +85,7 @@ class Themis:
passwd = getpass.getpass(prompt="Enter password: ")
keyring.set_password(f'{self.user}-temmies', self.user, passwd)
return self.login(user, passwd)
if "Welcome, logged in as" not in response.text:
elif "Welcome, logged in as" not in response.text:
raise ValueError("Login failed for an unknown reason.")
return session
@ -98,7 +95,7 @@ class Themis:
Gets a Year object using the year path (e.g., 2023, 2024).
"""
year_path = f"{start_year}-{end_year}"
return Year(self.session, year_path)
def all_years(self) -> list:

View File

@ -1,11 +1,5 @@
"""
This module defines the Year class for managing academic year courses.
"""
from bs4 import BeautifulSoup
from .course import Course
from bs4 import BeautifulSoup
class Year:
"""
Represents an academic year.
@ -43,6 +37,8 @@ class Year:
return course
raise ValueError(f"Course '{course_title}' not found in year {self.year_path}.")
from bs4 import BeautifulSoup
def get_course_by_tag(self, course_tag: str) -> Course:
"""
Gets a course by its tag (course identifier).
@ -53,21 +49,18 @@ class Year:
response = self.session.get(course_url)
if response.status_code != 200:
raise ConnectionError(
f"Failed to retrieve course '{course_tag}' for year {self.year_path}."
)
raise ConnectionError(f"Failed to retrieve course with tag '{course_tag}' for year {self.year_path}. Tried {course_url}")
soup = BeautifulSoup(response.text, "lxml")
title_elements = soup.find_all("a", class_="fill accent large")
title_element = title_elements[-1] if title_elements else None
if title_elements:
title_element = title_elements[-1]
if title_element:
course_title = title_element.get_text(strip=True)
else:
raise ValueError(
f"Could not retrieve course title for tag '{course_tag}' in year {self.year_path}."
)
raise ValueError(f"Could not retrieve course title for tag '{course_tag}' in year {self.year_path}.")
return Course(self.session, course_path, course_title, self)