Skip to content

Commit

Permalink
Wildcard support in filters (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
gouline authored Feb 20, 2024
1 parent d9f50dc commit d8788cc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ sandbox-models:
--metabase-username $$MB_USER \
--metabase-password $$MB_PASSWORD \
--metabase-database $$POSTGRES_DB \
--include-schemas "public",other \
--include-schemas "pub*",other \
--http-header x-dummy-key dummy-value \
--order-fields \
--verbose )
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ dbt-metabase exposures \
--metabase-username [email protected] \
--metabase-password Password123 \
--output-path models/ \
--exclude-collections temporary
--exclude-collections "temp*"
```

Once the execution completes, check your output path for exposures files containing descriptions, creator details and links for Metabase questions and dashboards:
Expand Down Expand Up @@ -295,7 +295,7 @@ c.export_models(
# Extracting exposures
c.extract_exposures(
output_path=".",
collection_filter=Filter(exclude=["temporary"]),
collection_filter=Filter(exclude=["temp*"]),
)
```

Expand Down
16 changes: 13 additions & 3 deletions dbtmetabase/format.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import fnmatch
import logging
import re
from logging.handlers import RotatingFileHandler
Expand Down Expand Up @@ -29,9 +30,18 @@ def __init__(

def match(self, item: str) -> bool:
item = self._norm_item(item)
included = not self.include or item in self.include
excluded = self.exclude and item in self.exclude
return included and not excluded

for exclude in self.exclude:
if fnmatch.fnmatch(item, exclude):
return False

if self.include:
for include in self.include:
if fnmatch.fnmatch(item, include):
return True
return False

return True

@staticmethod
def _norm_arg(arg: Optional[Sequence[str]]) -> Sequence[str]:
Expand Down
9 changes: 9 additions & 0 deletions tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ def test_filter(self):
self.assertTrue(Filter(include="alpha").match("Alpha"))
self.assertFalse(Filter(exclude="alpha").match("Alpha"))

def test_filter_wildcard(self):
self.assertTrue(Filter(include="stg_*").match("stg_orders"))
self.assertTrue(Filter(include="STG_*").match("stg_ORDERS"))
self.assertFalse(Filter(include="stg_*").match("orders"))
self.assertTrue(Filter(include="order?").match("orders"))
self.assertFalse(Filter(include="order?").match("ordersz"))
self.assertTrue(Filter(include="*orders", exclude="stg_*").match("_orders"))
self.assertFalse(Filter(include="*orders", exclude="stg_*").match("stg_orders"))

def test_null_value(self):
self.assertIsNotNone(NullValue)
self.assertFalse(NullValue)
Expand Down

0 comments on commit d8788cc

Please sign in to comment.