From 381389ae1a2f3e23c46edc492187fc50ff008b5e Mon Sep 17 00:00:00 2001 From: "Ankur Sinha (Ankur Sinha Gmail)" Date: Thu, 27 Jul 2023 17:34:31 +0100 Subject: [PATCH] wip --- pyneuroml/nsgr/__init__.py | 11 +++++++++ pyneuroml/utils/__init__.py | 46 +++++++++++++++++++++++++++++++++++++ tests/utils/test_utils.py | 18 ++++++++++++++- 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 pyneuroml/nsgr/__init__.py diff --git a/pyneuroml/nsgr/__init__.py b/pyneuroml/nsgr/__init__.py new file mode 100644 index 00000000..4c274059 --- /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 044c3781..027f6e21 100644 --- a/pyneuroml/utils/__init__.py +++ b/pyneuroml/utils/__init__.py @@ -6,13 +6,17 @@ Copyright 2023 NeuroML Contributors """ +import os import typing +import time +from pathlib import Path import logging import re import neuroml logger = logging.getLogger(__name__) +logger.setLevel(logging.DEBUG) def extract_position_info( @@ -133,3 +137,45 @@ 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 + :type ignore_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 + + # TODO: continue here + included = False + for sfx in include_suffixes: + + + if include is True: + 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 ff0dff68..a9070f60 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,18 @@ 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) + print(files) + current_files = list(pl.Path(".").glob("*")) + current_files = [f for f in current_files if f.is_file()] + self.assertEqual(len(files), len(current_files)) + + # no files + files = get_files_generated_after(timestamp=0, ignore_suffixes=[".py"]) + print(files) + current_files = list(pl.Path(".").glob("*.hahah")) + self.assertEqual(len(files), len(current_files))