From 47fe4bda1b7f537f40c43282edf31a5587a3fd35 Mon Sep 17 00:00:00 2001 From: Denys Fedoryshchenko Date: Tue, 10 Sep 2024 13:47:58 +0300 Subject: [PATCH] feat(settings/toml): Unify toml load toml_load duplicated in each subcommand, we can move it to top level and send loaded dict over context. Signed-off-by: Denys Fedoryshchenko --- kci-dev/kci-dev.py | 6 ++++-- kci-dev/libs/__init__.py | 0 kci-dev/libs/common.py | 10 ++++++++++ kci-dev/subcommands/commit.py | 14 +++----------- kci-dev/subcommands/patch.py | 14 +++----------- 5 files changed, 20 insertions(+), 24 deletions(-) create mode 100644 kci-dev/libs/__init__.py create mode 100644 kci-dev/libs/common.py diff --git a/kci-dev/kci-dev.py b/kci-dev/kci-dev.py index 1bb94af..fde9e30 100755 --- a/kci-dev/kci-dev.py +++ b/kci-dev/kci-dev.py @@ -2,9 +2,10 @@ # -*- coding: utf-8 -*- import click +import toml from subcommands import commit from subcommands import patch - +from libs.common import * @click.group( help="Stand alone tool for Linux Kernel developers and maintainers that can test local Linux Kernel changes on a enabled KernelCI server" @@ -13,8 +14,9 @@ @click.option("--settings", default=".kci-dev.toml", help="path of toml setting file") @click.pass_context def cli(ctx, settings): + cfg = load_toml(settings) ctx.ensure_object(dict) - ctx.obj["SETTINGS"] = settings + ctx.obj["CFG"] = cfg pass diff --git a/kci-dev/libs/__init__.py b/kci-dev/libs/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/kci-dev/libs/common.py b/kci-dev/libs/common.py new file mode 100644 index 0000000..ea5361e --- /dev/null +++ b/kci-dev/libs/common.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import toml + + +def load_toml(settings): + with open(settings) as fp: + config = toml.load(fp) + return config diff --git a/kci-dev/subcommands/commit.py b/kci-dev/subcommands/commit.py index e8b9856..bae0ef6 100644 --- a/kci-dev/subcommands/commit.py +++ b/kci-dev/subcommands/commit.py @@ -2,7 +2,6 @@ # -*- coding: utf-8 -*- import click -import toml import requests import json from git import Repo @@ -40,12 +39,6 @@ def send_build(url, patch, branch, treeurl, token): click.secho(response.json(), fg="green") -def load_toml(settings): - with open(settings) as fp: - config = toml.load(fp) - return config - - @click.command(help="Test commits from a local Kernel repository") @click.option( "--repository", @@ -66,11 +59,10 @@ def load_toml(settings): ) @click.pass_context def commit(ctx, repository, branch, private, path): - settings = ctx.obj.get("SETTINGS") - config = load_toml(settings) - url = api_connection(config["connection"]["host"]) + cfg = ctx.obj.get("CFG") + url = api_connection(cfg["connection"]["host"]) diff = find_diff(path, branch, repository) - send_build(url, diff, branch, repository, config["connection"]["token"]) + send_build(url, diff, branch, repository, cfg["connection"]["token"]) if __name__ == "__main__": diff --git a/kci-dev/subcommands/patch.py b/kci-dev/subcommands/patch.py index 0580394..29ce078 100644 --- a/kci-dev/subcommands/patch.py +++ b/kci-dev/subcommands/patch.py @@ -2,7 +2,6 @@ # -*- coding: utf-8 -*- import click -import toml import requests import json from git import Repo @@ -30,12 +29,6 @@ def send_build(url, patch, branch, treeurl, token): click.secho(response.json(), fg="green") -def load_toml(settings): - with open(settings) as fp: - config = toml.load(fp) - return config - - @click.command(help="Test a patch or a mbox file") @click.option( "--repository", @@ -52,11 +45,10 @@ def load_toml(settings): @click.option("--patch", required=True, help="mbox or patch file path") @click.pass_context def patch(ctx, repository, branch, private, patch): - settings = ctx.obj.get("SETTINGS") - config = load_toml(settings) - url = api_connection(config["connection"]["host"]) + cfg = ctx.obj.get("CFG") + url = api_connection(cfg["connection"]["host"]) patch = open(patch, "rb") - send_build(url, patch, branch, repository, config["connection"]["token"]) + send_build(url, patch, branch, repository, cfg["connection"]["token"]) if __name__ == "__main__":