From 9f6e876e14a074c386660c582c7f78e1503877aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonah=20Br=C3=BCchert?= Date: Tue, 16 Apr 2024 18:38:52 +0200 Subject: More python 3 adaption --- bin/hostinfo | 4 ++-- hostinfo/printer.py | 18 ++++++++---------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/bin/hostinfo b/bin/hostinfo index b85ccc8..639102c 100755 --- a/bin/hostinfo +++ b/bin/hostinfo @@ -20,8 +20,8 @@ from hostinfo import utils def _get_data(path): - stream = file(path, 'r') - return yaml.load(stream) + stream = open(path, 'r') + return yaml.safe_load(stream) def _match_key(data, keys): diff --git a/hostinfo/printer.py b/hostinfo/printer.py index 16c0249..4054df2 100644 --- a/hostinfo/printer.py +++ b/hostinfo/printer.py @@ -4,7 +4,7 @@ from hostinfo import prefix from hostinfo import utils -from typing import Iterable, Optional +from typing import Iterable, Optional, Any, Union def _get_full_key(prev_key, key): @@ -14,13 +14,13 @@ def _get_full_key(prev_key, key): def _sort_with_list(iterable: Iterable, sort: list[str]): - def helper(value): + def helper(value) -> str: if sort is None: return value (key, _) = value if key in sort: - return sort.index(key) + return str(sort.index(key)) return "%d %s" % (len(sort), key) return sorted(iterable, key=helper) @@ -60,15 +60,13 @@ class Printer: return True return False - def _print(self, valuestr: str, printer: prefix.Printer, + def _print(self, value: Union[str, dict[str, Any], list[str]], + printer: prefix.Printer, filter_key: Optional[str] = None, sort=None, force=False): - try: - value = valuestr.strip().splitlines() - except AttributeError: - pass - - if isinstance(value, dict): + if isinstance(value, str): + self._print_list(value.strip().splitlines(), printer, filter_key) + elif isinstance(value, dict): self._print_dict(value, printer, filter_key, sort, force) elif isinstance(value, list): self._print_list(value, printer, filter_key) -- cgit v1.2.3-1-g7c22