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
# partly from https://hub.docker.com/_/python?tab=description#create-a-dockerfile-in-your-python-app-project
WORKDIR /usr/src/wmn
# copy required source files
COPY wmn/ /usr/local/lib/wmn/wmn
# copy required source file
COPY wmn.py ./
ARG WMN_UID=1000
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
# requires config.yml to be present at build
COPY config.yml ./
RUN chown -R $WMN_UID /run/wmn && chmod 0600 /run/wmn/config.yml
USER $WMN_UID
USER wmn
VOLUME /etc/wmn/config.yml
ENV WMN_CONFIG_PATH=/etc/wmn/config.yml
ARG PORT=3031
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
CMD [ "uwsgi", "--die-on-term", \
"--need-plugin", "python3", \
"--wsgi-file", "/usr/src/wmn/wmn.py", \
"--module", "wmn.wmn", \
"--pythonpath", "/usr/local/lib/wmn", \
"--master", \
"--processes", "1", \
"--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
# 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.
import os
import pathlib
import sys
from typing import Optional, Dict, Any, Tuple
@ -49,13 +50,22 @@ class MatrixException(Exception):
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:
with open("config.yml", "r") as ymlfile:
with open(str(config_path()), "r") as ymlfile:
return yaml.safe_load(ymlfile)
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)

View file

@ -24,9 +24,14 @@ import re
import sys
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.
# Generally match !anything:example.com with unicode support.
@ -42,8 +47,7 @@ async def main():
username: ...
password: "..."
"""
with open("config.yml", "r") as ymlfile:
cfg = yaml.safe_load(ymlfile)
cfg = load_configuration()
parser = argparse.ArgumentParser(description="Notify a matrix channel.")
parser.add_argument(

View file

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