Skip to content

Commit

Permalink
feat(settings/toml): Unify toml load
Browse files Browse the repository at this point in the history
toml_load duplicated in each subcommand, we can move it to
top level and send loaded dict over context.

Signed-off-by: Denys Fedoryshchenko <denys.f@collabora.com>
  • Loading branch information
nuclearcat committed Sep 11, 2024
1 parent 78bdde3 commit 47fe4bd
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 24 deletions.
6 changes: 4 additions & 2 deletions kci-dev/kci-dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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


Expand Down
Empty file added kci-dev/libs/__init__.py
Empty file.
10 changes: 10 additions & 0 deletions kci-dev/libs/common.py
Original file line number Diff line number Diff line change
@@ -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
14 changes: 3 additions & 11 deletions kci-dev/subcommands/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# -*- coding: utf-8 -*-

import click
import toml
import requests
import json
from git import Repo
Expand Down Expand Up @@ -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",
Expand All @@ -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__":
Expand Down
14 changes: 3 additions & 11 deletions kci-dev/subcommands/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# -*- coding: utf-8 -*-

import click
import toml
import requests
import json
from git import Repo
Expand Down Expand Up @@ -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",
Expand All @@ -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__":
Expand Down

0 comments on commit 47fe4bd

Please sign in to comment.