From 8eeb8b5600139cc50648ba3c304938d91cf6d436 Mon Sep 17 00:00:00 2001 From: rzuckerm Date: Tue, 25 Apr 2023 19:53:31 -0500 Subject: [PATCH] Add ability to detect if sample program is an image (#62) * Add ability to detect if sample program is an image * Add ability to get project path --- docs/changelog.rst | 7 +++++++ setup.py | 4 ++-- subete/repo.py | 28 ++++++++++++++++++++++++++++ tests/test_integration.py | 17 +++++++++++++++++ 4 files changed, 54 insertions(+), 2 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 7a7d585..6912d48 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -4,6 +4,13 @@ Changelog Below you'll find all the changes that have been made to the code with newest changes first. +0.16.x +------ + +* v0.16.0 + * Added ability to detect if sample program is an image and return image type + * Added ability to get path to sample program + 0.15.x ------ diff --git a/setup.py b/setup.py index d31c29e..d634c21 100644 --- a/setup.py +++ b/setup.py @@ -12,8 +12,8 @@ long_description = fh.read() MAJOR = 0 -MINOR = 15 -PATCH = 1 +MINOR = 16 +PATCH = 0 name = "subete" version = f"{MAJOR}.{MINOR}" diff --git a/subete/repo.py b/subete/repo.py index 2ddc841..881534c 100644 --- a/subete/repo.py +++ b/subete/repo.py @@ -1,6 +1,7 @@ from __future__ import annotations import datetime +import imghdr import logging import os import random @@ -885,6 +886,19 @@ def project_pathlike_name(self) -> str: logger.info(f'Retrieving project pathlike name for {self}: {self._project}') return self._project.pathlike_name() + def project_path(self) -> str: + """ + Retrieves the path to the project file. + + Assuming you have a SampleProgram object called program, + here's how you would use this method:: + + project_path: str = program.project_path() + + :return: the project path (e.g., .../archive/p/python/hello_world.py) + """ + return os.path.join(self._path, self._file_name) + def code(self) -> str: """ Retrieves the code for this sample program. To save space @@ -903,6 +917,20 @@ def code(self) -> str: logger.info(f"Retrieving code from {self._path}/{self._file_name}") return Path(self._path, self._file_name).read_text(errors="replace") + def image_type(self) -> str: + """ + Determine if sample program is actual an image, and if so, what type. + + Assuming you have a SampleProgram object called program, + here's how you would use this method:: + + image_type: str = program.image_type() + + :return: Image type if sample program is an image (e.g., "png"), + empty string otherwise + """ + return imghdr.what(Path(self._path, self._file_name)) or "" + def line_count(self) -> int: """ Retrieves the number of lines in the sample program. diff --git a/tests/test_integration.py b/tests/test_integration.py index 09b4240..5643413 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -87,6 +87,23 @@ def test_code(test_repo): assert program.code() == "print('Hello, World!')\n" +@pytest.mark.parametrize( + "language,expected_result", + [ + ("Piet", "png"), + ("Python", ""), + ] +) +def test_image_type(language, expected_result, test_repo): + program: subete.SampleProgram = test_repo[language]["Hello World"] + assert program.image_type() == expected_result + + +def test_project_path(test_repo): + program: subete.SampleProgram = test_repo["Python"]["Hello World"] + assert program.project_path().endswith("/archive/p/python/hello_world.py") + + def test_project_has_test(test_repo): program: subete.SampleProgram = test_repo["Google Apps Script"]["Hello World"] assert program.project().has_testing()