From c7f7405a1b71efff5391d009c315caffffaed32e Mon Sep 17 00:00:00 2001 From: txoof Date: Fri, 9 Feb 2024 18:10:38 +0100 Subject: [PATCH] improve logging for command line options --- paperpi/paperpi.ipynb | 80 ++++++++++--- paperpi/paperpi.py | 64 ++++++++--- paperpi/plugins/lms_client/lms_client.ipynb | 121 ++++++++------------ paperpi/plugins/lms_client/lms_client.py | 54 +++------ 4 files changed, 171 insertions(+), 148 deletions(-) diff --git a/paperpi/paperpi.ipynb b/paperpi/paperpi.ipynb index fd6af0c..5509c0f 100644 --- a/paperpi/paperpi.ipynb +++ b/paperpi/paperpi.ipynb @@ -665,7 +665,12 @@ " \n", " # convert all config values to int, float, etc.\n", " config = config_str_to_val(config)\n", + "\n", + " log_level = config['main'].get('log_level', 'INFO')\n", " \n", + " logger.setLevel(log_level)\n", + " logging.root.setLevel(log_level) \n", + " \n", " if cmd_args.options.version:\n", " print(constants.VERSION_STRING)\n", " return\n", @@ -675,17 +680,17 @@ " Screen.list_compatible()\n", " return\n", " \n", - " if cmd_args.options.list_plugins:\n", - " get_help.get_help(plugin_path=Path(constants.BASE_DIRECTORY)/'plugins')\n", - " return\n", + " # if cmd_args.options.list_plugins:\n", + " # get_help.get_help(plugin_path=Path(constants.BASE_DIRECTORY)/'plugins')\n", + " # return\n", " \n", - " if cmd_args.options.plugin_info:\n", - " get_help.get_help(cmd_args.options.plugin_info)\n", - " return\n", + " # if cmd_args.options.plugin_info:\n", + " # get_help.get_help(cmd_args.options.plugin_info)\n", + " # return\n", " \n", - " if cmd_args.options.run_plugin_func:\n", - " run_module.run_module(cmd_args.options.run_plugin_func)\n", - " return \n", + " # if cmd_args.options.run_plugin_func:\n", + " # run_module.run_module(cmd_args.options.run_plugin_func)\n", + " # return \n", "\n", " if cmd_args.options.add_config:\n", " try:\n", @@ -707,14 +712,26 @@ " \n", " log_level = config['main'].get('log_level', 'INFO')\n", "\n", + " if cmd_args.options.list_plugins:\n", + " get_help.get_help(plugin_path=Path(constants.BASE_DIRECTORY)/'plugins')\n", + " return\n", + " \n", + " if cmd_args.options.plugin_info:\n", + " get_help.get_help(cmd_args.options.plugin_info)\n", + " return\n", + " \n", + " if cmd_args.options.run_plugin_func:\n", + " run_module.run_module(cmd_args.options.run_plugin_func)\n", + " return \n", + "\n", + "\n", + " \n", " logger.info(f'********** {constants.APP_NAME} {constants.VERSION} Starting **********')\n", " if cmd_args.options.main__daemon:\n", " logger.info(f'{constants.APP_NAME} is running in daemon mode')\n", " else:\n", " logger.info(f'{constants.APP_NAME} is running in on-demand mode')\n", - " \n", - " logger.setLevel(log_level)\n", - " logging.root.setLevel(log_level)\n", + "\n", "\n", " \n", " logger.debug(f'configuration:\\n{config}\\n\\n')\n", @@ -775,6 +792,18 @@ " return exit_code" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "5202f66c-fa4d-4bc2-a376-4ace6fe6610c", + "metadata": {}, + "outputs": [], + "source": [ + "# add test arguments here \n", + "# test_args = [\"--run_plugin_func\", \"lms_client.scan_servers\"]\n", + "test_args = []" + ] + }, { "cell_type": "code", "execution_count": null, @@ -784,15 +813,32 @@ "source": [ "if __name__ == \"__main__\":\n", " # remove jupyter runtime junk for testing\n", - " try:\n", + "\n", + " if '-f' in sys.argv:\n", " i = sys.argv.index('-f')\n", - " t = sys.argv[:i] + sys.argv[i+2:]\n", - " sys.argv = t\n", - " except ValueError:\n", - " pass\n", + " if 'jupyter/' in sys.argv[i+1]:\n", + " try:\n", + " t = sys.argv[:i] + sys.argv[i+2:]\n", + " sys.argv = t\n", + " except ValueError:\n", + " pass\n", + " for i in test_args:\n", + " if i not in sys.argv:\n", + " sys.argv.append(i)\n", + " \n", " exit_code = main()\n", " sys.exit(exit_code)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "110c373b-ec05-4344-b3af-f73c0d7b5d97", + "metadata": {}, + "outputs": [], + "source": [ + "sys.argv" + ] } ], "metadata": { diff --git a/paperpi/paperpi.py b/paperpi/paperpi.py index 004e884..b043391 100755 --- a/paperpi/paperpi.py +++ b/paperpi/paperpi.py @@ -569,7 +569,12 @@ def main(): # convert all config values to int, float, etc. config = config_str_to_val(config) + + log_level = config['main'].get('log_level', 'INFO') + logger.setLevel(log_level) + logging.root.setLevel(log_level) + if cmd_args.options.version: print(constants.VERSION_STRING) return @@ -579,17 +584,17 @@ def main(): Screen.list_compatible() return - if cmd_args.options.list_plugins: - get_help.get_help(plugin_path=Path(constants.BASE_DIRECTORY)/'plugins') - return + # if cmd_args.options.list_plugins: + # get_help.get_help(plugin_path=Path(constants.BASE_DIRECTORY)/'plugins') + # return - if cmd_args.options.plugin_info: - get_help.get_help(cmd_args.options.plugin_info) - return + # if cmd_args.options.plugin_info: + # get_help.get_help(cmd_args.options.plugin_info) + # return - if cmd_args.options.run_plugin_func: - run_module.run_module(cmd_args.options.run_plugin_func) - return + # if cmd_args.options.run_plugin_func: + # run_module.run_module(cmd_args.options.run_plugin_func) + # return if cmd_args.options.add_config: try: @@ -611,14 +616,26 @@ def main(): log_level = config['main'].get('log_level', 'INFO') + if cmd_args.options.list_plugins: + get_help.get_help(plugin_path=Path(constants.BASE_DIRECTORY)/'plugins') + return + + if cmd_args.options.plugin_info: + get_help.get_help(cmd_args.options.plugin_info) + return + + if cmd_args.options.run_plugin_func: + run_module.run_module(cmd_args.options.run_plugin_func) + return + + + logger.info(f'********** {constants.APP_NAME} {constants.VERSION} Starting **********') if cmd_args.options.main__daemon: logger.info(f'{constants.APP_NAME} is running in daemon mode') else: logger.info(f'{constants.APP_NAME} is running in on-demand mode') - - logger.setLevel(log_level) - logging.root.setLevel(log_level) + logger.debug(f'configuration:\n{config}\n\n') @@ -679,13 +696,26 @@ def main(): return exit_code # - +# add test arguments here +# test_args = ["--run_plugin_func", "lms_client.scan_servers"] +test_args = [] + if __name__ == "__main__": # remove jupyter runtime junk for testing - try: + + if '-f' in sys.argv: i = sys.argv.index('-f') - t = sys.argv[:i] + sys.argv[i+2:] - sys.argv = t - except ValueError: - pass + if 'jupyter/' in sys.argv[i+1]: + try: + t = sys.argv[:i] + sys.argv[i+2:] + sys.argv = t + except ValueError: + pass + for i in test_args: + if i not in sys.argv: + sys.argv.append(i) + exit_code = main() sys.exit(exit_code) + +sys.argv diff --git a/paperpi/plugins/lms_client/lms_client.ipynb b/paperpi/plugins/lms_client/lms_client.ipynb index 2a2e16d..106f493 100644 --- a/paperpi/plugins/lms_client/lms_client.ipynb +++ b/paperpi/plugins/lms_client/lms_client.ipynb @@ -2,42 +2,46 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ - "%load_ext autoreload\n", - "%autoreload 2" + "import logging" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ - "import logging" + "import requests\n", + "from epdlib.Screen import Update\n", + "from copy import copy" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "import QueryLMS" + ] + }, + { + "cell_type": "code", + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ - "import requests\n", - "from epdlib.Screen import Update\n", - "from copy import copy\n", - "\n", - "import QueryLMS\n", - "\n", "import sys\n", "from pathlib import Path" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -51,7 +55,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -60,7 +64,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -245,49 +249,38 @@ ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], - "source": [ - "!ln -s ../../library ./" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], "source": [ - "# # this code snip simulates running from within the display loop use this and the following\n", - "# # cell to test the output\n", - "# # fugly hack for making the library module available to the plugins\n", - "# import sys\n", - "# sys.path.append(layout.dir_path+'/../..')\n", - "# from library import PluginTools\n", - "# import logging\n", - "# logging.root.setLevel('WARNING')\n", - "# from library.CacheFiles import CacheFiles\n", - "# from library import Plugin\n", - "# from IPython.display import display\n", - "# test_plugin = Plugin(resolution=(800, 600), screen_mode='RGB')\n", - "# test_plugin.config = {\n", - "# 'player_name': 'SqueezePlay',\n", - "# 'idle_timeout': 5,\n", - "# 'layout': 'two_columns_album_art'\n", - "# }\n", - "# test_plugin.refresh_rate = 5\n", - "# l = layout.layout\n", - "# test_plugin.layout = l\n", - "# test_plugin.cache = CacheFiles()\n", - "# test_plugin.update_function = update_function\n", - "# test_plugin.update()\n", - "# test_plugin.image" + "# this code snip simulates running from within the display loop use this and the following\n", + "# cell to test the output\n", + "# fugly hack for making the library module available to the plugins\n", + "import sys\n", + "sys.path.append(layout.dir_path+'/../..')\n", + "from library import PluginTools\n", + "import logging\n", + "logging.root.setLevel('WARNING')\n", + "from library.CacheFiles import CacheFiles\n", + "from library import Plugin\n", + "from IPython.display import display\n", + "test_plugin = Plugin(resolution=(800, 600), screen_mode='RGB')\n", + "test_plugin.config = {\n", + " 'player_name': 'SqueezePlay',\n", + " 'idle_timeout': 5,\n", + " 'layout': 'two_columns_album_art'\n", + "}\n", + "test_plugin.refresh_rate = 5\n", + "l = layout.layout\n", + "test_plugin.layout = l\n", + "test_plugin.cache = CacheFiles()\n", + "test_plugin.update_function = update_function\n", + "test_plugin.update()\n", + "test_plugin.image" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ @@ -322,31 +315,13 @@ " except KeyError as e:\n", " pass " ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[NbConvertApp] Converting notebook lms_client.ipynb to python\n", - "[NbConvertApp] Writing 9005 bytes to lms_client.py\n" - ] - } - ], - "source": [ - "!jupyter-nbconvert --to python --template python_clean lms_client.ipynb" - ] } ], "metadata": { "kernelspec": { - "display_name": "PaperPi-VBShxqF-", + "display_name": "venv_paperpi-9876705927", "language": "python", - "name": "paperpi-vbshxqf-" + "name": "venv_paperpi-9876705927" }, "language_info": { "codemirror_mode": { @@ -358,7 +333,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.2" + "version": "3.11.2" } }, "nbformat": 4, diff --git a/paperpi/plugins/lms_client/lms_client.py b/paperpi/plugins/lms_client/lms_client.py index a557eea..05db609 100644 --- a/paperpi/plugins/lms_client/lms_client.py +++ b/paperpi/plugins/lms_client/lms_client.py @@ -1,18 +1,19 @@ -#!/usr/bin/env python3 -# coding: utf-8 - - - - - +# --- +# jupyter: +# jupytext: +# text_representation: +# extension: .py +# format_name: light +# format_version: '1.5' +# jupytext_version: 1.16.0 +# kernelspec: +# display_name: venv_paperpi-9876705927 +# language: python +# name: venv_paperpi-9876705927 +# --- import logging - - - - - import requests from epdlib.Screen import Update from copy import copy @@ -22,11 +23,6 @@ import sys from pathlib import Path - - - - - try: from . import layout from . import constants @@ -34,18 +30,9 @@ import layout import constants - - - - - logger = logging.getLogger(__name__) - - - - def update_function(self): '''update function for lms_client provides now-playing LMS information @@ -226,12 +213,6 @@ def build_lms(): return (is_updated, data, priority) - - - - - - # # this code snip simulates running from within the display loop use this and the following # # cell to test the output # # fugly hack for making the library module available to the plugins @@ -257,11 +238,6 @@ def build_lms(): # test_plugin.update() # test_plugin.image - - - - - def scan_servers(*args, **kwargs): """USER FACING HELPER FUNCTION: scan local network for LMS servers; print list of servers players for first server @@ -292,7 +268,3 @@ def scan_servers(*args, **kwargs): print('\n') except KeyError as e: pass - - - -