Skip to content

Commit

Permalink
total_filesize: error out when given a directory
Browse files Browse the repository at this point in the history
  • Loading branch information
Sbozzolo committed Jun 16, 2024
1 parent 0612aa7 commit a9496d8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
10 changes: 8 additions & 2 deletions kuibit/cactus_ascii_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"""

from collections.abc import Iterable
import os
import re

Expand Down Expand Up @@ -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
Expand All @@ -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...
Expand Down
38 changes: 38 additions & 0 deletions tests/test_cactus_ascii_utils.py
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.

"""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)

0 comments on commit a9496d8

Please sign in to comment.