Skip to content

Commit 4593693

Browse files
authored
Fix separators count bug on names with hardcoded separators (#58)
* Fix separators count for names with hardcoded values which already include separators
1 parent fcccc09 commit 4593693

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "vfxnaming"
7-
version = "1.5.7-beta"
7+
version = "1.5.8-beta"
88
authors = [
99
{ name="Chris Granados", email="info@chrisgranados.com" },
1010
]

src/vfxnaming/rules.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class Rule(Serializable):
3535
"""
3636

3737
__FIELDS_REGEX = re.compile(r"{(.+?)}")
38+
__EXTRACT_FIELDS_REGEX = re.compile(r"(\{.+?(?::.+?)?\})")
3839
__PATTERN_SEPARATORS_REGEX = re.compile(
3940
r"(}[_\-\.:\|/\\]{|[_\-\.:\|/\\]{|}[_\-\.:\|/\\])"
4041
)
@@ -134,7 +135,11 @@ def parse(self, name: AnyStr) -> Union[Dict, None]:
134135
dict: A dictionary with keys as tokens and values as given name parts.
135136
e.g.: {'side':'C', 'part':'helmet', 'number': 1, 'type':'MSH'}
136137
"""
137-
expected_separators = self.__PATTERN_SEPARATORS_REGEX.findall(self._pattern)
138+
extract_tokens = self.__EXTRACT_FIELDS_REGEX.findall(self._pattern)
139+
pattern_wout_tokens = self._pattern
140+
for each in extract_tokens:
141+
pattern_wout_tokens = pattern_wout_tokens.replace(each, "XYZ")
142+
expected_separators = self.__SEPARATORS_REGEX.findall(pattern_wout_tokens)
138143
if len(expected_separators) <= 0:
139144
logger.warning(
140145
f"No separators used for rule '{self.name}', parsing is not possible."
@@ -186,7 +191,11 @@ def validate(self, name: AnyStr, strict: bool = False, **validate_values) -> boo
186191
Returns:
187192
bool: True if name matches the rule pattern, False otherwise.
188193
"""
189-
expected_separators = self.__PATTERN_SEPARATORS_REGEX.findall(self._pattern)
194+
extract_tokens = self.__EXTRACT_FIELDS_REGEX.findall(self._pattern)
195+
pattern_wout_tokens = self._pattern
196+
for each in extract_tokens:
197+
pattern_wout_tokens = pattern_wout_tokens.replace(each, "XYZ")
198+
expected_separators = self.__SEPARATORS_REGEX.findall(pattern_wout_tokens)
190199
if len(expected_separators) <= 0:
191200
logger.warning(
192201
f"No separators used for rule '{self.name}', parsing is not possible."

tests/naming_test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ def setup(self):
230230
)
231231
tokens.add_token("type", lighting="LGT", animation="ANI", default="lighting")
232232
rules.add_rule("lights", "{category}_{function}_{whatAffects}_{digits}_{type}")
233+
rules.add_rule(
234+
"lights_extra",
235+
"With_extra_{category}_{function}_{whatAffects}_{digits}_{type}",
236+
)
233237

234238
@pytest.mark.parametrize(
235239
"name,expected",
@@ -261,6 +265,7 @@ def setup(self):
261265
],
262266
)
263267
def test_valid(self, name: str, expected: int):
268+
rules.set_active_rule("lights")
264269
validated = n.validate(name)
265270
assert len(validated) == expected
266271

@@ -290,9 +295,28 @@ def test_valid(self, name: str, expected: int):
290295
],
291296
)
292297
def test_valid_with_tokens(self, name: str, validate_values: dict, expected: int):
298+
rules.set_active_rule("lights")
293299
validated = n.validate(name, **validate_values)
294300
assert len(validated) == expected
295301

302+
@pytest.mark.parametrize(
303+
"name,expected",
304+
[
305+
(
306+
"With_extra_dramatic_bounce_chars_001_LGT",
307+
1,
308+
),
309+
(
310+
"Withextra_dramatic_bounce_chars_001_LGT",
311+
0,
312+
),
313+
],
314+
)
315+
def test_valid_with_hardcoded(self, name: str, expected: int):
316+
rules.set_active_rule("lights_extra")
317+
validated = n.validate(name)
318+
assert len(validated) == expected
319+
296320

297321
class Test_ValidateHarcodedValues:
298322
@pytest.fixture(autouse=True)

0 commit comments

Comments
 (0)