Repository navigation
Expand file tree
/
Copy pathsheets.py
More file actions
114 lines (87 loc) · 3.21 KB
/
Copy pathsheets.py
File metadata and controls
114 lines (87 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from __future__ import annotations
from pathlib import Path
from typing import TYPE_CHECKING, Optional, TypedDict
if TYPE_CHECKING:
from .state import GroupSelectionState
class SheetIdentity(TypedDict):
name: Optional[str]
"""The sheet name is `None` on ST builds <4205
because the required API method does not exist there."""
occurrence: int
kind: Optional[str]
path: Optional[str]
def active_sheet_identity(window) -> Optional[SheetIdentity]:
sheet = window.active_sheet()
if sheet is None:
return None
return sheet_identity(sheet, window)
def find_sheet_by_identity_and_group(window, identity: SheetIdentity, group: int):
matching = [
sheet
for sheet in window.sheets_in_group(group)
if sheet_name(sheet) == identity["name"]
and type(sheet).__name__ == identity["kind"]
and sheet.file_name() == identity["path"]
]
for sheet in matching:
if sheet_occurrence(sheet, window) == identity["occurrence"]:
return sheet
return None
def sheet_identity(sheet, window) -> SheetIdentity:
return {
"name": sheet_name(sheet),
"occurrence": sheet_occurrence(sheet, window),
"kind": type(sheet).__name__,
"path": sheet.file_name(),
}
def sheet_occurrence(sheet, window) -> int:
group = sheet.group()
if group is None:
return 0
name = sheet_name(sheet)
kind = type(sheet).__name__
path = sheet.file_name()
matching_sheet_ids = [
candidate.id()
for candidate in window.sheets_in_group(group)
if sheet_name(candidate) == name
and type(candidate).__name__ == kind
and candidate.file_name() == path
]
try:
return matching_sheet_ids.index(sheet.id())
except ValueError:
return 0
def sheet_title(sheet) -> str:
file_name = sheet.file_name()
if file_name:
return Path(file_name).name
return "Untitled"
def sheet_name(sheet) -> Optional[str]:
get_name = getattr(sheet, "name", None)
return get_name() if get_name else None
def sheet_is_dirty(sheet) -> bool:
view = sheet.view()
return view is not None and view.is_dirty()
def identities_have_dirty_sheet(identities: list[SheetIdentity], window, group: int) -> bool:
return any(
(sheet := find_sheet_by_identity_and_group(window, identity, group)) is not None
and sheet_is_dirty(sheet)
for identity in identities
)
def find_live_sheets(window, identities: list[SheetIdentity], group: int) -> list[object]:
return [
live_sheet
for identity in identities
if (live_sheet := find_sheet_by_identity_and_group(window, identity, group)) is not None
]
def apply_group_selection(window, selection: GroupSelectionState, group: int) -> bool:
sheets_to_select = find_live_sheets(window, selection["selected_sheets"], group)
if not sheets_to_select:
return False
active_sheet_index = selection["active_sheet_index"]
if active_sheet_index is None or not 0 <= active_sheet_index < len(sheets_to_select):
active_sheet_index = 0
window.select_sheets(sheets_to_select)
window.focus_sheet(sheets_to_select[active_sheet_index])
return True