Skip to content

Commit

Permalink
Merge branch 'main' into pr/saraaldosari/307
Browse files Browse the repository at this point in the history
  • Loading branch information
lordmauve committed Jan 19, 2025
2 parents 71d4535 + 61cc03d commit f565279
Show file tree
Hide file tree
Showing 40 changed files with 141 additions and 81 deletions.
5 changes: 0 additions & 5 deletions .flake8

This file was deleted.

8 changes: 5 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: "Run tests"
on: [push, pull_request]
on: [pull_request, workflow_call]
jobs:
build:
runs-on: ubuntu-latest
Expand All @@ -13,12 +13,14 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt -r requirements-dev.txt
- name: Install project
run: pip install .
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names in the entire repo
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# stricter tests for mission critical code
flake8 --count pgzero pgzrun.py test
flake8 --count src test
- name: Install xvfb and pulseaudio
run: |
Expand All @@ -27,7 +29,7 @@ jobs:
run: |
export XDG_RUNTIME_DIR="$RUNNER_TEMP"
pulseaudio -D --start
xvfb-run --auto-servernum python setup.py test
xvfb-run --auto-servernum pytest
- name: Cleanup xvfb pidx
uses: bcomnes/cleanup-xvfb@v1
- uses: actions/upload-artifact@master
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ examples_dev
/venv*/
/notebooks
/test/failed-image
__pycache__
src/pgzero/_version.py
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ repos:
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/PyCQA/flake8
rev: "4.0.1"
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.9.2"
hooks:
- id: flake8
- id: ruff
exclude: "^(doc|examples)/"
5 changes: 0 additions & 5 deletions MANIFEST.in

This file was deleted.

2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

.. image:: https://img.shields.io/github/workflow/status/lordmauve/pgzero/Run%20tests/master
.. image:: https://img.shields.io/github/actions/workflow/status/lordmauve/pgzero/test.yml?branch=main
:target: https://github.com/lordmauve/pgzero/actions/workflows/test.yml
:alt: GitHub Test Status

Expand Down
31 changes: 31 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[project]
name = "pgzero"
description = "A zero-boilerplate 2D games framework"
readme = "README.rst"
requires-python = ">=3.6"
dependencies = [
"pygame>=2.1",
"numpy",
"pyfxr",
]
dynamic = ["version"]

[project.urls]
Documentation = "https://pygame-zero.readthedocs.io/"
Source = "https://github.com/lordmauve/pgzero"

[project.scripts]
pgzrun = "pgzero.runner:main"

[build-system]
requires = ["setuptools>=64", "setuptools-scm>=8"]
build-backend = "setuptools.build_meta"

[tool.setuptools_scm]
write_to = "src/pgzero/_version.py"

[tool.ruff]
builtins = [
"Actor", "Rect", "ZRect", "animate", "clock", "exit", "images", "keyboard", "keymods",
"keys", "mouse", "music", "screen", "sounds", "storage", "tone"
]
2 changes: 0 additions & 2 deletions setup.cfg

This file was deleted.

43 changes: 0 additions & 43 deletions setup.py

This file was deleted.

4 changes: 3 additions & 1 deletion pgzero/__init__.py → src/pgzero/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
"""

__version__ = '1.3.dev0'
from ._version import __version__

__all__ = ['__version__']
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
6 changes: 5 additions & 1 deletion pgzero/loaders.py → src/pgzero/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,11 @@ def _load(self, path):
try:
return pygame.mixer.Sound(path)
except pygame.error as err:
if not err.args[0].startswith('Unable to open file'):
if not err.args[0].startswith((
'Unable to open file',
'Unknown WAVE format tag',
'MPEG formats not supported',
)):
raise
from .soundfmt import identify
try:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
61 changes: 53 additions & 8 deletions pgzero/runner.py → src/pgzero/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,30 +69,75 @@ def main():
help="Print periodic FPS measurements on the terminal."
)
parser.add_argument(
'program',
help="The Pygame Zero program to run."
'game',
help="The Pygame Zero game to run (a Python file or directory)."
)
args = parser.parse_args()

if __debug__:
warnings.simplefilter('default', DeprecationWarning)

load_and_run(args.program, fps=args.fps)
try:
load_and_run(args.game, fps=args.fps)
except NoMainModule as e:
sys.exit(e)


class NoMainModule(Exception):
"""Indicate that we couldn't find a main module to run."""


