Skip to content

Commit

Permalink
Enable CI checks also for main.py (martomi#367)
Browse files Browse the repository at this point in the history
* Enable CI checks also for main.py

And fix a few things brought up by black and mypy.

* Drop unneeded type checking

* Run flake8 on main.py in CI

Also:
  - Homogenize all tests to be called with filter `src tests *.py`.
    flake8 chokes on `.`
  - Fix issues in main.py found by flake8
  • Loading branch information
jinnatar authored Feb 13, 2023
1 parent c7f2991 commit 06496cb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ jobs:
pip install types-PyYAML types-python-dateutil types-paramiko types-retry
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Format check with Black
run: black --check src tests
run: black --check src tests *.py
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 src tests --count --select=E9,F63,F7,F82 --show-source --statistics
flake8 src tests *.py --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 src tests --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
flake8 src tests *.py --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Type Check with MyPy
run: |
mypy --install-types --non-interactive --check-untyped-defs src tests
mypy --install-types --non-interactive --check-untyped-defs src tests *.py
- name: Unit Tests
run: |
python -m unittest
24 changes: 11 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import signal
import subprocess
import time
from argparse import Namespace, ArgumentParser
from pathlib import Path
from typing import Tuple

Expand All @@ -20,13 +19,13 @@
from src.notifier.notify_manager import NotifyManager


def parse_arguments() -> Tuple[ArgumentParser, Namespace]:
def parse_arguments() -> Tuple[argparse.ArgumentParser, argparse.Namespace]:
parser = argparse.ArgumentParser(
description="ChiaFarmWatch: Watch your crops " "with a piece in mind for the yield."
)
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('--config', type=str, help="path to config.yaml")
group.add_argument('--version', action='store_true')
group.add_argument("--config", type=str, help="path to config.yaml")
group.add_argument("--version", action="store_true")
return parser, parser.parse_args()


Expand Down Expand Up @@ -57,25 +56,24 @@ def init(config: confuse.core.Configuration):
logging.info(f"Starting Chiadog ({version()})")

# Create log consumer based on provided configuration
chia_logs_config = config['chia_logs']
chia_logs_config = config["chia_logs"]
log_consumer = create_log_consumer_from_config(chia_logs_config)
if log_consumer is None:
exit(0)

# Keep a reference here so we can stop the thread
# TODO: read keep-alive thresholds from config
keep_alive_monitor = KeepAliveMonitor(config=config['keep_alive_monitor'])
keep_alive_monitor = KeepAliveMonitor(config=config["keep_alive_monitor"])

# Notify manager is responsible for the lifecycle of all notifiers
notify_manager = NotifyManager(config=config, keep_alive_monitor=keep_alive_monitor)

# Stats manager accumulates stats over 24 hours and sends a summary each day
stats_manager = StatsManager(config=config['daily_stats'], notify_manager=notify_manager)
stats_manager = StatsManager(config=config["daily_stats"], notify_manager=notify_manager)

# Link stuff up in the log handler
# Pipeline: Consume -> Handle -> Notify
log_handler = LogHandler(config=config, log_consumer=log_consumer, notify_manager=notify_manager,
stats_manager=stats_manager)
LogHandler(config=config, log_consumer=log_consumer, notify_manager=notify_manager, stats_manager=stats_manager)

def interrupt(signal_number, frame):
if signal_number == signal.SIGINT:
Expand Down Expand Up @@ -103,19 +101,19 @@ def version():
f = subprocess.Popen(command_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = f.communicate()
return stdout.decode(encoding="utf-8").rstrip()
except:
except (OSError, subprocess.CalledProcessError):
return "unknown"


if __name__ == "__main__":
# Parse config and configure logger
argparse, args = parse_arguments()
parser, args = parse_arguments()

# init sane config defaults
source_path = Path(__file__).resolve()
source_dir = source_path.parent
config = confuse.Configuration('chiadog', __name__)
config.set_file(source_dir / 'src/default_config.yaml')
config = confuse.Configuration("chiadog", __name__)
config.set_file(source_dir / "src/default_config.yaml")

# Override with given config
if args.config:
Expand Down
12 changes: 6 additions & 6 deletions src/chia_log/log_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from tempfile import mkdtemp
from threading import Thread
from time import sleep
from typing import List, Optional, Tuple
from typing import List, Tuple

# project
from src.util import OS
Expand Down Expand Up @@ -215,7 +215,7 @@ def get_host_info(host: str, user: str, path: str, port: int) -> Tuple[OS, PureP
return OS.LINUX, PurePosixPath(path)


def create_log_consumer_from_config(config: ConfigView) -> Optional[LogConsumer]:
def create_log_consumer_from_config(config: ConfigView) -> LogConsumer:
enabled_consumer = None
for consumer in config.keys():
if config[consumer]["enable"].get(bool):
Expand All @@ -224,8 +224,8 @@ def create_log_consumer_from_config(config: ConfigView) -> Optional[LogConsumer]
return None
enabled_consumer = consumer
if enabled_consumer is None:
logging.error("Couldn't find enabled log consumer in config.yaml")
return None
logging.critical("Couldn't find enabled log consumer in config.yaml")
exit(1)

enabled_consumer_config = config[enabled_consumer]

Expand Down Expand Up @@ -263,5 +263,5 @@ def create_log_consumer_from_config(config: ConfigView) -> Optional[LogConsumer]
remote_platform=platform,
)

logging.error("Unhandled consumer type")
return None
logging.critical("Unknown log consumer type enabled, typo?")
exit(1)

0 comments on commit 06496cb

Please sign in to comment.