From d81f6f1f6f60e972cf6cb2bcca1b28d514dbea05 Mon Sep 17 00:00:00 2001 From: Wes Barnett Date: Sat, 27 Mar 2021 19:05:00 -0400 Subject: [PATCH] use nametuple --- scripts/snap_pac.py | 27 +++++++++++++++++---------- tests/test_script.py | 18 ++++++++++-------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/scripts/snap_pac.py b/scripts/snap_pac.py index ab331a5..80b8a0a 100755 --- a/scripts/snap_pac.py +++ b/scripts/snap_pac.py @@ -17,6 +17,7 @@ """Script for taking pre/post snapshots; run from pacman hooks.""" from argparse import ArgumentParser +from collections import namedtuple from configparser import ConfigParser import json import logging @@ -59,6 +60,12 @@ class SnapperCmd: return " ".join(self.cmd) +ProcessedConfig = namedtuple( + "ProcessedConfig", + ["description", "cleanup_algorithm", "userdata", "snapshot"] +) + + class ConfigProcessor: def __init__(self, ini_file, snapshot_type, parent_cmd=None, packages=None): @@ -119,12 +126,12 @@ class ConfigProcessor: def __call__(self, section): if section not in self.config: self.config.add_section(section) - return { - "description": self.get_description(section), - "cleanup_algorithm": self.get_cleanup_algorithm(section), - "userdata": self.get_userdata(section), - "snapshot": self.config.getboolean(section, "snapshot") - } + return ProcessedConfig( + self.get_description(section), + self.get_cleanup_algorithm(section), + self.get_userdata(section), + self.config.getboolean(section, "snapshot") + ) def get_snapper_configs(conf_file): @@ -181,12 +188,12 @@ if __name__ == "__main__": for snapper_config in snapper_configs: - data = config_processor(snapper_config) - if data["snapshot"]: + processed_config = config_processor(snapper_config) + if processed_config["snapshot"]: prefile = tmpdir / f"snap-pac-pre_{snapper_config}" pre_number = get_pre_number(snapshot_type, prefile) - snapper_cmd = SnapperCmd(snapper_config, snapshot_type, data["cleanup_algorithm"], - data["description"], chroot, pre_number, data["userdata"]) + snapper_cmd = SnapperCmd(snapper_config, snapshot_type, processed_config["cleanup_algorithm"], + processed_config["description"], chroot, pre_number, processed_config["userdata"]) num = snapper_cmd() logging.info(f"==> {snapper_config}: {num}") diff --git a/tests/test_script.py b/tests/test_script.py index adbe07a..07df0cb 100644 --- a/tests/test_script.py +++ b/tests/test_script.py @@ -4,7 +4,10 @@ import os import pytest -from scripts.snap_pac import SnapperCmd, ConfigProcessor, check_skip, get_pre_number, get_snapper_configs +from scripts.snap_pac import ( + SnapperCmd, ConfigProcessor, check_skip, get_pre_number, get_snapper_configs, + ProcessedConfig +) @pytest.fixture @@ -65,28 +68,27 @@ def test_skip_snap_pac(): @pytest.mark.parametrize("section, command, packages, snapshot_type, result", [ ( "root", "foo", ["bar"], "pre", - {"description": "foo", "cleanup_algorithm": "number", "userdata": "", "snapshot": True} + ProcessedConfig("foo", "number", "", True) ), ( "root", "pacman -Syu", [], "pre", - {"description": "pacman -Syu", "cleanup_algorithm": "number", "userdata": "important=yes", "snapshot": True} + ProcessedConfig("pacman -Syu", "number", "important=yes", True) ), ( "mail", "pacman -Syu", [], "pre", - {"description": "pacman -Syu", "cleanup_algorithm": "number", "userdata": "", "snapshot": False} + ProcessedConfig("pacman -Syu", "number", "", False) ), ( "home", "pacman -Syu", [], "pre", - {"description": "pac", "cleanup_algorithm": "number", "userdata": "foo=bar,requestid=42", "snapshot": True} + ProcessedConfig("pac", "number", "foo=bar,requestid=42", True) ), ( "home", "pacman -Syu", [], "post", - {"description": "a r", "cleanup_algorithm": "number", "userdata": "foo=bar,requestid=42", "snapshot": True} + ProcessedConfig("a r", "number", "foo=bar,requestid=42", True) ), ( "myconfig", "pacman -S linux", ["linux"], "post", - {"description": "linux", "cleanup_algorithm": "timeline", - "userdata": "foo=bar,important=yes,requestid=42", "snapshot": True} + ProcessedConfig("linux", "timeline", "foo=bar,important=yes,requestid=42", True) ), ]) def test_config_processor(section, command, packages, snapshot_type, result):