From 9164a3ed3a4dd15bf324c069726e33362b09e9a0 Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Sun, 8 Dec 2019 17:15:48 +0100 Subject: [PATCH] restructure project for packaging --- debian/openwebrx.install | 1 + openwebrx.py | 64 ++-------------------------------------- owrx/__main__.py | 60 +++++++++++++++++++++++++++++++++++++ owrx/config.py | 26 +++++++++++----- owrx/service.py | 2 +- owrx/source.py | 2 +- sdrhu.py | 2 +- setup.py | 10 +++++-- 8 files changed, 92 insertions(+), 75 deletions(-) create mode 100644 debian/openwebrx.install create mode 100644 owrx/__main__.py diff --git a/debian/openwebrx.install b/debian/openwebrx.install new file mode 100644 index 0000000..ff6bc97 --- /dev/null +++ b/debian/openwebrx.install @@ -0,0 +1 @@ +config_webrx.py etc/ \ No newline at end of file diff --git a/openwebrx.py b/openwebrx.py index 97387df..4232fae 100755 --- a/openwebrx.py +++ b/openwebrx.py @@ -1,66 +1,6 @@ #!/usr/bin/env python3 -from http.server import HTTPServer -from owrx.http import RequestHandler -from owrx.config import PropertyManager -from owrx.feature import FeatureDetector -from owrx.source import SdrService -from socketserver import ThreadingMixIn -from owrx.sdrhu import SdrHuUpdater -from owrx.service import Services -from owrx.websocket import WebSocketConnection -from owrx.pskreporter import PskReporter - -import logging - -logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s") - - -class ThreadedHttpServer(ThreadingMixIn, HTTPServer): - pass - - -def main(): - print( - """ - -OpenWebRX - Open Source SDR Web App for Everyone! | for license see LICENSE file in the package -_________________________________________________________________________________________________ - -Author contact info: Andras Retzler, HA7ILM -Author contact info: Jakob Ketterl, DD5JFK - - """ - ) - - pm = PropertyManager.getSharedInstance().loadConfig("config_webrx") - - featureDetector = FeatureDetector() - if not featureDetector.is_available("core"): - print( - "you are missing required dependencies to run openwebrx. " - "please check that the following core requirements are installed:" - ) - print(", ".join(featureDetector.get_requirements("core"))) - return - - # Get error messages about unknown / unavailable features as soon as possible - SdrService.loadProps() - - if "sdrhu_key" in pm and pm["sdrhu_public_listing"]: - updater = SdrHuUpdater() - updater.start() - - Services.start() - - server = ThreadedHttpServer(("0.0.0.0", pm.getPropertyValue("web_port")), RequestHandler) - server.serve_forever() - +from owrx.__main__ import main if __name__ == "__main__": - try: - main() - except KeyboardInterrupt: - WebSocketConnection.closeAll() - Services.stop() - PskReporter.stop() + main() diff --git a/owrx/__main__.py b/owrx/__main__.py new file mode 100644 index 0000000..499731d --- /dev/null +++ b/owrx/__main__.py @@ -0,0 +1,60 @@ +from http.server import HTTPServer +from owrx.http import RequestHandler +from owrx.config import PropertyManager +from owrx.feature import FeatureDetector +from owrx.source import SdrService +from socketserver import ThreadingMixIn +from owrx.sdrhu import SdrHuUpdater +from owrx.service import Services +from owrx.websocket import WebSocketConnection +from owrx.pskreporter import PskReporter + +import logging + +logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s") + + +class ThreadedHttpServer(ThreadingMixIn, HTTPServer): + pass + + +def main(): + print( + """ + +OpenWebRX - Open Source SDR Web App for Everyone! | for license see LICENSE file in the package +_________________________________________________________________________________________________ + +Author contact info: Andras Retzler, HA7ILM +Author contact info: Jakob Ketterl, DD5JFK + + """ + ) + + pm = PropertyManager.getSharedInstance().loadConfig() + + featureDetector = FeatureDetector() + if not featureDetector.is_available("core"): + print( + "you are missing required dependencies to run openwebrx. " + "please check that the following core requirements are installed:" + ) + print(", ".join(featureDetector.get_requirements("core"))) + return + + # Get error messages about unknown / unavailable features as soon as possible + SdrService.loadProps() + + if "sdrhu_key" in pm and pm["sdrhu_public_listing"]: + updater = SdrHuUpdater() + updater.start() + + Services.start() + + try: + server = ThreadedHttpServer(("0.0.0.0", pm.getPropertyValue("web_port")), RequestHandler) + server.serve_forever() + except KeyboardInterrupt: + WebSocketConnection.closeAll() + Services.stop() + PskReporter.stop() diff --git a/owrx/config.py b/owrx/config.py index d8b6ad2..ac287cc 100644 --- a/owrx/config.py +++ b/owrx/config.py @@ -1,3 +1,4 @@ +import importlib.util import logging logger = logging.getLogger(__name__) @@ -50,6 +51,10 @@ class Property(object): return self +class ConfigNotFoundException(Exception): + pass + + class PropertyManager(object): sharedInstance = None @@ -128,10 +133,17 @@ class PropertyManager(object): p.setValue(other_pm[key]) return self - def loadConfig(self, filename): - cfg = __import__(filename) - for name, value in cfg.__dict__.items(): - if name.startswith("__"): - continue - self[name] = value - return self + def loadConfig(self): + for file in ["/etc/config_webrx.py", "./config_webrx.py"]: + try: + spec = importlib.util.spec_from_file_location("config_webrx", file) + cfg = importlib.util.module_from_spec(spec) + spec.loader.exec_module(cfg) + for name, value in cfg.__dict__.items(): + if name.startswith("__"): + continue + self[name] = value + return self + except FileNotFoundError: + logger.debug("not found: %s", file) + raise ConfigNotFoundException("no usable config found! please make sure you have a valid configuration file!") diff --git a/owrx/service.py b/owrx/service.py index db9d65b..4e68200 100644 --- a/owrx/service.py +++ b/owrx/service.py @@ -3,7 +3,7 @@ from owrx.socket import getAvailablePort from datetime import datetime, timezone, timedelta from owrx.source import SdrService, SdrSource from owrx.bands import Bandplan -from csdr import dsp, output +from csdr.csdr import dsp, output from owrx.wsjt import WsjtParser from owrx.aprs import AprsParser from owrx.config import PropertyManager diff --git a/owrx/source.py b/owrx/source.py index d18398e..a048c83 100644 --- a/owrx/source.py +++ b/owrx/source.py @@ -7,7 +7,7 @@ from owrx.aprs import AprsParser from owrx.metrics import Metrics, DirectMetric from owrx.socket import getAvailablePort import threading -import csdr +from csdr import csdr import time import os import signal diff --git a/sdrhu.py b/sdrhu.py index a194e02..d74d166 100755 --- a/sdrhu.py +++ b/sdrhu.py @@ -25,7 +25,7 @@ from owrx.sdrhu import SdrHuUpdater from owrx.config import PropertyManager if __name__ == "__main__": - pm = PropertyManager.getSharedInstance().loadConfig("config_webrx") + pm = PropertyManager.getSharedInstance().loadConfig() if not "sdrhu_key" in pm: exit(1) diff --git a/setup.py b/setup.py index 12c1562..025a3b5 100644 --- a/setup.py +++ b/setup.py @@ -1,11 +1,14 @@ -from setuptools import setup, find_packages +from setuptools import setup, find_namespace_packages from owrx.version import strictversion setup( name="OpenWebRX", version=str(strictversion), - packages=find_packages(), - entry_points={"console_scripts": ["openwebrx=openwebrx:main"]}, + packages=find_namespace_packages(include=["owrx", "csdr", "htdocs"]), + package_data={ + "htdocs": ["*", "**/*"] + }, + entry_points={"console_scripts": ["openwebrx=owrx.__main__:main"]}, # use the github page for now url="https://github.com/jketterl/openwebrx", author="AndrĂ¡s Retzler, Jakob Ketterl", @@ -13,4 +16,5 @@ setup( maintainer="Jakob Ketterl", maintainer_email="jakob.ketterl@gmx.de", license="GAGPL", + python_requires=">=3.6", )