Skip to content

Commit

Permalink
Merge pull request #76 from vkottler/dev/1.6.0
Browse files Browse the repository at this point in the history
Initial work on task factories
  • Loading branch information
vkottler authored Jun 26, 2023
2 parents c2b5f4e + 311d5c3 commit f5e9314
Show file tree
Hide file tree
Showing 44 changed files with 870 additions and 366 deletions.
13 changes: 0 additions & 13 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ jobs:
strategy:
matrix:
python-version:
- "3.7"
- "3.8"
- "3.9"
- "3.10"
Expand All @@ -42,29 +41,17 @@ jobs:
- run: pip${{matrix.python-version}} install vmklib>=1.8.0

- run: mk python-sa-types
if: |
matrix.python-version != '3.7'
|| matrix.system != 'windows-latest'

- name: lint and build
run: mk python-lint python-sa yaml python-build-once
env:
PY_LINT_MYPY_EXTRA_ARGS: --no-incremental
if: |
matrix.python-version != '3.7'
|| matrix.system != 'windows-latest'

- run: mk python-test
env:
PY_TEST_EXTRA_ARGS: --cov-report=xml
if: |
matrix.python-version != '3.7'
|| matrix.system != 'windows-latest'

- uses: codecov/codecov-action@main
if: |
matrix.python-version != '3.7'
|| matrix.system != 'windows-latest'

- run: mk pypi-upload-ci
env:
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
=====================================
generator=datazen
version=3.1.2
hash=3bc7656b5df806d21148ec2b5b2c0082
hash=d94e863595f8d7f340e96c0820f3b232
=====================================
-->

# runtimepy ([1.5.4](https://pypi.org/project/runtimepy/))
# runtimepy ([1.6.0](https://pypi.org/project/runtimepy/))

