From a9496d8c72b25c7efb54b6326ff65a9ffde14995 Mon Sep 17 00:00:00 2001 From: Gabriele Bozzola Date: Sat, 25 Nov 2023 09:56:32 -0800 Subject: [PATCH] total_filesize: error out when given a directory --- NEWS.md | 1 + kuibit/cactus_ascii_utils.py | 10 +++++++-- tests/test_cactus_ascii_utils.py | 38 ++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100755 tests/test_cactus_ascii_utils.py diff --git a/NEWS.md b/NEWS.md index 8ffe5b4a..b06e4254 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,7 @@ #### General - Python 3.12 is now supported, Python 3.8.1 is required for development. +- `total_filesize` now errors out when directories are passed. ## Version 1.4.0 (2 May 2023) diff --git a/kuibit/cactus_ascii_utils.py b/kuibit/cactus_ascii_utils.py index 770c5dae..aae51077 100755 --- a/kuibit/cactus_ascii_utils.py +++ b/kuibit/cactus_ascii_utils.py @@ -40,6 +40,7 @@ """ +from collections.abc import Iterable import os import re @@ -226,9 +227,10 @@ def scan_header( return time_column, data_column -def total_filesize(allfiles, unit="MB"): +def total_filesize(allfiles: Iterable, unit="MB") -> float: """Return the total size of the given files. - Available units B, KB, MB and GB + + Available units B, KB, MB and GB. :param allfiles: List of the full paths of the files. :type allfiles: list @@ -238,6 +240,10 @@ def total_filesize(allfiles, unit="MB"): :rtype: float """ + directories = [path for path in allfiles if not os.path.isfile(path)] + + if len(directories) > 0: + raise ValueError(f"Given list contains directories: {directories}") # This function is here, but it could be anywhere, it doesn't really # apply only to ASCII files, nor only to Cactus files... diff --git a/tests/test_cactus_ascii_utils.py b/tests/test_cactus_ascii_utils.py new file mode 100755 index 00000000..8ecb2e88 --- /dev/null +++ b/tests/test_cactus_ascii_utils.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2023 Gabriele Bozzola +# +# Inspired by code originally developed by Wolfgang Kastaun. This file may +# contain algorithms and/or structures first implemented in +# GitHub:wokast/PyCactus/PostCactus/cactus_scalars.py +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, see . + +"""Tests for kuibit.cactus_ascii_utils +""" +import unittest + +from kuibit import cactus_ascii_utils as cau + + +class TestASCIIUtils(unittest.TestCase): + def test_total_filesize(self): + file_with_dir = ["tests/tov", "tests/tov/log.txt"] + + with self.assertRaises(ValueError): + cau.total_filesize(file_with_dir) + + files = ["tests/tov/log.txt", "tests/tov/ligo_sens.dat"] + + self.assertEqual(cau.total_filesize(files, unit="B"), 211972)