machine_access_control 0.1.0__tar.gz → 0.2.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (23) hide show
  1. machine_access_control-0.2.3/PKG-INFO +59 -0
  2. machine_access_control-0.2.3/README.rst +29 -0
  3. {machine_access_control-0.1.0 → machine_access_control-0.2.3}/pyproject.toml +35 -13
  4. machine_access_control-0.2.3/src/dm_mac/__init__.py +137 -0
  5. machine_access_control-0.2.3/src/dm_mac/cli_utils.py +38 -0
  6. machine_access_control-0.2.3/src/dm_mac/models/__init__.py +1 -0
  7. machine_access_control-0.2.3/src/dm_mac/models/machine.py +597 -0
  8. machine_access_control-0.2.3/src/dm_mac/models/users.py +214 -0
  9. machine_access_control-0.2.3/src/dm_mac/neongetter.py +418 -0
  10. machine_access_control-0.2.3/src/dm_mac/slack_handler.py +357 -0
  11. machine_access_control-0.2.3/src/dm_mac/utils.py +60 -0
  12. machine_access_control-0.2.3/src/dm_mac/views/api.py +38 -0
  13. machine_access_control-0.2.3/src/dm_mac/views/machine.py +179 -0
  14. machine_access_control-0.2.3/src/dm_mac/views/prometheus.py +211 -0
  15. machine_access_control-0.1.0/PKG-INFO +0 -54
  16. machine_access_control-0.1.0/README.md +0 -34
  17. machine_access_control-0.1.0/src/dm_mac/__init__.py +0 -14
  18. machine_access_control-0.1.0/src/dm_mac/machine_state.py +0 -73
  19. machine_access_control-0.1.0/src/dm_mac/views/api.py +0 -12
  20. machine_access_control-0.1.0/src/dm_mac/views/machine.py +0 -30
  21. {machine_access_control-0.1.0 → machine_access_control-0.2.3}/LICENSE +0 -0
  22. {machine_access_control-0.1.0 → machine_access_control-0.2.3}/src/dm_mac/py.typed +0 -0
  23. {machine_access_control-0.1.0 → machine_access_control-0.2.3}/src/dm_mac/views/__init__.py +0 -0
