fix: runs in dkrfile & tstng.yml, md fmt err, rm distutils, time delay

This commit is contained in:
Jovial Joe Jayarson 2022-02-26 13:19:59 +05:30
parent 88979b6b3c
commit 9915bc091b
3 changed files with 39 additions and 15 deletions

2
.github/testing.yml vendored
View File

@ -21,7 +21,7 @@ jobs:
run: |
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python - --version 1.1.13
echo "##vso[task.setvariable variable=PATH]${PATH}:$HOME/.poetry/bin"
source $HOME/.poetry/env"
source $HOME/.poetry/env
poetry install
- name: Run unit tests
run: |

View File

@ -1,4 +1,4 @@
FROM python:3.10.2-slim-bullseye as base
FROM python:3.10.2-slim-bullseye
ENV PYTHONFAULTHANDLER=1 \
PYTHONUNBUFFERED=1 \
@ -11,12 +11,9 @@ ENV PYTHONFAULTHANDLER=1 \
# poetry:
POETRY_VERSION=1.1.13 \
POETRY_NO_INTERACTION=1 \
POETRY_VIRTUALENVS_CREATE=false \
POETRY_CACHE_DIR='/var/cache/pypoetry' \
PATH="$PATH:/root/.local/bin"
WORKDIR /src
# install poetry
# RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
RUN pip install pipx
@ -24,9 +21,9 @@ RUN pipx install "poetry==$POETRY_VERSION"
RUN pipx ensurepath
# install dependencies
COPY pyproject.toml poetry.lock /src/
COPY pyproject.toml poetry.lock /
RUN poetry install --no-dev --no-root --no-interaction --no-ansi
# copy and run program
COPY main.py /src/
CMD [ "python", "/src/main.py" ]
ADD main.py /main.py
CMD [ "poetry", "run", "python", "/main.py" ]

41
main.py
View File

@ -33,11 +33,11 @@ Other 35 mins ⣦⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀
"""
# standard
from distutils.util import strtobool
from dataclasses import dataclass
from datetime import datetime
from base64 import b64encode
import logging as logger
from time import sleep
from typing import Any
import sys
import re
@ -106,9 +106,9 @@ class WakaInput:
return False
try:
self.show_title: bool = bool(strtobool(self.show_title))
self.show_time: bool = bool(strtobool(self.show_time))
self.show_total_time: bool = bool(strtobool(self.show_total_time))
self.show_title: bool = strtobool(self.show_title)
self.show_time: bool = strtobool(self.show_time)
self.show_total_time: bool = strtobool(self.show_total_time)
except (ValueError, AttributeError) as err:
logger.error(err)
return False
@ -128,6 +128,32 @@ class WakaInput:
return True
def strtobool(val: str) -> bool:
"""
strtobool
---------
PEP 632 https://www.python.org/dev/peps/pep-0632/ is depreciating distutils
Following code is somewhat shamelessly copied from the original source.
Convert a string representation of truth to True or False.
- True values are `'y', 'yes', 't', 'true', 'on', and '1'`
- False values are `'n', 'no', 'f', 'false', 'off', and '0'`
- Raises `ValueError` if `val` is anything else.
"""
val = val.lower()
if val in {'y', 'yes', 't', 'true', 'on', '1'}:
return True
if val in {'n', 'no', 'f', 'false', 'off', '0'}:
return False
raise ValueError(f'invalid truth value for {val}')
################### logic ###################
def make_title(dawn: str, dusk: str, /) -> str:
@ -250,12 +276,13 @@ def fetch_stats() -> Any:
headers={'Authorization': f'Basic {encoded_key}'}
)
logger.debug(
f'API response at trial #{4 - tries}: {resp.status_code} {resp.reason}'
f'API response @ trial #{4 - tries}: {resp.status_code} {resp.reason}'
)
if resp.status_code == 200 and (statistic := resp.json()):
logger.debug('Fetched WakaTime statistics')
break
logger.debug('Retrying ...')
logger.debug('Retrying in 3s ...')
sleep(3)
tries -= 1
if err := (statistic.get('error') or statistic.get('errors')):
@ -283,7 +310,7 @@ def churn(old_readme: str, /) -> str | None:
print('\n', generated_content, '\n', sep='')
new_readme = re.sub(
pattern=wk_c.waka_block_pattern,
repl=f'{wk_c.start_comment}```txt\n{generated_content}\n```{wk_c.end_comment}',
repl=f'{wk_c.start_comment}\n\n```text\n{generated_content}\n```\n\n{wk_c.end_comment}',
string=old_readme
)
# return None # un-comment when testing with --dev