Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from tilia.settings import settings

from tilia.requests import Get, Post, post, get
from tilia.timelines.component_kinds import ComponentKind
from tilia.timelines.timeline_kinds import TimelineKind
from tilia.ui import commands
from tilia.ui.windows import WindowKind
Expand Down Expand Up @@ -724,3 +725,24 @@ def test_are_unique_between_files_timelines_and_components(
self.assert_ids_are_unique(
tls, 61
) # 10 marker timelines + 1 slider timeline + 50 components

def test_create_component_with_higher_id_before_lower_id(self, marker_tl):
marker_tl.create_component(ComponentKind.MARKER, time=0, id=10)
marker_tl.create_component(ComponentKind.MARKER, time=1, id=1)
marker_tl.create_component(ComponentKind.MARKER, time=2)

assert [c.id for c in marker_tl.components] == ["10", "1", "11"]

def test_create_component_with_duplicate_ids(self, marker_tl):
marker_tl.create_component(ComponentKind.MARKER, time=0) # id=1; tl has id=0
marker_tl.create_component(ComponentKind.MARKER, time=1, id=1)
marker_tl.create_component(ComponentKind.MARKER, time=2, id=1)

assert [c.id for c in marker_tl.components] == ["1", "2", "3"]

@pytest.mark.parametrize("id", ["not an int", 3.1415, False])
def test_invalid_id(self, marker_tl, id):
with PatchPost("tilia.errors", Post.DISPLAY_ERROR) as error:
marker_tl.create_component(ComponentKind.MARKER, time=1, id=id)
error.assert_called()
assert marker_tl.components[0].id == "1"
42 changes: 29 additions & 13 deletions tilia/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,28 +275,44 @@ def on_record_state(self, action, no_repeat=False, repeat_identifier=""):
repeat_identifier=repeat_identifier,
)

def get_id(self) -> str:
def get_id(self, id: str | None = None) -> str:
"""
Returns an ID string.
IDs are unique accross timeline component and timelines.
IDs are unique across timeline component and timelines.
Other IDs might contain duplicates.
"""
timeline_ids = {c.id for c in self.timelines}

def invalid_id(id):
tilia.errors.display(tilia.errors.INVALID_ID, id)
return str(next(self._id_counter))

if id is None:
return str(next(self._id_counter))

if type(id) not in [int, str]:
return invalid_id(id)

try:
int_id = int(id)
except ValueError:
return invalid_id(id)

timeline_ids = {int(c.id) for c in self.timelines}
component_lists = [
tl.components for tl in self.timelines if tl.components is not None
]
component_ids = set()
for component_list in component_lists:
component_ids = component_ids.union({c.id for c in component_list})
component_ids = {
int(c.id) for component_list in component_lists for c in component_list
}
existing_ids = timeline_ids.union(component_ids)

next_id = next(self._id_counter)
# Find the highest existing ID and increment until counter is higher
if existing_ids:
highest_id = max(int(id_str) for id_str in existing_ids)
while next_id <= highest_id:
next_id = next(self._id_counter)
return str(next_id)
if int_id in existing_ids:
return str(next(self._id_counter))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

next(self._id_counter) might already be in existing_ids, no?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, see test_create_component_with_higher_id_before_lower_id. self._id_counter always increments from the largest input id, so there's no way it'll be smaller.


if not existing_ids or int_id > max(existing_ids):
self._id_counter = itertools.count(int_id + 1)
Comment on lines +312 to +313
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better than what we had.


return str(int_id)

def reset_id_generator(self):
self._id_counter = itertools.count()
Expand Down
3 changes: 3 additions & 0 deletions tilia/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ class Error(NamedTuple):
"Error creating score timeline",
"Duplicate or missing staff numbers. Staff numbers found: {}.\nDeleting timeline.",
)
INVALID_ID = Error(
"Error parsing id", "'{}' is not parsable as a valid id. Using generated id."
)


def display(error: Error, *args):
Expand Down
4 changes: 2 additions & 2 deletions tilia/timelines/base/timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __init__(
id: int | None = None,
**kwargs, # ignores components_hash
):
self.id = id if id is not None else get(Get.ID)
self.id = get(Get.ID, id)

self.name = name
self.is_visible = is_visible
Expand Down Expand Up @@ -189,7 +189,7 @@ def get_data(self, attr: str):
def create_component(
self, kind: ComponentKind, *args, id=None, **kwargs
) -> tuple[TC | None, str | None]:
component_id = id or get(Get.ID)
component_id = get(Get.ID, id)
success, component, reason = self.component_manager.create_component(
kind, self, component_id, *args, **kwargs
)
Expand Down