[![python](https://img.shields.io/pypi/pyversions/runtimepy.svg)](https://pypi.org/project/runtimepy/)
![Build Status](https://github.com/vkottler/runtimepy/workflows/Python%20Package/badge.svg)
Expand All @@ -23,7 +23,6 @@ See also: [generated documentation](https://vkottler.github.io/python/pydoc/runt

This package is tested with the following Python minor versions:

* [`python3.7`](https://docs.python.org/3.7/)
* [`python3.8`](https://docs.python.org/3.8/)
* [`python3.9`](https://docs.python.org/3.9/)
* [`python3.10`](https://docs.python.org/3.10/)
Expand Down
2 changes: 1 addition & 1 deletion local/configs/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: A framework for implementing Python services.
entry: {{entry}}

requirements:
- vcorelib>=1.6.2
- vcorelib>=2.0.2
- websockets
- "windows-curses; sys_platform == 'win32'"

Expand Down
2 changes: 1 addition & 1 deletion local/configs/python.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ author_info:
name: Vaughn Kottler
email: [email protected]
username: vkottler
versions: ["3.7", "3.8", "3.9", "3.10", "3.11"]
versions: ["3.8", "3.9", "3.10", "3.11"]

systems:
- macos-latest
Expand Down
4 changes: 2 additions & 2 deletions local/variables/package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
major: 1
minor: 5
patch: 4
minor: 6
patch: 0
entry: runtimepy
5 changes: 2 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ build-backend = "setuptools.build_meta:__legacy__"

[project]
name = "runtimepy"
version = "1.5.4"
version = "1.6.0"
description = "A framework for implementing Python services."
readme = "README.md"
requires-python = ">=3.7"
requires-python = ">=3.8"
authors = [
{name = "Vaughn Kottler", email = "[email protected]"}
]
maintainers = [
{name = "Vaughn Kottler", email = "[email protected]"}
]
classifiers = [
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
Expand Down
4 changes: 2 additions & 2 deletions runtimepy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# =====================================
# generator=datazen
# version=3.1.2
# hash=d02bf554f7d30131decd07af8dbad01f
# hash=f0d184abebfdcfa5ac01de06782820d9
# =====================================

"""
Expand All @@ -10,4 +10,4 @@

DESCRIPTION = "A framework for implementing Python services."
PKG_NAME = "runtimepy"
VERSION = "1.5.4"
VERSION = "1.6.0"
8 changes: 3 additions & 5 deletions runtimepy/commands/tui.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

# third-party
from vcorelib.args import CommandFunction as _CommandFunction
from vcorelib.asyncio import run_handle_interrupt as _run_handle_interrupt
from vcorelib.asyncio import run_handle_stop as _run_handle_stop

# internal
from runtimepy.channel.environment import (
Expand All @@ -29,10 +29,8 @@ def start(window: _CursesWindow, args: _Namespace) -> None:
_ChannelEnvironment(),
max_iterations=args.iterations,
)

eloop = _asyncio.new_event_loop()
_asyncio.set_event_loop(eloop)
_run_handle_interrupt(task.run(window), eloop)
stop_sig = _asyncio.Event()
return _run_handle_stop(stop_sig, task.run(window, stop_sig=stop_sig))


def tui_cmd(args: _Namespace) -> int:
Expand Down
12 changes: 3 additions & 9 deletions runtimepy/data/schemas/ClientConnectionConfig.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
---
type: object
required: [factory, name]
additionalProperties: false
includes:
- has_factory.yaml
- has_name.yaml

properties:
factory:
type: string

name:
type: string

defer:
type: boolean
default: false
Expand Down
5 changes: 5 additions & 0 deletions runtimepy/data/schemas/ConnectionArbiterConfig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ properties:
items:
$ref: package://runtimepy/schemas/ServerConnectionConfig.yaml

tasks:
type: array
items:
$ref: package://runtimepy/schemas/TaskConfig.yaml

# Runtime application or applications.
# defaults to: "runtimepy.net.apps.init_only"
app:
Expand Down
8 changes: 2 additions & 6 deletions runtimepy/data/schemas/ServerConnectionConfig.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
---
type: object
required: [factory]
additionalProperties: false
includes:
- has_factory.yaml

properties:
factory:
type: string

args:
type: array

Expand Down
15 changes: 15 additions & 0 deletions runtimepy/data/schemas/TaskConfig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
includes:
- has_factory.yaml
- has_name.yaml

properties:
period_s:
type: number
default: 1.0
exclusiveMinimum: 0.0

average_depth:
type: integer
default: 10
minimum: 1
8 changes: 8 additions & 0 deletions runtimepy/data/schemas/has_factory.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
type: object
required: [factory]
additionalProperties: false

properties:
factory:
type: string
6 changes: 6 additions & 0 deletions runtimepy/data/schemas/has_name.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
required: [name]

properties:
name:
type: string
8 changes: 8 additions & 0 deletions runtimepy/net/arbiter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,21 @@
ConfigConnectionArbiter as _ConfigConnectionArbiter,
)
from runtimepy.net.arbiter.info import AppInfo, ConnectionMap
from runtimepy.net.arbiter.task import (
ArbiterTask,
ArbiterTaskManager,
TaskFactory,
)

__all__ = [
"AppInfo",
"ArbiterTask",
"ArbiterTaskManager",
"ConnectionArbiter",
"ConnectionMap",
"NetworkApplication",
"init_only",
"TaskFactory",
]


Expand Down
25 changes: 23 additions & 2 deletions runtimepy/net/arbiter/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

# internal
from runtimepy.net.arbiter.info import AppInfo, ConnectionMap
from runtimepy.net.arbiter.task import (
ArbiterTaskManager as _ArbiterTaskManager,
)
from runtimepy.net.connection import Connection as _Connection
from runtimepy.net.manager import ConnectionManager as _ConnectionManager

Expand Down Expand Up @@ -75,6 +78,8 @@ def __init__(
manager = _ConnectionManager()
self.manager = manager

self.task_manager = _ArbiterTaskManager()

if stop_sig is None:
stop_sig = _asyncio.Event()
self.stop_sig = stop_sig
Expand Down Expand Up @@ -187,6 +192,16 @@ async def _entry(
config if config is not None else self._config,
)

# Initialize tasks.
await _asyncio.gather(
*(x.init(info) for x in self.task_manager.tasks)
)

# Start tasks.
await stack.enter_async_context(
self.task_manager.running(stop_sig=self.stop_sig)
)

# Get application methods.
apps = self._apps
if app is not None:
Expand All @@ -197,8 +212,14 @@ async def _entry(
if result == 0:
info.logger = _getLogger(curr_app.__name__)
info.logger.info("Starting.")
result = await curr_app(info)
info.logger.info("Returned %d.", result)
try:
result = await curr_app(info)
info.logger.info("Returned %d.", result)
except AssertionError as exc:
info.logger.exception(
"Failed an assertion:", exc_info=exc
)
result = -1

finally:
for conn in self._connections.values():
Expand Down
12 changes: 12 additions & 0 deletions runtimepy/net/arbiter/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def init(self, data: _JsonObject) -> None:
self.factories: _List[_Any] = data.get("factories", []) # type: ignore
self.clients: _List[_Any] = data.get("clients", []) # type: ignore
self.servers: _List[_Any] = data.get("servers", []) # type: ignore
self.tasks: _List[_Any] = data.get("tasks", []) # type: ignore

self.directory = _Path(str(data.get("directory", ".")))

Expand Down Expand Up @@ -183,6 +184,17 @@ async def process_config(self, config: ConnectionArbiterConfig) -> None:
),
), f"Couldn't register a '{factory}' server!"

# Register tasks.
for task in config.tasks:
name = task["name"]
factory = task["factory"]
assert self.factory_task(
factory,
name,
period_s=task["period_s"],
average_depth=task["average_depth"],
), f"Couldn't register task '{name}' ({factory})!"

# Set the new application entry if it's set.
if config.app is not None:
self.set_app(config.app)
Expand Down
12 changes: 12 additions & 0 deletions runtimepy/net/arbiter/factory/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""
A module implementing an interface extension to the base connection-arbiter so
that methods that create connections can be registered by name.
"""

# internal
from runtimepy.net.arbiter.factory.connection import (
ConnectionFactory,
FactoryConnectionArbiter,
)

__all__ = ["ConnectionFactory", "FactoryConnectionArbiter"]
Loading

0 comments on commit f5e9314

Please sign in to comment.