🔀 Merge branch '2-logger'

This commit is contained in:
Freezed 2022-09-25 22:32:43 +02:00
commit 22388cb913
4 changed files with 64 additions and 17 deletions

1
.gitignore vendored
View File

@ -1,3 +1,2 @@
client.py
__pycache__/
issues.csv

View File

@ -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"

View File

@ -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`

61
main.py
View File

@ -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)