Skip to content

Commit

Permalink
Add ability to detect if sample program is an image (#62)
Browse files Browse the repository at this point in the history
* Add ability to detect if sample program is an image

* Add ability to get project path
  • Loading branch information
rzuckerm authored Apr 26, 2023
1 parent 3b9375e commit 8eeb8b5
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
7 changes: 7 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
------

Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
long_description = fh.read()

MAJOR = 0
MINOR = 15
PATCH = 1
MINOR = 16
PATCH = 0

name = "subete"
version = f"{MAJOR}.{MINOR}"
Expand Down
28 changes: 28 additions & 0 deletions subete/repo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import datetime
import imghdr
import logging
import os
import random
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down
17 changes: 17 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 8eeb8b5

Please sign in to comment.