@@ -0,0 +1,59 @@
1
+ Metadata-Version: 2.1
2
+ Name: machine_access_control
3
+ Version: 0.2.3
4
+ Summary: Decatur Makers Machine Access Control package
5
+ Home-page: https://github.com/jantman/machine_access_control
6
+ License: MIT
7
+ Author: Jason Antman
8
+ Author-email: jason@jasonantman.com
9
+ Requires-Python: >=3.12,<4.0
10
+ Classifier: Development Status :: 3 - Alpha
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3.12
14
+ Classifier: Programming Language :: Python :: 3.13
15
+ Requires-Dist: aiohttp (>=3.11.4,<4.0.0)
16
+ Requires-Dist: asyncio (>=3.4.3,<4.0.0)
17
+ Requires-Dist: filelock (>=3.15.4,<4.0.0)
18
+ Requires-Dist: humanize (>=4.11.0,<5.0.0)
19
+ Requires-Dist: jsonschema (>=4.23.0,<5.0.0)
20
+ Requires-Dist: prometheus-client (>=0.20.0,<0.21.0)
21
+ Requires-Dist: quart (>=0.19.8,<0.20.0)
22
+ Requires-Dist: requests (>=2.32.3,<3.0.0)
23
+ Requires-Dist: slack-bolt (>=1.21.3,<2.0.0)
24
+ Requires-Dist: slack-sdk (>=3.33.5,<4.0.0)
25
+ Project-URL: Changelog, https://github.com/jantman/machine_access_control/releases
26
+ Project-URL: Documentation, https://github.com/jantman/machine_access_control
27
+ Project-URL: Repository, https://github.com/jantman/machine_access_control
28
+ Description-Content-Type: text/x-rst
29
+
30
+ Decatur Makers Machine Access Control (dm-mac)
31
+ ==============================================
32
+
33
+ .. image:: https://www.repostatus.org/badges/latest/concept.svg
34
+ :alt: Project Status: Concept – Minimal or no implementation has been done yet, or the repository is only intended to be a limited example, demo, or proof-of-concept.
35
+ :target: https://www.repostatus.org/#concept
36
+ .. image:: https://github.com/jantman/machine-access-control/actions/workflows/tests.yml/badge.svg
37
+ :target: https://github.com/jantman/machine-access-control/actions/workflows/tests.yml
38
+
39
+ This is a software and hardware project for using RFID cards/fobs to
40
+ control use of various power tools and equipment in the `Decatur
41
+ Makers <https://decaturmakers.org/>`__ makerspace. It is made up of
42
+ custom ESP32-based hardware (machine control units) controlling power to
43
+ each enabled machine and running ESPHome, and a central access
44
+ control/management/logging server application written in Python/Quart.
45
+ Like our `“glue” server <https://github.com/decaturmakers/glue>`__ that
46
+ powers the RFID-based door access control to the makerspace, dm-mac uses
47
+ the `Neon CRM <https://www.neoncrm.com/>`__ as its source for user data,
48
+ though that is completely optional and pluggable.
49
+
50
+ For full documentation, see:
51
+ https://jantman.github.io/machine-access-control/
52
+
53
+ License
54
+ -------
55
+
56
+ Distributed under the terms of the `MIT
57
+ license <https://github.com/jantman/machine_access_control/blob/main/LICENSE>`__,
58
+ *Machine_Access_Control* (``dm_mac``) is free and open source software.
59
+
@@ -0,0 +1,29 @@
1
+ Decatur Makers Machine Access Control (dm-mac)
2
+ ==============================================
3
+
4
+ .. image:: https://www.repostatus.org/badges/latest/concept.svg
5
+ :alt: Project Status: Concept – Minimal or no implementation has been done yet, or the repository is only intended to be a limited example, demo, or proof-of-concept.
6
+ :target: https://www.repostatus.org/#concept
7
+ .. image:: https://github.com/jantman/machine-access-control/actions/workflows/tests.yml/badge.svg
8
+ :target: https://github.com/jantman/machine-access-control/actions/workflows/tests.yml
9
+
10
+ This is a software and hardware project for using RFID cards/fobs to
11
+ control use of various power tools and equipment in the `Decatur
12
+ Makers <https://decaturmakers.org/>`__ makerspace. It is made up of
13
+ custom ESP32-based hardware (machine control units) controlling power to
14
+ each enabled machine and running ESPHome, and a central access
15
+ control/management/logging server application written in Python/Quart.
16
+ Like our `“glue” server <https://github.com/decaturmakers/glue>`__ that
17
+ powers the RFID-based door access control to the makerspace, dm-mac uses
18
+ the `Neon CRM <https://www.neoncrm.com/>`__ as its source for user data,
19
+ though that is completely optional and pluggable.
20
+
21
+ For full documentation, see:
22
+ https://jantman.github.io/machine-access-control/
23
+
24
+ License
25
+ -------
26
+
27
+ Distributed under the terms of the `MIT
28
+ license <https://github.com/jantman/machine_access_control/blob/main/LICENSE>`__,
29
+ *Machine_Access_Control* (``dm_mac``) is free and open source software.
@@ -1,10 +1,10 @@
1
1
  [tool.poetry]
2
2
  name = "machine_access_control"
3
- version = "0.1.0"
3
+ version = "0.2.3"
4
4
  description = "Decatur Makers Machine Access Control package"
5
5
  authors = ["Jason Antman <jason@jasonantman.com>"]
6
6
  license = "MIT"
7
- readme = "README.md"
7
+ readme = "README.rst"
8
8
  homepage = "https://github.com/jantman/machine_access_control"
9
9
  repository = "https://github.com/jantman/machine_access_control"
10
10
  documentation = "https://github.com/jantman/machine_access_control"
@@ -15,14 +15,27 @@ classifiers = [
15
15
  "Development Status :: 3 - Alpha",
16
16
  ]
17
17
 
18
+ [tool.poetry.scripts]
19
+ neongetter = "dm_mac.neongetter:main"
20
+ mac-server = "dm_mac:main"
21
+
18
22
  [tool.poetry.urls]
19
23
  Changelog = "https://github.com/jantman/machine_access_control/releases"
20
24
 
21
25
  [tool.poetry.dependencies]
22
26
  python = "^3.12"
23
- flask = "^3.0.3"
27
+ jsonschema = "^4.23.0"
28
+ requests = "^2.32.3"
29
+ filelock = "^3.15.4"
30
+ prometheus-client = "^0.20.0"
31
+ quart = "^0.19.8"
32
+ asyncio = "^3.4.3"
33
+ slack-bolt = "^1.21.3"
34
+ aiohttp = "^3.11.4"
35
+ slack-sdk = "^3.33.5"
36
+ humanize = "^4.11.0"
24
37
 
