-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #272 from thegridelectric/dev
Publish latest changes on dev
- Loading branch information
Showing
56 changed files
with
5,335 additions
and
2,792 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "gridworks-proactor" | ||
version = "1.0.0" | ||
version = "1.1.0" | ||
description = "Gridworks Proactor" | ||
authors = ["Jessica Millar <[email protected]>"] | ||
license = "MIT" | ||
|
@@ -16,6 +16,9 @@ classifiers = [ | |
"Development Status :: 3 - Alpha", | ||
] | ||
|
||
[tool.poetry.scripts] | ||
gwtest = "gwproactor_test.cli:app" | ||
|
||
[tool.poetry.urls] | ||
Changelog = "https://github.com/thegridelectric/gridworks-proactor/releases" | ||
|
||
|
@@ -26,21 +29,21 @@ python-dotenv = "^1.0.0" | |
xdg = "^6.0.0" | ||
paho-mqtt = "^1.6.1" | ||
result = "^0.9.0" | ||
gridworks-protocol = "^1.0.2" | ||
#gridworks-protocol = {path="../gridworks-protocol", develop=true} | ||
#gridworks-protocol = {git = "https://github.com/thegridelectric/gridworks-protocol.git", branch="jm/report"} | ||
gridworks-protocol = "^1.2.0" | ||
#gridworks-protocol = { path = "../gridworks-protocol", develop = true } | ||
#gridworks-protocol = { git = "https://github.com/thegridelectric/gridworks-protocol.git", branch = "dev" } | ||
aiohttp = "^3.8.5" | ||
yarl = "^1.9.2" | ||
multidict = "^6.0.4" | ||
pytest = {version = ">=7.2.0", optional = true} | ||
pytest-asyncio = {version = ">=0.20.3", optional = true} | ||
gridworks-cert = {version = ">=0.4.2", optional = true} | ||
freezegun = {version = "^1.5.1", optional = true} | ||
pytest = { version = ">=7.2.0", optional = true } | ||
pytest-asyncio = { version = ">=0.20.3", optional = true } | ||
gridworks-cert = { version = ">=0.4.2", optional = true } | ||
freezegun = { version = "^1.5.1", optional = true } | ||
pydantic-settings = "^2.4.0" | ||
|
||
[tool.poetry.group.dev.dependencies] | ||
Pygments = ">=2.10.0" | ||
coverage = {extras = ["toml"], version = ">=6.2"} | ||
coverage = { extras = ["toml"], version = ">=6.2" } | ||
furo = ">=2021.11.12" | ||
mypy = ">=0.930" | ||
pep8-naming = ">=0.12.1" | ||
|
@@ -50,13 +53,13 @@ pyupgrade = ">=2.29.1" | |
sphinx = ">=4.3.2" | ||
sphinx-autobuild = ">=2021.3.14" | ||
sphinx-click = ">=3.0.2" | ||
xdoctest = {extras = ["colors"], version = ">=0.15.10"} | ||
myst-parser = {version = ">=0.16.1"} | ||
xdoctest = { extras = ["colors"], version = ">=0.15.10" } | ||
myst-parser = { version = ">=0.16.1" } | ||
sphinxcontrib-mermaid = "^0.8.1" | ||
ruff = "^0.6.2" | ||
|
||
[tool.pytest.ini_options] | ||
asyncio_default_fixture_loop_scope="function" | ||
asyncio_default_fixture_loop_scope = "function" | ||
|
||
[tool.poetry.extras] | ||
tests = ["pytest", "pytest-asyncio", "gridworks-cert", "freezegun"] | ||
|
@@ -71,7 +74,7 @@ source = ["gwproactor", "gwproactor_test", "tests"] | |
|
||
[tool.coverage.report] | ||
show_missing = false | ||
fail_under = 80 | ||
fail_under = 75 | ||
|
||
[tool.mypy] | ||
strict = true | ||
|
@@ -164,6 +167,7 @@ ignore = [ | |
"C901", # Complexity | ||
"FBT", # Don't care about booleans as positional arguments in tests, e.g. via @pytest.mark.parametrize() | ||
"G004", # Ok to use f-strings in logging in test code | ||
"N806", # We want to be able to have non pep8 variable names. | ||
"PLR0912", # Complexity | ||
"PLR0913", # Complexity | ||
"PLR0915", # Complexity | ||
|
@@ -175,6 +179,7 @@ ignore = [ | |
"C901", # Complexity | ||
"FBT", | ||
"G004", | ||
"N806", # We want to be able to have non pep8 variable names. | ||
"PLR0912", | ||
"PLR0913", | ||
"PLR0915", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
import argparse | ||
import logging | ||
import sys | ||
import traceback | ||
from pathlib import Path | ||
from typing import Optional, Type, TypeVar | ||
|
||
import dotenv | ||
import rich | ||
from pydantic import BaseModel | ||
from pydantic_settings import BaseSettings | ||
|
||
from gwproactor import Proactor, ProactorSettings, setup_logging | ||
from gwproactor.config import MQTTClient | ||
from gwproactor.config.paths import TLSPaths | ||
|
||
LOGGING_FORMAT = "%(asctime)s %(message)s" | ||
|
||
|
||
def missing_tls_paths(paths: TLSPaths) -> list[tuple[str, Optional[Path]]]: | ||
missing = [] | ||
for path_name in paths.model_fields: | ||
path = getattr(paths, path_name) | ||
if path is None or not Path(path).exists(): | ||
missing.append((path_name, path)) | ||
return missing | ||
|
||
|
||
def check_tls_paths_present( | ||
model: BaseModel | BaseSettings, *, raise_error: bool = True | ||
) -> str: | ||
missing_str = "" | ||
for k in model.model_fields: | ||
v = getattr(model, k) | ||
if isinstance(v, MQTTClient) and v.tls.use_tls: | ||
missing_paths = missing_tls_paths(v.tls.paths) | ||
if missing_paths: | ||
missing_str += f"client {k}\n" | ||
for path_name, path in missing_paths: | ||
missing_str += f" {path_name:20s} {path}\n" | ||
if missing_str: | ||
error_str = f"ERROR. TLS usage requested but the following files are missing:\n{missing_str}" | ||
if raise_error: | ||
raise ValueError(error_str) | ||
else: | ||
error_str = "" | ||
return error_str | ||
|
||
|
||
ProactorT = TypeVar("ProactorT", bound=Proactor) | ||
ProactorSettingsT = TypeVar("ProactorSettingsT", bound=ProactorSettings) | ||
|
||
|
||
def get_settings( | ||
*, | ||
settings_type: Optional[Type[ProactorSettingsT]] = None, | ||
settings: Optional[ProactorSettingsT] = None, | ||
env_file: str | Path = ".env", | ||
) -> ProactorSettingsT: | ||
if (settings_type is None and settings is None) or ( | ||
settings_type is not None and settings is not None | ||
): | ||
raise ValueError("ERROR. Specify exactly one of (settings_type, settings)") | ||
if settings_type is not None: | ||
settings = settings_type(_env_file=str(env_file)) | ||
return settings | ||
|
||
|
||
def print_settings( | ||
*, | ||
settings_type: Optional[Type[ProactorSettingsT]] = None, | ||
settings: Optional[ProactorSettingsT] = None, | ||
env_file: str | Path = ".env", | ||
) -> None: | ||
dotenv_file = dotenv.find_dotenv(env_file) | ||
rich.print( | ||
f"Env file: <{dotenv_file}> exists:{env_file and Path(dotenv_file).exists()}" | ||
) | ||
settings = get_settings( | ||
settings_type=settings_type, settings=settings, env_file=dotenv_file | ||
) | ||
rich.print(settings) | ||
missing_tls_paths_ = check_tls_paths_present(settings, raise_error=False) | ||
if missing_tls_paths_: | ||
rich.print(missing_tls_paths_) | ||
|
||
|
||
def get_proactor( # noqa: PLR0913 | ||
name: str, | ||
proactor_type: Type[ProactorT], | ||
*, | ||
settings_type: Optional[Type[ProactorSettingsT]] = None, | ||
settings: Optional[ProactorSettingsT] = None, | ||
dry_run: bool = False, | ||
verbose: bool = False, | ||
message_summary: bool = False, | ||
env_file: str | Path = ".env", | ||
run_in_thread: bool = False, | ||
add_screen_handler: bool = True, | ||
) -> ProactorT: | ||
dotenv_file = dotenv.find_dotenv(env_file) | ||
dotenv_file_debug_str = ( | ||
f"Env file: <{dotenv_file}> exists:{Path(dotenv_file).exists()}" | ||
) | ||
settings = get_settings( | ||
settings_type=settings_type, settings=settings, env_file=dotenv_file | ||
) | ||
if dry_run: | ||
rich.print(dotenv_file_debug_str) | ||
rich.print(settings) | ||
missing_tls_paths_ = check_tls_paths_present(settings, raise_error=False) | ||
if missing_tls_paths_: | ||
rich.print(missing_tls_paths_) | ||
rich.print("Dry run. Doing nothing.") | ||
sys.exit(0) | ||
else: | ||
settings.paths.mkdirs() | ||
args = argparse.Namespace( | ||
verbose=verbose, | ||
message_summary=message_summary, | ||
) | ||
setup_logging(args, settings, add_screen_handler=add_screen_handler) | ||
logger = logging.getLogger( | ||
settings.logging.qualified_logger_names()["lifecycle"] | ||
) | ||
logger.info("") | ||
logger.info(dotenv_file_debug_str) | ||
logger.info("Settings:") | ||
logger.info(settings.model_dump_json(indent=2)) | ||
rich.print(settings) | ||
check_tls_paths_present(settings) | ||
proactor = proactor_type(name=name, settings=settings) | ||
if run_in_thread: | ||
logger.info("run_async_actors_main() starting") | ||
proactor.run_in_thread() | ||
return proactor | ||
|
||
|
||
async def run_async_main( # noqa: PLR0913 | ||
name: str, | ||
proactor_type: Type[ProactorT], | ||
*, | ||
settings_type: Optional[Type[ProactorSettingsT]] = None, | ||
settings: Optional[ProactorSettingsT] = None, | ||
env_file: str | Path = ".env", | ||
dry_run: bool = False, | ||
verbose: bool = False, | ||
message_summary: bool = False, | ||
) -> None: | ||
settings = get_settings( | ||
settings_type=settings_type, | ||
settings=settings, | ||
env_file=dotenv.find_dotenv(env_file), | ||
) | ||
exception_logger = logging.getLogger(settings.logging.base_log_name) | ||
try: | ||
proactor = get_proactor( | ||
name=name, | ||
proactor_type=proactor_type, | ||
settings=settings, | ||
dry_run=dry_run, | ||
verbose=verbose, | ||
message_summary=message_summary, | ||
) | ||
exception_logger = proactor.logger | ||
try: | ||
await proactor.run_forever() | ||
finally: | ||
proactor.stop() | ||
except SystemExit: | ||
pass | ||
except KeyboardInterrupt: | ||
pass | ||
except BaseException as e: | ||
try: | ||
exception_logger.exception( | ||
"ERROR in run_async_actors_main. Shutting down: " "[%s] / [%s]", | ||
e, # noqa: TRY401 | ||
type(e), # noqa: TRY401 | ||
) | ||
except: # noqa: E722 | ||
traceback.print_exception(e) | ||
raise |
Oops, something went wrong.