Skip to content

Commit

Permalink
Added exclude_patterns parameter in system.read_recursive_dir
Browse files Browse the repository at this point in the history
  • Loading branch information
sgeulette committed Oct 19, 2023
1 parent d0f7ca3 commit 8906f2a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Changelog
[sgeulette]
- Added `bs.remove_some_childrens` function.
[sgeulette]
- Added `exclude_patterns` parameter in `system.read_recursive_dir`
[sgeulette]

0.31 (2023-09-26)
-----------------
Expand Down
12 changes: 10 additions & 2 deletions imio/pyutils/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,23 @@ def read_dir(dirpath, with_path=False, only_folders=False, only_files=False, to_
return files


def read_recursive_dir(root_dir, rel_dir, with_folder=False, with_full_path=False):
def read_recursive_dir(root_dir, rel_dir, with_folder=False, with_full_path=False, exclude_patterns=[]):
""" Read the dir and return files """
files = []
full_dir = os.path.join(root_dir, rel_dir)
for filename in os.listdir(full_dir):
fullpath = os.path.join(full_dir, filename)
relpath = os.path.join(rel_dir, filename)
cont = False
for pat in exclude_patterns:
if re.search(pat, relpath):
cont = True
break
if cont:
continue
if os.path.isdir(fullpath):
files.extend(read_recursive_dir(root_dir, relpath, with_folder=with_folder, with_full_path=with_full_path))
files.extend(read_recursive_dir(root_dir, relpath, with_folder=with_folder, with_full_path=with_full_path,
exclude_patterns=exclude_patterns))
if not with_folder:
continue
if with_full_path:
Expand Down
28 changes: 28 additions & 0 deletions imio/pyutils/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
from imio.pyutils.utils import replace_in_list
from imio.pyutils.utils import safe_encode
from imio.pyutils.utils import sort_by_indexes
from imio.pyutils.system import read_recursive_dir

import os
import types
import unittest

Expand Down Expand Up @@ -158,3 +160,29 @@ def test_listify(self):
self.assertEqual(listify(("value")), ["value"])
self.assertEqual(listify(("value", )), ("value", ))
self.assertEqual(listify(("value", ), force=True), ["value"])


class TestSystem(unittest.TestCase):
""" """

def setUp(self):
file_dir = os.path.dirname(__file__)
self.dir = file_dir.replace('/pyutils', '')

def test_read_recursive_dir(self):
res = read_recursive_dir(self.dir, '')
self.assertEqual(len(res), 12)
# include folder
self.assertNotIn('pyutils', res)
res = read_recursive_dir(self.dir, '', with_folder=True)
self.assertIn('pyutils', res)
# include full path
self.assertEqual(len([path for path in res if path.endswith('/pyutils')]), 0)
res = read_recursive_dir(self.dir, '', with_folder=True, with_full_path=True)
self.assertEqual(len([path for path in res if path.endswith('/pyutils')]), 1)
# exclude patterns
self.assertTrue([path for path in res if path.endswith('.pyc')])
res = read_recursive_dir(self.dir, '', exclude_patterns=[r'\.pyc$'])
self.assertFalse([path for path in res if path.endswith('.pyc')])
res = read_recursive_dir(self.dir, '', exclude_patterns=[r'^pyutils/', r'\.pyc$'])
self.assertEqual(len(res), 1)

0 comments on commit 8906f2a

Please sign in to comment.