Skip to content

Commit

Permalink
add comp.__contains__ method
Browse files Browse the repository at this point in the history
fix checks before self.tools was initialized
  • Loading branch information
brunocbreis committed Feb 21, 2023
1 parent 13e1625 commit 5515add
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 6 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## Unreleased

### Added

- new comp \__contains__ method allows for quick checking if tool in comp even when self.tools is None

### Fixed

- add_merge doesn't break when comp is empty

## v0.1.1 – 2023-02-21

Some bug fixes and minor changes and improvements.
Expand Down
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- [ ] basic_comp() for jumpstarting projects
- [ ] default_comp() that uses configurable settings?
- [ ] quick titles or something
- [X] implement \__contains__ method to avoid bugs when self.tools haven't been initialized

## Docs

Expand Down
25 changes: 20 additions & 5 deletions pysion/composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,22 @@ def __setitem__(self, key: str, value: Tool | Macro):

self.add_tools(value)

def __contains__(self, value: Tool | Macro | Modifier | BezierSpline) -> bool:
if not (self.tools or self.modifiers):
return False

match value:
case Modifier() | BezierSpline():
if not self.modifiers:
return False
return value in self.modifiers.values()
case Tool() | Macro():
if not self.tools:
return False
return value in self.tools.values()
case _:
raise ValueError

# Private methods
def _auto_set_active_tool(self) -> None:
if not self.tools:
Expand Down Expand Up @@ -278,16 +294,15 @@ def add_merge(
)

# Add tools to comp if not already
if background not in self.tools.values():
if background not in self:
if background is not None:
self.add_tools(background)

if foreground not in self.tools.values():
if foreground not in self:
if foreground is not None:
self.add_tools(foreground)

merge.add_inputs(bg_input, fg_input)

self.add_tools(merge)

return merge
Expand Down Expand Up @@ -363,7 +378,7 @@ def animate(
"""
match tool:
case Tool():
if tool not in self.tools.values():
if tool not in self:
print(f"Adding {tool.name} to the comp.\n")
self.add_tools(tool)

Expand Down Expand Up @@ -420,7 +435,7 @@ def animate_position(
"""
match tool:
case Tool():
if tool not in self.tools.values():
if tool not in self:
print(f"Adding {tool.name} to the comp.\n")
self.add_tools(tool)

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = pysion
version = 0.1.1
version = 0.1.2
author = Bruno Reis
description = Package for creating code that is readable by DaVinci Resolve Fusion.
url = https://github.com/brunocbreis/pysion
Expand Down
33 changes: 33 additions & 0 deletions tests/test_composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,36 @@ def test_merge_macro(comp: Composition):
comp.add_merge("MergeMacro", macro, comp["MyBackground"])

assert comp["MergeMacro"]["Background"].source_operator == macro.name


def test_merge_tools_not_in_comp_yet():
comp = Composition()
bg1 = Tool(ToolID.background, "BG1")
bg2 = Tool(ToolID.background, "BG2")

merge = comp.add_merge("MergeBGs", bg1, bg2)

assert bg1 in comp.tools.values()
assert bg2 in comp.tools.values()
assert merge in comp.tools.values()


def test_composition_contains_tool(comp: Composition):
assert comp["MyBackground"] in comp


def test_composition_contains_modifier(comp: Composition):
pub = comp.publish(comp["MyBackground"], "TopLeftRed", 1)

assert pub in comp


def test_composition_does_not_contain(comp):
new_tool = Tool(ToolID.channel_booleans, "random")

assert not new_tool in comp


def test_composition_contains_error(comp):
with pytest.raises(ValueError):
"Pizza" in comp

0 comments on commit 5515add

Please sign in to comment.