Skip to content

Commit

Permalink
Fix #9 added ui_api and service.ui_api (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
robnagler authored Jan 2, 2025
1 parent 88614de commit 948d760
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ Learn more at https://github.com/slaclab/slicops.

#### License

License: http://github.com/slaclab/slactwin/LICENSE
License: http://github.com/slaclab/slicops/LICENSE

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.
2 changes: 1 addition & 1 deletion slicops/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
""":mod:`slicops` package
: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/slactwin/LICENSE
:license: http://github.com/slaclab/slicops/LICENSE
"""

import pkg_resources
Expand Down
62 changes: 62 additions & 0 deletions slicops/config.py
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()
19 changes: 18 additions & 1 deletion slicops/pkcli/__init__.py
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()
32 changes: 32 additions & 0 deletions slicops/pkcli/service.py
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,
),
)
69 changes: 69 additions & 0 deletions slicops/quest.py
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
2 changes: 1 addition & 1 deletion slicops/slicops_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
See :mod:`pykern.pkcli` for how this module is used.
: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/slactwin/LICENSE
:license: http://github.com/slaclab/slicops/LICENSE
"""

import pykern.pkcli
Expand Down
23 changes: 23 additions & 0 deletions slicops/ui_api.py
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

0 comments on commit 948d760

Please sign in to comment.