Refactor to wmn package/module

This commit is contained in:
Ben 2021-10-28 22:58:37 +02:00
parent 56a5ce3cef
commit 330a259be1
Signed by: ben
GPG key ID: 0F54A7ED232D3319
5 changed files with 38 additions and 22 deletions

View file

@ -3,21 +3,21 @@ MAINTAINER Benedikt Ziemons <ben@rs485.network>
RUN apk add --no-cache uwsgi-python3 python3 py3-yaml py3-flask py3-matrix-nio py3-dateutil RUN apk add --no-cache uwsgi-python3 python3 py3-yaml py3-flask py3-matrix-nio py3-dateutil
# partly from https://hub.docker.com/_/python?tab=description#create-a-dockerfile-in-your-python-app-project # copy required source files
WORKDIR /usr/src/wmn COPY wmn/ /usr/local/lib/wmn/wmn
# copy required source file ARG WMN_UID=1000
COPY wmn.py ./ ARG WMN_GID=1000
WORKDIR /run/wmn RUN mkdir -p /etc/wmn && \
chmod 0700 /etc/wmn && \
chown "${WMN_UID}" /etc/wmn && \
addgroup -g "${WMN_GID}" wmn && \
adduser -s /bin/sh -u "${WMN_UID}" -G wmn -D wmn
ARG WMN_UID=999 USER wmn
VOLUME /etc/wmn/config.yml
# requires config.yml to be present at build ENV WMN_CONFIG_PATH=/etc/wmn/config.yml
COPY config.yml ./
RUN chown -R $WMN_UID /run/wmn && chmod 0600 /run/wmn/config.yml
USER $WMN_UID
ARG PORT=3031 ARG PORT=3031
EXPOSE $PORT EXPOSE $PORT
@ -26,7 +26,8 @@ ENV UWSGI_SOCKET=:$PORT
# opens a uwsgi socket at the given port, which is to be used by a reverse proxy # opens a uwsgi socket at the given port, which is to be used by a reverse proxy
CMD [ "uwsgi", "--die-on-term", \ CMD [ "uwsgi", "--die-on-term", \
"--need-plugin", "python3", \ "--need-plugin", "python3", \
"--wsgi-file", "/usr/src/wmn/wmn.py", \ "--module", "wmn.wmn", \
"--pythonpath", "/usr/local/lib/wmn", \
"--master", \ "--master", \
"--processes", "1", \ "--processes", "1", \
"--threads", "2" ] "--threads", "2" ]

1
wmn/__init__.py Normal file
View file

@ -0,0 +1 @@
__all__ = ["common", "notify", "wmn"]

View file

@ -16,7 +16,8 @@
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import os
import pathlib
import sys import sys
from typing import Optional, Dict, Any, Tuple from typing import Optional, Dict, Any, Tuple
@ -49,13 +50,22 @@ class MatrixException(Exception):
return format_response(self.response) return format_response(self.response)
def config_path() -> pathlib.Path:
path = os.environ.get("WMN_CONFIG_PATH", "./config.yml")
path = pathlib.Path(path)
path = path.absolute().resolve()
if not path.exists():
raise RuntimeError("Cannot find config: " + path)
return path
def load_configuration() -> Cfg: def load_configuration() -> Cfg:
with open("config.yml", "r") as ymlfile: with open(str(config_path()), "r") as ymlfile:
return yaml.safe_load(ymlfile) return yaml.safe_load(ymlfile)
def save_configuration(configuration: Cfg): def save_configuration(configuration: Cfg):
with open("config.yml", "w") as ymlfile: with open(str(config_path()), "w") as ymlfile:
yaml.safe_dump(configuration, ymlfile) yaml.safe_dump(configuration, ymlfile)

View file

@ -24,9 +24,14 @@ import re
import sys import sys
import nio import nio
import yaml
from common import client_login, send_message, resolve_room, MatrixException from .common import (
client_login,
send_message,
resolve_room,
MatrixException,
load_configuration,
)
# Not going to care for specifics like the underscore. # Not going to care for specifics like the underscore.
# Generally match !anything:example.com with unicode support. # Generally match !anything:example.com with unicode support.
@ -42,8 +47,7 @@ async def main():
username: ... username: ...
password: "..." password: "..."
""" """
with open("config.yml", "r") as ymlfile: cfg = load_configuration()
cfg = yaml.safe_load(ymlfile)
parser = argparse.ArgumentParser(description="Notify a matrix channel.") parser = argparse.ArgumentParser(description="Notify a matrix channel.")
parser.add_argument( parser.add_argument(

View file

@ -24,12 +24,12 @@ import traceback
from datetime import datetime from datetime import datetime
from typing import Tuple, Optional, Dict, Any from typing import Tuple, Optional, Dict, Any
import dateutil.parser
import nio import nio
from flask import Flask, request, abort from flask import Flask, request, abort
from werkzeug.datastructures import MultiDict from werkzeug.datastructures import MultiDict
import dateutil.parser
from common import ( from .common import (
client_login, client_login,
send_message, send_message,
Cfg, Cfg,