25
- [tool.poetry.dev-dependencies]
38
+ [tool.poetry.group.dev.dependencies]
26
39
  Pygments = ">=2.10.0"
27
40
  black = ">=21.10b0"
28
41
  coverage = {extras = ["toml"], version = ">=6.2"}
@@ -44,9 +57,21 @@ pytest = ">=6.2.5"
44
57
  pyupgrade = ">=2.29.1"
45
58
  safety = ">=1.10.3"
46
59
  typeguard = ">=2.13.3"
47
-
48
- [tool.poetry.group.dev.dependencies]
60
+ types-jsonschema = "^4.23.0.20240712"
49
61
  pytest-blockage = "^0.2.4"
62
+ sphinx = "<8.0"
63
+ sphinx-rtd-theme = "^2.0.0"
64
+ responses = "^0.25.3"
65
+ types-requests = "^2.32.0.20240712"
66
+ faker = "^26.3.0"
67
+ pytest-html = "^4.1.1"
68
+ poetry-plugin-export = "^1.8.0"
69
+ freezegun = "^1.5.1"
70
+
71
+ [tool.isort]
72
+ profile = "black"
73
+ force_single_line = true
74
+ lines_after_imports = 2
50
75
 
51
76
  [tool.coverage.paths]
52
77
  source = ["src", "*/site-packages"]
@@ -58,12 +83,7 @@ source = ["dm_mac"]
58
83
 
59
84
  [tool.coverage.report]
60
85
  show_missing = true
61
- fail_under = 100
62
-
63
- [tool.isort]
64
- profile = "black"
65
- force_single_line = true
66
- lines_after_imports = 2
86
+ fail_under = 5
67
87
 
68
88
  [tool.mypy]
69
89
  strict = true
@@ -74,5 +94,7 @@ show_error_codes = true
74
94
  show_error_context = true
75
95
 
76
96
  [build-system]
77
- requires = ["poetry-core>=1.0.0"]
78
97
  build-backend = "poetry.core.masonry.api"
