Skip to content

Commit 6b13998

Browse files
add Event
1 parent 176c77e commit 6b13998

File tree

18 files changed

+1104
-0
lines changed

18 files changed

+1104
-0
lines changed

.gitattribute

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
*.apk binary
2+
3+
*.ogg binary
4+
*.mp3 binary
5+
*.wav binary
6+
7+
*.png binary
8+
*.jpg binary
9+
*.gif binary
10+
11+
*.mp4 binary
12+
*.avi binary
13+
*.webm binary
14+
15+
*.ttf binary
16+
*.ttc binary
17+
*.otf binary
18+
19+
*.zip binary
20+
*.7z binary

.github/workflows/python-package.yml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
3+
4+
name: "unittest"
5+
6+
on:
7+
push:
8+
branches: [ main ]
9+
pull_request:
10+
branches: [ main ]
11+
12+
jobs:
13+
noname:
14+
runs-on: ubuntu-latest
15+
strategy:
16+
matrix:
17+
python-version: ['3.10', '3.11', '3.12']
18+
steps:
19+
- uses: actions/checkout@v3
20+
- name: Set up Python ${{ matrix.python-version }}
21+
uses: actions/setup-python@v4
22+
with:
23+
python-version: ${{ matrix.python-version }}
24+
- name: Install dependencies
25+
run: |
26+
python -m pip install --upgrade pip
27+
python -m pip install pytest "asyncgui>=0.6,<0.7"
28+
python -m pip install .
29+
- name: Test with pytest
30+
run: make test

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
__pycache__/
2+
bin/
3+
*.pyc
4+
*.pyo
5+
/.venv/
6+
/.pytest_cache/
7+
/dist
8+
/docs/
9+
/src/asyncgui.py

.vscode/launch.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "pytest",
9+
"type": "debugpy",
10+
"request": "launch",
11+
"module": "pytest",
12+
"args": ["${file}"]
13+
},
14+
{
15+
"name": "Python: Current File",
16+
"type": "debugpy",
17+
"request": "launch",
18+
"program": "${file}",
19+
"console": "integratedTerminal"
20+
}
21+
]
22+
}

.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"python.pythonPath": ".venv/bin/python"
3+
}

LICENSE

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2024 gottadiveintopython
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Makefile

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
test:
2+
python -m pytest ./tests
3+
4+
html:
5+
sphinx-build -b html ./sphinx ./docs
6+
7+
livehtml:
8+
sphinx-autobuild -b html ./sphinx ./docs

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# SyncTools
2+
3+
Inter-task sychronization and communication.
4+
5+
## Installation
6+
7+
Pin the minor version.
8+
9+
```
10+
poetry add asyncgui-ext-synctools@~0.1
11+
pip install "asyncgui-ext-synctools>=0.1,<0.2"
12+
```
13+
14+
## Tested on
15+
16+
- CPython 3.10
17+
- CPython 3.11
18+
- CPython 3.12

poetry.lock

+678
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
[tool.poetry]
2+
name = "asyncgui-ext-synctools"
3+
version = "0.1.0"
4+
description = "Inter-task sychronization and communication."
5+
authors = ["Nattōsai Mitō <[email protected]>"]
6+
license = "MIT"
7+
readme = 'README.md'
8+
repository = 'https://github.com/asyncgui/asyncgui-ext-synctools'
9+
homepage = 'https://github.com/asyncgui/asyncgui-ext-synctools'
10+
keywords = ['async', ]
11+
classifiers=[
12+
'Development Status :: 5 - Production/Stable',
13+
'License :: OSI Approved :: MIT License',
14+
'Intended Audience :: Developers',
15+
'Programming Language :: Python',
16+
'Programming Language :: Python :: 3.10',
17+
'Programming Language :: Python :: 3.11',
18+
'Programming Language :: Python :: 3.12',
19+
'Topic :: Software Development :: Libraries',
20+
'Operating System :: OS Independent',
21+
]
22+
packages = [
23+
{ include = "asyncgui_ext", from = "src" },
24+
]
25+
26+
[tool.poetry.dependencies]
27+
python = "^3.10"
28+
asyncgui = "^0.6"
29+
30+
[tool.poetry.group.dev.dependencies]
31+
pytest = "^8.0"
32+
33+
[tool.poetry.group.doc.dependencies]
34+
sphinx = "^7.2.6"
35+
sphinx-autobuild = "^2021.3.14"
36+
furo = "^2023.9.10"
37+
38+
[build-system]
39+
requires = ["poetry-core"]
40+
build-backend = "poetry.core.masonry.api"
41+
42+
[tool.pytest.ini_options]
43+
xfail_strict = true
44+
addopts = "--maxfail=4 --strict-markers"

sphinx/_static/dummy

Whitespace-only changes.

