From 772bf19b84ed31d4490ea13b64e8fe74a21b1a60 Mon Sep 17 00:00:00 2001 From: "Ankur Sinha (Ankur Sinha Gmail)" Date: Thu, 27 Jul 2023 17:34:31 +0100 Subject: [PATCH] feat: add method to get files modified after particular time --- pyneuroml/nsgr/__init__.py | 11 ++++++ pyneuroml/utils/__init__.py | 67 +++++++++++++++++++++++++++++++++++-- tests/utils/test_utils.py | 23 ++++++++++++- 3 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 pyneuroml/nsgr/__init__.py diff --git a/pyneuroml/nsgr/__init__.py b/pyneuroml/nsgr/__init__.py new file mode 100644 index 000000000..4c274059a --- /dev/null +++ b/pyneuroml/nsgr/__init__.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python3 +""" +Module for working with NSG. See https://github.com/OpenSourceBrain/pynsgr + +File: pyneuroml/nsgr/__init__.py + +Copyright 2023 NeuroML contributors +""" + + + diff --git a/pyneuroml/utils/__init__.py b/pyneuroml/utils/__init__.py index 044c37818..c8ee3cad3 100644 --- a/pyneuroml/utils/__init__.py +++ b/pyneuroml/utils/__init__.py @@ -6,13 +6,17 @@ Copyright 2023 NeuroML Contributors """ -import typing import logging +import os import re -import neuroml +import time +import typing +from pathlib import Path +import neuroml logger = logging.getLogger(__name__) +logger.setLevel(logging.DEBUG) def extract_position_info( @@ -133,3 +137,62 @@ def convert_case(name): """Converts from camelCase to under_score""" s1 = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", name) return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower() + + +def get_files_generated_after( + timestamp: float = time.time(), + directory: str = ".", + ignore_suffixes: list[str] = ["xml", "nml"], + include_suffixes: list[str] = [], +) -> typing.List[str]: + """Get files modified after provided time stamp in directory, excluding provided suffixes. + + Ignores directories. + + :param timestamp: time stamp to compare to + :type timestamp: float + :param directory: directory to list files of + :type directory: str + :param ignore_suffixes: file suffixes to ignore (none if empty) + :type ignore_suffixes: str + :param include_suffixes: file suffixes to include (all if empty) + :type include_suffixes: str + :returns: list of file names + :rtype: list(str) + + """ + logger.debug(f"Timestamp is: {timestamp}") + current_files = list(Path(directory).glob("*")) + # only files, ignore directories + current_files = [f for f in current_files if f.is_file()] + files = [] + for file in current_files: + excluded = False + for sfx in ignore_suffixes: + if file.name.endswith(sfx): + excluded = True + break + + # no need to proceed + if excluded is True: + continue + + included = False + # if no suffixes, ignore this + if len(include_suffixes) == 0: + included = True + else: + for sfx in include_suffixes: + if file.name.endswith(sfx): + included = True + break + + # no need to proceed + if included is False: + continue + + file_mtime = os.path.getmtime(file) + if file_mtime > timestamp: + files.append(file.name) + + return files diff --git a/tests/utils/test_utils.py b/tests/utils/test_utils.py index ff0dff681..2b9f09d94 100644 --- a/tests/utils/test_utils.py +++ b/tests/utils/test_utils.py @@ -10,9 +10,10 @@ import logging import pathlib as pl +import time from pyneuroml.pynml import read_neuroml2_file -from pyneuroml.utils import extract_position_info +from pyneuroml.utils import extract_position_info, get_files_generated_after from .. import BaseTestCase @@ -46,3 +47,23 @@ def test_extract_position_info(self): for c in ["HL23PV", "HL23PYR", "HL23VIP", "HL23SST"]: self.assertIn(c, cell_id_vs_cell.keys()) + + def test_get_files_generated_after(self): + """test get_files_generated_after method.""" + # compare to epoch, should just list all files + files = get_files_generated_after(timestamp=0, + include_suffixes=[".sh", ".ini"]) + logger.debug(files) + current_files = list(pl.Path(".").glob("*.sh")) + current_files.extend(list(pl.Path(".").glob("*.ini"))) + current_files = [f for f in current_files if f.is_file()] + self.assertEqual(len(files), len(current_files)) + + files = get_files_generated_after(timestamp=0, + include_suffixes=[".sh", ".ini"], + ignore_suffixes=[".sh"]) + logger.debug(files) + current_files = list(pl.Path(".").glob("*.ini")) + current_files = [f for f in current_files if f.is_file()] + self.assertEqual(len(files), len(current_files)) +