98
+ requires = [
99
+ "poetry-core>=1",
100
+ ]
@@ -0,0 +1,137 @@
1
+ """Decatur Makers Machine Access Control."""
2
+
3
+ import argparse
4
+ import logging
5
+ import os
6
+ import sys
7
+ from asyncio import AbstractEventLoop
8
+ from asyncio import get_event_loop
9
+ from time import time
10
+
11
+ from quart import Quart
12
+ from quart import has_request_context
13
+ from quart import request
14
+ from quart.logging import default_handler
15
+ from slack_bolt.adapter.socket_mode.async_handler import AsyncSocketModeHandler
16
+
17
+ from dm_mac.models.machine import MachinesConfig
18
+ from dm_mac.models.users import UsersConfig
19
+ from dm_mac.slack_handler import SlackHandler
20
+ from dm_mac.utils import set_log_debug
21
+ from dm_mac.utils import set_log_info
22
+ from dm_mac.views.api import api
23
+ from dm_mac.views.machine import machineapi
24
+ from dm_mac.views.prometheus import prometheus_route
25
+
26
+
27
+ logger: logging.Logger = logging.getLogger()
28
+ logging.basicConfig(
29
+ level=logging.WARNING, format="[%(asctime)s %(levelname)s] %(message)s"
30
+ )
31
+
32
+ # BEGIN adding request information to logs
33
+
34
+
35
+ class RequestFormatter(logging.Formatter):
36
+ """Custom log formatter to add request information."""
37
+
38
+ def format(self, record: logging.LogRecord) -> str:
39
+ """Custom log formatter to add request information."""
40
+ if has_request_context():
41
+ record.url = request.url
42
+ record.remote_addr = request.remote_addr
43
+ else:
44
+ record.url = None
45
+ record.remote_addr = None
46
+
47
+ return super().format(record)
48
+
49
+
50
+ formatter = RequestFormatter(
51
+ "%(asctime)s %(levelname)s:[%(remote_addr)s]:%(name)s:%(message)s"
52
+ )
53
+ default_handler.setFormatter(formatter)
54
+
55
+ # END adding request information to logs
56
+
57
+ # enable logging from libraries (i.e. everything but the views)
58
+ logging.getLogger().addHandler(default_handler)
59
+
60
+ logging.getLogger("AUTH").setLevel(logging.INFO)
61
+ logging.getLogger().setLevel(logging.INFO)
62
+
63
+ # see: https://github.com/pallets/flask/issues/4786#issuecomment-1416354177
64
+ api.register_blueprint(machineapi)
65
+
66
+ app: Quart
67
+
68
+
69
+ def asyncio_exception_handler(_, context):
70
+ # get details of the exception
71
+ exception = context["exception"]
72
+ message = context["message"]
73
+ # log exception
74
+ logger.error(f"Task failed, msg={message}, exception={exception}")
75
+
76
+
77
+ def create_app() -> Quart:
78
+ """Factory to create the app."""
79
+ app: Quart = Quart("dm_mac")
80
+ app.config.update({"MACHINES": MachinesConfig()})
81
+ app.config.update({"USERS": UsersConfig()})
82
+ app.config.update({"START_TIME": time()})
83
+ app.config.update({"SLACK_HANDLER": None})
84
+ app.register_blueprint(api)
85
+ app.add_url_rule("/metrics", view_func=prometheus_route)
86
+ return app
87
+
88
+
89
+ def main() -> None:
90
+ global app
91
+ p = argparse.ArgumentParser(description="Run Machine Access Control (MAC) server")
92
+ p.add_argument(
93
+ "-d",
94
+ "--debug",
95
+ dest="debug",
96
+ action="store_true",
97
+ default=False,
98
+ help="Debug mode",
99
+ )
100
+ p.add_argument(
101
+ "-v",
102
+ "--verbose",
103
+ dest="verbose",
104
+ action="store_true",
105
+ default=False,
106
+ help="verbose output",
107
+ )
108
+ p.add_argument(
109
+ "-P",
110
+ "--port",
111
+ dest="port",
112
+ action="store",
113
+ type=int,
114
+ default=5000,
115
+ help="Port number to listen on (default 5000)",
116
+ )
117
+ args = p.parse_args(sys.argv[1:])
118
+ if args.verbose:
119
+ set_log_debug(logger)
120
+ else:
121
+ set_log_info(logger)
122
+ loop: AbstractEventLoop = get_event_loop()
123
+ loop.set_exception_handler(asyncio_exception_handler)
124
+ app = create_app()
125
+ token: str = os.environ.get("SLACK_APP_TOKEN", "").strip()
126
+ if token:
127
+ slack: SlackHandler = SlackHandler(app)
128
+ app.config.update({"SLACK_HANDLER": slack})
129
+ handler = AsyncSocketModeHandler(
130
+ slack.app, os.environ["SLACK_APP_TOKEN"], loop=loop
131
+ )
132
+ loop.create_task(handler.start_async())
133
+ app.run(loop=loop, debug=args.debug, host="0.0.0.0", port=args.port)
134
+
135
+
136
+ if __name__ == "__main__": # pragma: no cover
137
+ main()
@@ -0,0 +1,38 @@
1
+ """Utility functions for command-line tools."""
2
+
3
+ import logging
4
+ import os
5
+
6
+
7
+ def set_log_info(lgr: logging.Logger) -> None:
8
+ """Set logger level to INFO."""
9
+ set_log_level_format(
10
+ lgr, logging.INFO, "%(asctime)s %(levelname)s:%(name)s:%(message)s"
11
+ )
12
+
13
+
14
+ def set_log_debug(lgr: logging.Logger) -> None:
15
+ """Set logger level to DEBUG, and debug-level output format."""
16
+ set_log_level_format(
17
+ lgr,
18
+ logging.DEBUG,
19
+ "%(asctime)s [%(levelname)s %(filename)s:%(lineno)s - "
20
+ "%(name)s.%(funcName)s() ] %(message)s",
21
+ )
22
+
23
+
24
+ def set_log_level_format(lgr: logging.Logger, level: int, fmt: str) -> None:
25
+ """Set logger level and format."""
26
+ formatter = logging.Formatter(fmt=fmt)
27
+ lgr.handlers[0].setFormatter(formatter)
28
+ lgr.setLevel(level)
29
+
30
+
31
+ def env_var_or_die(varname: str, content: str) -> str:
32
+ """Return the value of an env var, or raise exception if not set."""
33
+ try:
34
+ return os.environ[varname]
35
+ except KeyError as ex:
36
+ raise RuntimeError(
37
+ f"ERROR: Please set the {varname} environment variable to {content}."
38
+ ) from ex
@@ -0,0 +1 @@
1
+ """Init for models (empty)."""