Skip to content

Commit

Permalink
feat: add method to get files modified after particular time
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjayankur31 committed Jul 27, 2023
1 parent 1c33c44 commit 772bf19
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 3 deletions.
11 changes: 11 additions & 0 deletions pyneuroml/nsgr/__init__.py
Original file line number Diff line number Diff line change
@@ -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
"""



67 changes: 65 additions & 2 deletions pyneuroml/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
23 changes: 22 additions & 1 deletion tests/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))

0 comments on commit 772bf19

Please sign in to comment.