Skip to content

Commit cc7f901

Browse files
committed
Fixed imports for projects without client-side filtering support.
1 parent a411a2c commit cc7f901

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

c8y_api/model/_base.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,18 @@
1212
from c8y_api._base_api import CumulocityRestApi
1313
from c8y_api.model.matcher import JsonMatcher
1414
from c8y_api.model._util import _DateUtil, _StringUtil, _QueryUtil
15+
16+
# trying to import various matchers that need external libraries
1517
try:
16-
from c8y_api.model.matcher import PydfMatcher
18+
from c8y_api.model.matcher import PydfMatcher as DefaultMatcher
1719
except ImportError:
18-
pass
20+
try:
21+
from c8y_api.model.matcher import JmesPathMatcher as DefaultMatcher
22+
except ImportError:
23+
try:
24+
from c8y_api.model.matcher import JsonPathMatcher as DefaultMatcher
25+
except ImportError:
26+
DefaultMatcher = None
1927

2028

2129
def get_by_path(dictionary: dict, path: str, default: Any = None) -> Any:
@@ -52,7 +60,7 @@ def get_all_by_path(dictionary: dict, paths: list[str] | dict[str, Any]) -> tupl
5260
5361
Return:
5462
The extracted values (or defaults it specified) as tuple. The
55-
number of elements in the tuple matches the length of the paths
63+
number of elements in the tuple matches the length of the `paths`
5664
argument.
5765
"""
5866
if isinstance(paths, dict):
@@ -639,7 +647,7 @@ def __init__(self, c8y: CumulocityRestApi, resource: str):
639647
# the last event for e.g. /event/events
640648
self.object_name = self.resource.split('/')[-1]
641649
# the default JSON matcher for client-side filtering
642-
self.default_matcher = PydfMatcher
650+
self.default_matcher = DefaultMatcher
643651

644652
def build_object_path(self, object_id: int | str) -> str:
645653
"""Build the path to a specific object of this resource.
@@ -801,8 +809,12 @@ def _iterate(
801809
num_results = 0
802810
# compile/prepare filter if defined
803811
if isinstance(include, str):
812+
if not self.default_matcher:
813+
raise ValueError("No default matcher defined (client-side filtering not supported?)")
804814
include = self.default_matcher(include)
805815
if isinstance(exclude, str):
816+
if not self.default_matcher:
817+
raise ValueError("No default matcher defined (client-side filtering not supported?)")
806818
exclude = self.default_matcher(exclude)
807819

808820
while True:

c8y_api/model/administration.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,8 +1361,12 @@ def select(
13611361
if username:
13621362
# compile/prepare filter if defined
13631363
if isinstance(include, str):
1364+
if not self.default_matcher:
1365+
raise ValueError("No default matcher defined (client-side filtering not supported?)")
13641366
include = self.default_matcher(include)
13651367
if isinstance(exclude, str):
1368+
if not self.default_matcher:
1369+
raise ValueError("No default matcher defined (client-side filtering not supported?)")
13661370
exclude = self.default_matcher(exclude)
13671371
# select by username
13681372
query = f'/user/{self.c8y.tenant_id}/users/{username}/groups?pageSize={page_size}&currentPage='

0 commit comments

Comments
 (0)