Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 50a2aca

Browse files
committedMay 30, 2023
🟢
1 parent 31ab156 commit 50a2aca

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed
 

‎src/napari_matplotlib/util.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import List, Optional, Tuple, Union
1+
from typing import Generator, List, Optional, Tuple, Union
22
from warnings import warn
33

44
import napari.qt
@@ -47,6 +47,21 @@ def __contains__(self, val: int) -> bool:
4747
return True
4848

4949

50+
def _logical_lines(
51+
nodes: List[tinycss2.ast.Node],
52+
) -> Generator[Tuple[tinycss2.ast.Node, tinycss2.ast.Node], None, None]:
53+
"""Generator to provide logical lines (thing: value) of css (terminated by ';')"""
54+
ident, dimension = None, None
55+
for node in nodes:
56+
if node == ";":
57+
yield (ident, dimension)
58+
ident, dimension = None, None
59+
elif node.type == "ident":
60+
ident = node
61+
elif node.type == "dimension":
62+
dimension = node
63+
64+
5065
def _has_id(nodes: List[tinycss2.ast.Node], id_name: str) -> bool:
5166
"""
5267
Is `id_name` in IdentTokens in the list of CSS `nodes`?
@@ -66,13 +81,8 @@ def _get_dimension(
6681
-------
6782
None if no IdentToken is found.
6883
"""
69-
cleaned_nodes = [node for node in nodes if node.type != "whitespace"]
70-
for name, _, value, _ in zip(*(iter(cleaned_nodes),) * 4):
71-
if (
72-
name.type == "ident"
73-
and value.type == "dimension"
74-
and name.value == id_name
75-
):
84+
for name, value in _logical_lines(nodes):
85+
if name.value == id_name:
7686
return value.int_value
7787
warn(f"Unable to find DimensionToken for {id_name}", RuntimeWarning)
7888
return None

0 commit comments

Comments
 (0)
Please sign in to comment.