Skip to content

Commit ddf85b0

Browse files
committed
Add exclude path regex
1 parent d5c3189 commit ddf85b0

File tree

4 files changed

+31
-13
lines changed

4 files changed

+31
-13
lines changed

pathtraits/cli.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,21 @@ def main():
2626
default=DB_PATH,
2727
type=click.Path(file_okay=True, dir_okay=False),
2828
)
29+
@click.option(
30+
"--exclude-regex",
31+
default=None,
32+
)
2933
@click.option("-v", "--verbose", flag_value=True, default=False)
30-
def batch(path, db_path, verbose):
34+
def batch(path, db_path, exclude_regex, verbose):
3135
"""
3236
Update database once, searches for all directories recursively.
3337
3438
:param path: path to scan in batch mode recursively
3539
:param db_path: path to the database
40+
:param exclude_regex: exclue file paths matching this regex
3641
:param verbose: enable verbose logging
3742
"""
38-
scan.batch(path, db_path, verbose)
43+
scan.batch(path, db_path, exclude_regex, verbose)
3944

4045

4146
@main.command(help="Update database continiously, watches for new or changed files.")

pathtraits/db.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def flatten_dict(dictionary: dict, root_key: str = "", separator: str = "/"):
7474
"""
7575
items = []
7676
for key, value in dictionary.items():
77-
new_key = root_key + separator + key if root_key else key
77+
new_key = str(root_key) + str(separator) + str(key) if root_key else key
7878
if isinstance(value, MutableMapping):
7979
items.extend(
8080
TraitsDB.flatten_dict(value, new_key, separator=separator).items()
@@ -132,7 +132,7 @@ def get(self, table, cols="*", condition=None, **kwargs):
132132
for (k, v) in kwargs.items()
133133
}
134134
condition = " AND ".join([f"{k}={v}" for (k, v) in escaped_kwargs.items()])
135-
get_row_query = f"SELECT {cols} FROM {table} WHERE {condition};"
135+
get_row_query = f"SELECT {cols} FROM [{table}] WHERE {condition};"
136136
response = self.execute(get_row_query)
137137

138138
if response is None:
@@ -158,7 +158,7 @@ def put_path_id(self, path):
158158
get_row_query = f"SELECT id FROM path WHERE path = '{path}' LIMIT 1;"
159159
res = self.execute(get_row_query).fetchone()
160160
if res:
161-
return res[0]
161+
return res["id"]
162162
# create
163163
self.put("path", path=path)
164164
path_id = self.get("path", path=path, cols="id")["id"]
@@ -334,10 +334,9 @@ def add_pathpair(self, pair: PathPair):
334334

335335
# get element type for list
336336
# add: handle lists with mixed element type
337-
t = type(v[0]) if isinstance(v, list) else type(v)
337+
t = type(v[0]) if isinstance(v, list) and len(v) > 0 else type(v)
338338
k = f"{k}/{TraitsDB.sql_type(t)}"
339339
if k not in self.traits:
340-
t = type(v[0]) if isinstance(v, list) else type(v)
341340
self.create_trait_table(k, t)
342341
if k in self.traits:
343342
# add to list

pathtraits/scan.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717

1818
# pylint: disable=W0102
19-
def scan_meta_yml(path, pathpairs=[]):
19+
def scan_meta_yml(path, pathpairs=[], exclude_regex=None):
2020
"""
2121
Scan a directory recursively for meta yml files and return PathPairs.
2222
@@ -27,8 +27,12 @@ def scan_meta_yml(path, pathpairs=[]):
2727
# faster than os.walk
2828
with os.scandir(path) as ents:
2929
for e in ents:
30+
if not exclude_regex is None:
31+
if exclude_regex.search(e.path):
32+
logger.debug("exclude subtree path: %s", e.path)
33+
continue
3034
if e.is_dir():
31-
scan_meta_yml(e.path, pathpairs)
35+
scan_meta_yml(e.path, pathpairs, exclude_regex)
3236
else:
3337
if not yaml_re.search(e.path):
3438
continue
@@ -43,7 +47,7 @@ def scan_meta_yml(path, pathpairs=[]):
4347
return pathpairs
4448

4549

46-
def batch(path, db_path, verbose):
50+
def batch(path, db_path, exclude_regex, verbose):
4751
"""
4852
Update database once, searches for all directories recursively.
4953
@@ -57,7 +61,9 @@ def batch(path, db_path, verbose):
5761
if db_path is None:
5862
db_path = path + "/.pathtraits.db"
5963
db = TraitsDB(db_path)
60-
pathpairs = scan_meta_yml(path)
64+
if exclude_regex is not None:
65+
exclude_regex = re.compile(exclude_regex)
66+
pathpairs = scan_meta_yml(path, exclude_regex=exclude_regex)
6167
for pathpair in pathpairs:
6268
db.add_pathpair(pathpair)
6369

test/test.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class TestMain(unittest.TestCase):
1919
@classmethod
2020
def setUpClass(cls):
2121
cls.db_path = tempfile.mkstemp()[1]
22-
pathtraits.scan.batch("test/example", cls.db_path, False)
22+
pathtraits.scan.batch("test/example", cls.db_path, "North_America$", False)
2323
cls.db = pathtraits.db.TraitsDB(cls.db_path)
2424

2525
@classmethod
@@ -62,9 +62,17 @@ def test_example(self):
6262
for k, v in target.items():
6363
self.assertEqual(source[k], v)
6464

65+
def test_missing_north_america(self):
66+
source = pathtraits.access.get_dict(
67+
self.db, "test/example/Americas/North_America"
68+
)
69+
target = {"description": "all data", "is_example": True}
70+
for k, v in target.items():
71+
self.assertEqual(source[k], v)
72+
6573
def test_data_view(self):
6674
source = len(self.db.execute("SELECT * FROM data;").fetchall())
67-
target = 10
75+
target = 8
6876
self.assertEqual(source, target)
6977

7078

0 commit comments

Comments
 (0)