From 7e30b214f4a4256ef28aa7fec6ec5e06bd7fd7a9 Mon Sep 17 00:00:00 2001 From: Stefaan Lippens Date: Sun, 5 May 2024 15:52:00 +0200 Subject: [PATCH] Improve error message when `DuProcessor.from_du_listing` fails --- duviz.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/duviz.py b/duviz.py index 5f4a088..ccecc45 100755 --- a/duviz.py +++ b/duviz.py @@ -117,7 +117,7 @@ class DuProcessor: Size tree from `du` (disk usage) listings """ - _du_regex = re.compile(r'([0-9]*)\s*(.*)') + _du_regex = re.compile(r"([0-9]+)\s*(.+)") @classmethod def from_du( @@ -157,10 +157,13 @@ def from_du_listing( ) -> SizeTree: def pairs(lines: Iterable[str]) -> Iterator[Tuple[List[str], int]]: for line in lines: - kb, path = cls._du_regex.match(line).group(1, 2) - if progress_report: - progress_report(path) - yield path_split(path, root)[1:], 1024 * int(kb) + try: + kb, path = cls._du_regex.match(line).group(1, 2) + if progress_report: + progress_report(path) + yield path_split(path, root)[1:], 1024 * int(kb) + except Exception as e: + raise ValueError(f"Failed to parse {line!r}") from e return SizeTree.from_path_size_pairs(root=root, pairs=pairs(du_listing))