From 09347598de834cdd7e2339c4cd5f78c8c24a4185 Mon Sep 17 00:00:00 2001 From: Freezed <2160318-free_zed@users.noreply.gitlab.com> Date: Sun, 25 Sep 2022 22:24:43 +0200 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=94=A8=20Improve=20lint=20and=20makef?= =?UTF-8?q?ile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 17 +++++++++-------- README.md | 2 +- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 90dd5c9..c163908 100644 --- a/Makefile +++ b/Makefile @@ -1,25 +1,26 @@ +VBIN=${VIRTUAL_ENV}/bin/ + help: # Print help on Makefile @grep '^[^.#]\+:\s\+.*#' Makefile | \ sed "s/\(.\+\):\s*\(.*\) #\s*\(.*\)/`printf "\033[93m"`\1`printf "\033[0m"` \3/" | \ expand -t20 open_all: # Open all projects files - ${EDITOR} ${VIRTUAL_ENV}/bin/activate - ${EDITOR} .gitignore client* main.py Makefile pyproject.toml README.md requirements-dev.txt requirements.txt + ${EDITOR} ${VBIN}/activate + ${EDITOR} .gitignore .gitlab-ci.yaml client* main.py Makefile pyproject.toml README.md requirements-dev.txt requirements.txt ${EDITOR} .git/hooks/p*-commit pre_commit: # Run the pre-commit hook .git/hooks/pre-commit clean: # Remove files not tracked in source control - rm -rf .pytest_cache + rm -rf __pycache__ find . -type f -name "*.orig" -delete find . -type f -name "*.pyc" -delete - find . -type d -name "__pycache__" -delete find . -type d -empty -delete lint: # Lint code - ${VIRTUAL_ENV}/bin/black --check --quiet *.py - ${VIRTUAL_ENV}/bin/pflake8 --config=pyproject.toml - ${VIRTUAL_ENV}/bin/pydocstyle - ${VIRTUAL_ENV}/bin/pylint --rcfile=pyproject.toml *.py + ${VBIN}/black --quiet --check *.py && echo "βœ… black" || echo "🚨 black" + ${VBIN}/pflake8 --config=pyproject.toml && echo "βœ… pflake8" || echo "🚨 pflake8" + ${VBIN}/pydocstyle && echo "βœ… pydocstyle" || echo "🚨 pydocstyle" + ${VBIN}/pylint --rcfile=pyproject.toml *.py && echo "βœ… pylint" || echo "🚨 pylint" diff --git a/README.md b/README.md index a310c04..84a90d5 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ python main.py -h * `pip install -r requirements-dev.txt` - A `Makefile` with toolsβ€―: run `make help` to have a look - Use the `pre-commit` git hook - * `echo "make --no-print-directory lint" > .git/hooks/pre-commit` + * `echo "make --no-print-directory --quiet lint" > .git/hooks/pre-commit` * `chmod u+x .git/hooks/pre-commit` From 8dd707f4f62ce9ca181d16b4114c9c262114feb8 Mon Sep 17 00:00:00 2001 From: Freezed <2160318-free_zed@users.noreply.gitlab.com> Date: Tue, 20 Sep 2022 23:58:41 +0200 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=94=8A=20Add=20a=20basic=20logger=20#?= =?UTF-8?q?2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 - main.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 54 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index c3bbc60..35013a4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ client.py __pycache__/ -issues.csv diff --git a/main.py b/main.py index 36af398..902e6cb 100755 --- a/main.py +++ b/main.py @@ -11,11 +11,36 @@ Licenses: GNU AGPL v3: http://www.gnu.org/licenses/ import argparse +import logging +import os import sys -from pprint import pprint as pp +import client # pylint: disable=import-error -from client import LDP_ALIAS, opnsrch_clt # pylint: disable=import-error +logger = logging.getLogger(os.path.splitext(os.path.basename(sys.argv[0]))[0]) + + +def setup_logging(options): + """Configure logging.""" + root = logging.getLogger("") + logger.setLevel(options.debug and logging.DEBUG or logging.INFO) + + if options.debug: + # Logging with --debug option + # Logging on terminal standard output + + handler_tty = logging.StreamHandler() + handler_tty.setFormatter( + logging.Formatter("%(name)s::%(levelname)s::%(message)s") + ) + root.addHandler(handler_tty) + + else: + # Logging default setings + + handler_tty = logging.StreamHandler() + handler_tty.setFormatter(logging.Formatter("%(levelname)s\t%(message)s")) + root.addHandler(handler_tty) class CustomFormatter( @@ -36,6 +61,14 @@ def parse_args(args=sys.argv[1:]): description=sys.modules[__name__].__doc__, formatter_class=CustomFormatter ) + parser.add_argument( + "-d", + "--debug", + action="store_true", + default=False, + help="Log activity in console", + ) + parser.add_argument( "-m", "--mapping", @@ -52,17 +85,29 @@ def get_map_props(): LDP indices are rolling and LDP do not implement the call to get indices related to an alias. """ - indices = list(opnsrch_clt.indices.get_alias("*")) - mapping = opnsrch_clt.indices.get_mapping(LDP_ALIAS)[indices[0]]["mappings"] - map_props = sorted(list(mapping["properties"])) + map_props = [] + + if client.LDP_CRED_TYPE == "token": + indices = list(client.opnsrch_clt.indices.get_alias("*")) + mapping = client.opnsrch_clt.indices.get_mapping(client.LDP_ALIAS)[indices[0]][ + "mappings" + ] + map_props = sorted(list(mapping["properties"])) + logger.debug("Found mapping!") + else: + logger.critical( + "Stream mapping unavailable with credential type: '%s'", + client.LDP_CRED_TYPE, + ) return map_props def main(options): - """Execute as a script. Functions related to the arguments passed.""" + """Execute as script. Functions related to the arguments passed.""" if options.mapping: response = get_map_props() + logger.debug("Mapping for '%s' stream: %s", client.LDP_STREAM_NAME, response) else: raise RuntimeError @@ -71,6 +116,8 @@ def main(options): if __name__ == "__main__": pargs = parse_args() + setup_logging(pargs) result = main(pargs) - pp(result) + if not pargs.debug: + print(result)