-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update from CompassLabs/Dojo@02fac27
- Loading branch information
Showing
18 changed files
with
691 additions
and
342 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import argparse | ||
import logging | ||
import os | ||
from datetime import timedelta | ||
from typing import Callable, Literal, Optional, Protocol, Union | ||
|
||
from mypy_extensions import NamedArg | ||
from pytimeparse.timeparse import timeparse | ||
|
||
Main = Callable[ | ||
[ | ||
NamedArg(Optional[int], "dashboard_server_port"), | ||
NamedArg(bool, "simulation_status_bar"), | ||
NamedArg(bool, "auto_close"), | ||
NamedArg(Optional[timedelta], "run_length"), | ||
], | ||
None, | ||
] | ||
|
||
from enum import Enum | ||
|
||
|
||
class CliLogLevel(Enum): | ||
CRITICAL = 50 | ||
FATAL = CRITICAL | ||
ERROR = 40 | ||
WARNING = 30 | ||
WARN = WARNING | ||
INFO = 20 | ||
DEBUG = 10 | ||
NOTSET = 0 | ||
|
||
def __str__(self) -> str: | ||
return logging._levelToName[self.value] | ||
|
||
@staticmethod | ||
def of_string(s: str) -> "CliLogLevel": | ||
s = s.upper() | ||
try: | ||
return CliLogLevel[s] | ||
except: | ||
raise Exception(f"{s=} is not a valid log level!") | ||
|
||
|
||
class DojoLogFilter(logging.Filter): | ||
def __init__(self) -> None: | ||
super().__init__("dojo") | ||
|
||
|
||
class NotLogFilter(logging.Filter): | ||
def __init__(self, inner: logging.Filter): | ||
self.inner = inner | ||
|
||
def filter(self, record: logging.LogRecord) -> bool: | ||
return not (self.inner.filter(record)) | ||
|
||
|
||
def run_main(main_f: Main) -> None: | ||
parser = argparse.ArgumentParser(description="Run a Dojo Simulation") | ||
default_dashboard_server_port = 8786 | ||
dashboard_group = parser.add_mutually_exclusive_group() | ||
dashboard_group.add_argument( | ||
"--dashboard-server-port", | ||
type=int, | ||
default=default_dashboard_server_port, | ||
help=f"The port the dashboard should be server on. {default_dashboard_server_port=}", | ||
) | ||
dashboard_group.add_argument("--no-dashboard", action="store_true") | ||
parser.add_argument( | ||
"--log-level", | ||
type=CliLogLevel.of_string, | ||
choices=list(CliLogLevel), | ||
default=CliLogLevel.INFO, | ||
help="log level for dojo", | ||
) | ||
parser.add_argument( | ||
"--global-log-level", | ||
type=CliLogLevel.of_string, | ||
choices=list(CliLogLevel), | ||
default=CliLogLevel.ERROR, | ||
help="log level for all libraries other than dojo", | ||
) | ||
parser.add_argument("--simulation-status-bar", type=bool, default=False) | ||
parser.add_argument("--auto-close", type=bool, default=True) | ||
parser.add_argument( | ||
"--run-length", | ||
type=lambda s: timedelta(seconds=timeparse(s)), | ||
default=None, | ||
help="parsed using pytimeparse (e.g. you can use '1h')", | ||
) | ||
args = parser.parse_args() | ||
|
||
main_handler = logging.StreamHandler() | ||
main_handler.setLevel(args.global_log_level.value) | ||
main_handler.addFilter(NotLogFilter(DojoLogFilter())) | ||
dojo_handler = logging.StreamHandler() | ||
dojo_handler.setLevel(args.log_level.value) | ||
dojo_handler.addFilter(DojoLogFilter()) | ||
logging.basicConfig( | ||
format="%(asctime)s - %(levelname)s - %(message)s", | ||
level=logging.NOTSET, | ||
handlers=[dojo_handler, main_handler], | ||
) | ||
|
||
if args.no_dashboard: | ||
dashboard_server_port: Optional[int] = None | ||
else: | ||
dashboard_server_port = args.dashboard_server_port | ||
|
||
call_args = { | ||
"dashboard_server_port": dashboard_server_port, | ||
"simulation_status_bar": args.simulation_status_bar, | ||
"auto_close": args.auto_close, | ||
"run_length": args.run_length, | ||
} | ||
call_args = { | ||
# let main functions use default values for run_length - we don't want to override them with the 'None' value | ||
k: v | ||
for k, v in call_args.items() | ||
if not (k == "run_length" and v == None) | ||
} | ||
main_f(**call_args) |
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
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
Oops, something went wrong.