-
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.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
8 changed files
with
207 additions
and
4 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
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,62 @@ | ||
"""Common configuration | ||
:copyright: Copyright (c) 2024 The Board of Trustees of the Leland Stanford Junior University, through SLAC National Accelerator Laboratory (subject to receipt of any required approvals from the U.S. Dept. of Energy). All Rights Reserved. | ||
:license: http://github.com/slaclab/slicops/LICENSE | ||
""" | ||
|
||
from pykern.pkcollections import PKDict | ||
from pykern.pkdebug import pkdc, pkdlog, pkdp | ||
import pykern.pkasyncio | ||
import pykern.pkconfig | ||
import pykern.pkunit | ||
import pykern.util | ||
|
||
_cfg = None | ||
|
||
|
||
def cfg(): | ||
"""`PKDict` of common configuration. | ||
db_api | ||
api_uri, auth_secret, tcp_ip, and tcp_port values. `PKDict` | ||
is compatible with `pykern.http.server_start`, | ||
Returns: | ||
PKDict: configuration values. (Make a copy before modifying.) | ||
""" | ||
return _cfg | ||
|
||
|
||
def dev_path(path, **ensure_kwargs): | ||
"""Prefixes root of the directory for development to `path`. | ||
Returns: | ||
py.path: absolute path with parent directories created | ||
""" | ||
return _dev_root_d.join(path) | ||
|
||
|
||
def _init(): | ||
global _cfg | ||
if _cfg: | ||
return | ||
if pykern.pkconfig.channel_in("dev"): | ||
global _dev_root_d | ||
|
||
_dev_root_d = pykern.util.dev_run_dir(dev_path) | ||
_cfg = pykern.pkconfig.init( | ||
ui_api=PKDict( | ||
api_uri=("/api-v1", str, "URI for API requests"), | ||
auth_secret=pykern.pkconfig.RequiredUnlessDev( | ||
"development_secret", | ||
str, | ||
"secret required to access db_api", | ||
), | ||
tcp_ip=(None, pykern.pkasyncio.cfg_ip, "IP address for server"), | ||
tcp_port=(9030, pykern.pkasyncio.cfg_ip, "port of server"), | ||
), | ||
) | ||
|
||
|
||
_init() |
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 +1,18 @@ | ||
# | ||
"""Base class and common routines for other pkcli's to inherit/use. | ||
:copyright: Copyright (c) 2024 The Board of Trustees of the Leland Stanford Junior University, through SLAC National Accelerator Laboratory (subject to receipt of any required approvals from the U.S. Dept. of Energy). All Rights Reserved. | ||
:license: http://github.com/slaclab/slicops/LICENSE | ||
""" | ||
|
||
from pykern.pkcollections import PKDict | ||
from pykern.pkdebug import pkdc, pkdlog, pkdp | ||
|
||
|
||
class CommandsBase: | ||
"""Common functionality between all pkclis""" | ||
|
||
def quest_start(self): | ||
"""Begin a request which wraps all APIs.""" | ||
from slicops import quest | ||
|
||
return quest.start() |
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,32 @@ | ||
"""Start SlicOps services. | ||
:copyright: Copyright (c) 2024 The Board of Trustees of the Leland Stanford Junior University, through SLAC National Accelerator Laboratory (subject to receipt of any required approvals from the U.S. Dept. of Energy). All Rights Reserved. | ||
:license: http://github.com/slaclab/slicops/LICENSE | ||
""" | ||
|
||
from pykern.pkcollections import PKDict | ||
from pykern.pkdebug import pkdc, pkdlog, pkdp | ||
import slicops.pkcli | ||
|
||
|
||
class Commands(slicops.pkcli.CommandsBase): | ||
|
||
def ui_api(self): | ||
"""Start UI API web server. | ||
This web server provides a friendly and secure API for the | ||
""" | ||
from pykern import http | ||
from slicops import config, ui_api, quest | ||
|
||
http.server_start( | ||
attr_classes=quest.attr_classes(), | ||
api_classes=(ui_api.UIAPI,), | ||
http_config=config.cfg() | ||
.ui_api.copy() | ||
.pkupdate( | ||
# Turn off authentication and version checking | ||
auth_secret=False, | ||
), | ||
) |
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,69 @@ | ||
"""Context for a web or command line operation (think: request object) | ||
A quest is an instance of `API` and contains several attributes (`Attr`): | ||
:copyright: Copyright (c) 2024 The Board of Trustees of the Leland Stanford Junior University, through SLAC National Accelerator Laboratory (subject to receipt of any required approvals from the U.S. Dept. of Energy). All Rights Reserved. | ||
:license: http://github.com/slaclab/slicops/LICENSE | ||
""" | ||
|
||
from pykern.pkcollections import PKDict | ||
from pykern.pkdebug import pkdc, pkdlog, pkdp, pkdexc | ||
import contextlib | ||
import pykern.quest | ||
|
||
_attr_classes = [] | ||
|
||
|
||
def attr_classes(): | ||
"""Classes instantiated automatically on every `start` | ||
Returns: | ||
tuple: class objects | ||
""" | ||
return tuple(_attr_classes) | ||
|
||
|
||
def register_attr(attr_class): | ||
"""Called by modules to add their classes to `attr_classes` | ||
Args: | ||
attr_class (class): implements `pykern.quets.Attr` | ||
""" | ||
if attr_class in _attr_classes: | ||
raise AssertionError(f"duplicate class={attr_class}") | ||
_attr_classes.append(attr_class) | ||
|
||
|
||
def import_and_start(): | ||
"""Import `slicops.modules`, initialize them, and call `start` | ||
Returns: | ||
asyncio.task: instantiated coroutine | ||
""" | ||
from slicops import modules | ||
|
||
modules.import_and_init() | ||
return start() | ||
|
||
|
||
def start(): | ||
"""Calls `pykern.quest.start` with `attr_classes` | ||
Returns: | ||
asyncio.task: instantiated coroutine | ||
""" | ||
from slicops import quest | ||
|
||
return pykern.quest.start(API, _attr_classes) | ||
|
||
|
||
class API(pykern.quest.API): | ||
"""Superclass of quest entry points""" | ||
|
||
pass | ||
|
||
|
||
class Attr(pykern.quest.Attr): | ||
"""Superclass of quest context objects""" | ||
|
||
pass |
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,23 @@ | ||
"""UI API server implementation | ||
:copyright: Copyright (c) 2024 The Board of Trustees of the Leland Stanford Junior University, through SLAC National Accelerator Laboratory (subject to receipt of any required approvals from the U.S. Dept. of Energy). All Rights Reserved. | ||
:license: http://github.com/slaclab/slicops/LICENSE | ||
""" | ||
|
||
from pykern.pkcollections import PKDict | ||
from pykern.pkdebug import pkdc, pkdlog, pkdp | ||
import slicops.quest | ||
|
||
|
||
class UIAPI(slicops.quest.API): | ||
"""API entry points to be dispatched | ||
All entry points take ``api_args``, which is a dictionary of arguments. | ||
Entry points return a `PKDict`, but could be an any Python data structure. | ||
Or, raise a `pykern.quest.APIError`. | ||
""" | ||
|
||
async def api_echo(self, api_args): | ||
return api_args |