-
Notifications
You must be signed in to change notification settings - Fork 120
SG-38756 Fix parent property of PathTemplate in case of optional key #1016
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
d896ad8
6021bbd
0ae2f93
ed752fc
c04d861
3163f52
1bf7819
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -557,6 +557,17 @@ def root_path(self): | |||||
| """ | ||||||
| return self._prefix | ||||||
|
|
||||||
| def _dirname(self): | ||||||
| """ | ||||||
| Returns the directory part of the template. | ||||||
|
|
||||||
| :returns: String | ||||||
| """ | ||||||
| dirname, basename = os.path.split(self._repr_def) | ||||||
| if "[" in basename and "]" not in basename: | ||||||
| return dirname.split("[")[0] | ||||||
| return dirname | ||||||
|
|
||||||
| @property | ||||||
| def parent(self): | ||||||
| """ | ||||||
|
|
@@ -566,8 +577,8 @@ def parent(self): | |||||
|
|
||||||
| :returns: :class:`Template` | ||||||
| """ | ||||||
| parent_definition = os.path.dirname(self.definition) | ||||||
| if parent_definition: | ||||||
| parent_definition = self._dirname() | ||||||
| if parent_definition and parent_definition != "/": | ||||||
|
||||||
| if parent_definition and parent_definition != "/": | |
| if parent_definition and os.path.dirname(parent_definition) != parent_definition: |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1184,3 +1184,43 @@ def test_aliased_key(self): | |
| template = TemplatePath(definition, keys, root_path=self.project_root) | ||
| result = template.parent | ||
| self.assertEqual("{new_name}", result.definition) | ||
|
|
||
| def test_parent_with_optional(self): | ||
| definition = "shots/{Sequence}[-{seq_num}]/{Shot}/{Step}/work" | ||
| parent_expected_definitions = [ | ||
| "shots/{Sequence}-{seq_num}/{Shot}/{Step}", | ||
| "shots/{Sequence}/{Shot}/{Step}", | ||
| ] | ||
| parent_expected__repr_def = os.path.dirname(definition) | ||
|
Comment on lines
+1189
to
+1194
|
||
|
|
||
| template = TemplatePath(definition, self.keys, root_path=self.project_root) | ||
| parent = template.parent | ||
| self.assertEqual(parent_expected_definitions, parent._definitions) | ||
| self.assertEqual(parent_expected__repr_def, parent._repr_def) | ||
|
|
||
|
|
||
| class TestDirname(TestTemplatePath): | ||
| def test_dirname_with_word(self): | ||
| definition = "shots/{Sequence}/{Shot}/{Step}/work" | ||
| template = TemplatePath(definition, self.keys, root_path=self.project_root) | ||
| self.assertEqual("shots/{Sequence}/{Shot}/{Step}", template.dirname) | ||
|
|
||
| def test_dirname_with_key(self): | ||
| definition = "shots/{Sequence}/{Shot}/{Step}" | ||
| template = TemplatePath(definition, self.keys, root_path=self.project_root) | ||
| self.assertEqual("shots/{Sequence}/{Shot}", template.dirname) | ||
|
|
||
| def test_dirname_with_optional(self): | ||
| definition = "shots/{Sequence}/{Shot}[/{Step}]" | ||
| template = TemplatePath(definition, {}, root_path=self.project_root) | ||
|
||
| self.assertEqual("shots/{Sequence}/{Shot}", template.dirname) | ||
|
|
||
| def test_dirname_with_optional_word_and_key(self): | ||
| definition = "shots/{Sequence}/{Shot}/[abc_{Step}]" | ||
| template = TemplatePath(definition, {}, root_path=self.project_root) | ||
|
||
| self.assertEqual("shots/{Sequence}/{Shot}", template.dirname) | ||
|
|
||
| def test_dirname_with_optional_key_and_key(self): | ||
| definition = "shots/{Sequence}/[{Shot}]-{Step}" | ||
| template = TemplatePath(definition, {}, root_path=self.project_root) | ||
|
||
| self.assertEqual("shots/{Sequence}", template.dirname) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic for handling optional keys in
_dirname()is incomplete. If the basename contains a[but no], it splits by[and takes only the first part, but this doesn't account for cases where the opening bracket is in the dirname portion. For example, withshots/{Sequence}/[{Shot}]-{Step}, afteros.path.split, basename would be[{Shot}]-{Step}(containing both[and]), and the condition would be false, but the dirnameshots/{Sequence}would be incorrect if it also had an unclosed bracket. Consider a more robust approach that properly handles nested or partial optional sections.