def load_and_run(path, *, fps: bool = False):
"""Load and run the given Python file as the main PGZero game module.
"""Load and run the given Python file or directory.
If a file, run this as the main PGZero game module.
If a directory, run the first file inside the directory containing:
* `__main__.py`
* `main.py`
* `run_game.py`
* `<name>.py` where `<name>` is the basename of the directory
Note that the 'import pgzrun' IDE mode doesn't pass through this entry
point, as the module is already loaded.
"""
with open(path, 'rb') as f:
src = f.read()
path = path.rstrip(os.sep)
try:
with open(path, 'rb') as f:
src = f.read()
except FileNotFoundError:
raise NoMainModule(f"Error: {path} does not exist.")
except IsADirectoryError:
name = os.path.basename(path)
for candidate in (
'__main__.py',
'main.py',
'run_game.py',
f'{name}.py'
):
try:
with open(os.path.join(path, candidate), 'rb') as f:
src = f.read()
break
except FileNotFoundError:
pass
else:
raise NoMainModule(f"""\
Error: {path} is a directory.
To run a directory with pgzrun, it must contain a file named __main__.py,
main.py, run_game.py, or a .py file with the same name as the directory.
You can also run a specific file with:
pgzrun path/to/your/file.py
""")
else:
name, _ = os.path.splitext(os.path.basename(path))

code = compile(src, os.path.basename(path), 'exec', dont_inherit=True)

name, _ = os.path.splitext(os.path.basename(path))
mod = ModuleType(name)
mod.__file__ = path
mod.__name__ = name
Expand Down
2 changes: 1 addition & 1 deletion pgzero/screen.py → src/pgzero/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def rect(self, rect, color, width=1):
c = make_color(color)

hw = width / 2
l, t, w, h = rect
l, t, w, h = rect # noqa: E741
l1, l2 = round(l - hw), round(l + hw)
r1, r2 = round(l + w - hw), round(l + w + hw)
t1, t2 = round(t - hw), round(t + hw)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file modified test/expected-image/circle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/expected-image/filled_polygon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/expected-image/wrapped_gradient_text.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions test/game_tests/blue/run_game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def draw():
screen.fill('blue')
2 changes: 2 additions & 0 deletions test/game_tests/green/green.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def draw():
screen.fill('green')
2 changes: 2 additions & 0 deletions test/game_tests/pink/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def draw():
screen.fill('pink')
2 changes: 2 additions & 0 deletions test/game_tests/red/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def draw():
screen.fill('red')
30 changes: 24 additions & 6 deletions test/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,32 @@
class RunnerTest(unittest.TestCase):
"""Test that we can load and run the current file."""

def test_run(self):
"""We can load and run a game saved as UTF-8."""
def assert_runnable(self, path: Path):
"""Check that we can run the given file."""
clock.schedule_unique(sys.exit, 0.05)
with self.assertRaises(SystemExit):
load_and_run(str(game_tests / 'utf8.py'))
load_and_run(str(path))

def test_run_utf8(self):
"""We can load and run a game saved as UTF-8."""
self.assert_runnable(game_tests / 'utf8.py')

def test_import(self):
"""Games can import other modules, which can acccess the builtins."""
clock.schedule_unique(sys.exit, 0.05)
with self.assertRaises(SystemExit):
load_and_run(str(game_tests / 'importing.py'))
self.assert_runnable(game_tests / 'importing.py')

def test_run_directory_dunder_main(self):
"""We can run a directory containing __main__.py"""
self.assert_runnable(game_tests / 'red')

def test_run_directory_main(self):
"""We can run a directory containing main.py"""
self.assert_runnable(game_tests / 'pink')

def test_run_directory_ane_name(self):
"""We can run a directory containing <basename>.py"""
self.assert_runnable(game_tests / 'green')

def test_run_directory_run_game(self):
"""We can run a directory containing run_game.py"""
self.assert_runnable(game_tests / 'blue')
7 changes: 5 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ envlist = py36, py37, py38, py39, py310, flake8
[testenv:flake8]
basepython=python
deps=flake8
commands=flake8 pgzero pgzrun.py test examples
commands=flake8 src test examples

[testenv]
passenv=
Expand All @@ -17,4 +17,7 @@ commands =
py.test --cov=pgzero --basetemp={envtmpdir}

[flake8]
builtins = Actor,Rect,ZRect,animate,clock,exit,images,keyboard,keymods,keys,mouse,music,screen,sounds,storage,tone
max-line-length = 88
builtins =
Actor, Rect, ZRect, animate, clock, exit, images, keyboard, keymods,
keys, mouse, music, screen, sounds, storage, tone

0 comments on commit f565279

Please # to comment.