sphinx/conf.py

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Configuration file for the Sphinx documentation builder.
2+
#
3+
# For the full list of built-in configuration values, see the documentation:
4+
# https://www.sphinx-doc.org/en/master/usage/configuration.html
5+
6+
import importlib.metadata
7+
8+
# -- Project information -----------------------------------------------------
9+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
10+
project = 'asyncgui-ext-synctools'
11+
copyright = '2024, Mitō Nattōsai'
12+
author = 'Mitō Nattōsai'
13+
release = importlib.metadata.version(project)
14+
15+
rst_epilog = """
16+
.. |ja| replace:: 🇯🇵
17+
"""
18+
19+
# -- General configuration ---------------------------------------------------
20+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
21+
extensions = [
22+
'sphinx.ext.autodoc',
23+
'sphinx.ext.intersphinx',
24+
# 'sphinx.ext.viewcode',
25+
'sphinx.ext.githubpages',
26+
# 'sphinx_tabs.tabs',
27+
28+
]
29+
templates_path = ['_templates']
30+
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
31+
language = 'en'
32+
add_module_names = False
33+
gettext_auto_build = False
34+
gettext_location = False
35+
36+
37+
# -- Options for HTML output -------------------------------------------------
38+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
39+
html_theme = "furo"
40+
html_static_path = ['_static']
41+
42+
# -- Options for todo extension ----------------------------------------------
43+
# https://www.sphinx-doc.org/en/master/usage/extensions/todo.html#configuration
44+
todo_include_todos = True
45+
46+
47+
# -- Options for intersphinx extension ---------------------------------------
48+
# https://www.sphinx-doc.org/en/master/usage/extensions/intersphinx.html#configuration
49+
intersphinx_mapping = {
50+
'python': ('https://docs.python.org/3', None),
51+
'asyncgui': ('https://asyncgui.github.io/asyncgui/', None),
52+
}
53+
54+
55+
# -- Options for autodoc extension ---------------------------------------
56+
# https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#configuration
57+
# autodoc_mock_imports = ['pygame', ]
58+
autodoc_default_options = {
59+
# 'members': True,
60+
# 'undoc-members': True,
61+
'no-show-inheritance': True,
62+
}
63+
64+
65+
# -- Options for tabs extension ---------------------------------------
66+
# https://sphinx-tabs.readthedocs.io/en/latest/
67+
sphinx_tabs_disable_tab_closing = True
68+

sphinx/index.rst

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=========
2+
SyncTools
3+
=========
4+
5+
Inter-task sychronization and communication.
6+
7+
.. toctree::
8+
:hidden:
9+
10+
reference

sphinx/reference.rst

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
=============
2+
API Reference
3+
=============
4+
5+
6+
.. autoclass:: asyncgui_ext.synctools.event.Event
7+
:members:
8+
:undoc-members:
9+
:exclude-members:

src/asyncgui_ext/synctools/__init__.py

Whitespace-only changes.

src/asyncgui_ext/synctools/all.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
__all__ = (
2+
'Event',
3+
)
4+
from .event import Event

src/asyncgui_ext/synctools/event.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
__all__ = (
2+
'Event',
3+
)
4+
import types
5+
import typing as T
6+
7+
8+
class Event:
9+
'''
10+
Similar to :class:`asyncio.Event`.
11+
The differences are:
12+
13+
* :meth:`set` accepts any number of arguments but doesn't use them at all so it can be used as a callback function
14+
in any library.
15+
* :attr:`is_set` is a property not a function.
16+
17+
.. code-block::
18+
19+
e = Event()
20+
any_library.register_callback(e.set)
21+
'''
22+
23+
__slots__ = ('_flag', '_waiting_tasks', )
24+
25+
def __init__(self):
26+
self._flag = False
27+
self._waiting_tasks = []
28+
29+
@property
30+
def is_set(self) -> bool:
31+
return self._flag
32+
33+
def set(self, *args, **kwargs):
34+
'''
35+
Set the event.
36+
Unlike asyncio's, all tasks waiting for this event to be set will be resumed *immediately*.
37+
'''
38+
if self._flag:
39+
return
40+
self._flag = True
41+
tasks = self._waiting_tasks
42+
self._waiting_tasks = []
43+
for t in tasks:
44+
if t is not None:
45+
t._step()
46+
47+
def clear(self):
48+
'''Unset the event.'''
49+
self._flag = False
50+
51+
@types.coroutine
52+
def wait(self) -> T.Awaitable:
53+
'''
54+
Wait for the event to be set.
55+
Return *immediately* if it's already set.
56+
'''
57+
if self._flag:
58+
return
59+
try:
60+
tasks = self._waiting_tasks
61+
idx = len(tasks)
62+
yield tasks.append
63+
finally:
64+
tasks[idx] = None

0 commit comments

Comments
 (0)