machine_access_control 0.6.0__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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Jason Antman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,61 @@
1
+ Metadata-Version: 2.4
2
+ Name: machine_access_control
3
+ Version: 0.6.0
4
+ Summary: Decatur Makers Machine Access Control package
5
+ License: MIT
6
+ License-File: LICENSE
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
+ Classifier: Programming Language :: Python :: 3.14
16
+ Requires-Dist: aiohttp (>=3.12.14,<4.0.0)
17
+ Requires-Dist: asyncio (>=3.4.3,<4.0.0)
18
+ Requires-Dist: filelock (>=3.16.1,<4.0.0)
19
+ Requires-Dist: humanize (>=4.11.0,<5.0.0)
20
+ Requires-Dist: jsonschema (>=4.23.0,<5.0.0)
21
+ Requires-Dist: prometheus-client (>=0.20.0,<0.21.0)
22
+ Requires-Dist: quart (>=0.19.8,<0.20.0)
23
+ Requires-Dist: requests (>=2.32.4,<3.0.0)
24
+ Requires-Dist: slack-bolt (>=1.21.3,<2.0.0)
25
+ Requires-Dist: slack-sdk (>=3.33.5,<4.0.0)
26
+ Project-URL: Changelog, https://github.com/jantman/machine_access_control/releases
27
+ Project-URL: Documentation, https://github.com/jantman/machine_access_control
28
+ Project-URL: Homepage, https://github.com/jantman/machine_access_control
29
+ Project-URL: Repository, https://github.com/jantman/machine_access_control
30
+ Description-Content-Type: text/x-rst
31
+
32
+ Decatur Makers Machine Access Control (dm-mac)
33
+ ==============================================
34
+
35
+ .. image:: https://www.repostatus.org/badges/latest/concept.svg
36
+ :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.
37
+ :target: https://www.repostatus.org/#concept
38
+ .. image:: https://github.com/jantman/machine-access-control/actions/workflows/tests.yml/badge.svg
39
+ :target: https://github.com/jantman/machine-access-control/actions/workflows/tests.yml
40
+
41
+ This is a software and hardware project for using RFID cards/fobs to
42
+ control use of various power tools and equipment in the `Decatur
43
+ Makers <https://decaturmakers.org/>`__ makerspace. It is made up of
44
+ custom ESP32-based hardware (machine control units) controlling power to
45
+ each enabled machine and running ESPHome, and a central access
46
+ control/management/logging server application written in Python/Quart.
47
+ Like our `“glue” server <https://github.com/decaturmakers/glue>`__ that
48
+ powers the RFID-based door access control to the makerspace, dm-mac uses
49
+ the `Neon CRM <https://www.neoncrm.com/>`__ as its source for user data,
50
+ though that is completely optional and pluggable.
51
+
52
+ For full documentation, see:
53
+ https://jantman.github.io/machine-access-control/
54
+
55
+ License
56
+ -------
57
+
58
+ Distributed under the terms of the `MIT
59
+ license <https://github.com/jantman/machine_access_control/blob/main/LICENSE>`__,
60
+ *Machine_Access_Control* (``dm_mac``) is free and open source software.
61
+
@@ -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.
@@ -0,0 +1,100 @@
1
+ [tool.poetry]
2
+ name = "machine_access_control"
3
+ version = "0.6.0"
4
+ description = "Decatur Makers Machine Access Control package"
5
+ authors = ["Jason Antman <jason@jasonantman.com>"]
6
+ license = "MIT"
7
+ readme = "README.rst"
8
+ homepage = "https://github.com/jantman/machine_access_control"
9
+ repository = "https://github.com/jantman/machine_access_control"
10
+ documentation = "https://github.com/jantman/machine_access_control"
11
+ packages = [
12
+ { include = "dm_mac", from = "src" },
13
+ ]
14
+ classifiers = [
15
+ "Development Status :: 3 - Alpha",
16
+ ]
17
+
18
+ [tool.poetry.scripts]
19
+ neongetter = "dm_mac.neongetter:main"
20
+ mac-server = "dm_mac:main"
21
+
22
+ [tool.poetry.urls]
23
+ Changelog = "https://github.com/jantman/machine_access_control/releases"
24
+
25
+ [tool.poetry.dependencies]
26
+ python = "^3.12"
27
+ jsonschema = "^4.23.0"
28
+ requests = "^2.32.4"
29
+ filelock = "^3.16.1"
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.12.14"
35
+ slack-sdk = "^3.33.5"
36
+ humanize = "^4.11.0"
37
+
38
+ [tool.poetry.group.dev.dependencies]
39
+ Pygments = ">=2.10.0"
40
+ black = ">=21.10b0"
41
+ coverage = {extras = ["toml"], version = ">=6.2"}
42
+ darglint = ">=1.8.1"
43
+ flake8 = ">=7.1.1"
44
+ flake8-bandit = ">=2.1.2"
45
+ flake8-bugbear = ">=21.9.2"
46
+ flake8-docstrings = ">=1.6.0"
47
+ flake8-rst-docstrings = ">=0.2.5"
48
+ furo = ">=2021.11.12"
49
+ isort = ">=5.10.1"
50
+ mypy = ">=0.930"
51
+ nox = ">=2024.4.15"
52
+ nox-poetry = ">= 1.2.0"
53
+ pep8-naming = ">=0.12.1"
54
+ pre-commit = ">=2.16.0"
55
+ pre-commit-hooks = ">=4.1.0"
56
+ pytest = ">=6.2.5"
57
+ pyupgrade = ">=2.29.1"
58
+ safety = ">=1.10.3"
59
+ typeguard = ">=2.13.3"
60
+ types-jsonschema = "^4.23.0.20240712"
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.9.0"
69
+ freezegun = "^1.5.1"
70
+
71
+ [tool.isort]
72
+ profile = "black"
73
+ force_single_line = true
74
+ lines_after_imports = 2
75
+
76
+ [tool.coverage.paths]
77
+ source = ["src", "*/site-packages"]
78
+ tests = ["tests", "*/tests"]
79
+
80
+ [tool.coverage.run]
81
+ branch = true
82
+ source = ["dm_mac"]
83
+
84
+ [tool.coverage.report]
85
+ show_missing = true
86
+ fail_under = 5
87
+
88
+ [tool.mypy]
89
+ strict = true
90
+ warn_unreachable = true
91
+ pretty = true
92
+ show_column_numbers = true
93
+ show_error_codes = true
94
+ show_error_context = true
95
+
96
+ [build-system]
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)."""