Skip to content

Commit 56be849

Browse files
committed
Refactor extract parts of entitytree module that need to be usable without wx
1 parent 30ebb31 commit 56be849

File tree

2 files changed

+64
-26
lines changed

2 files changed

+64
-26
lines changed

src/crystal/browser/entitytree.py

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
from __future__ import annotations
22

33
from concurrent.futures import CancelledError, Future
4+
from crystal.browser.entitytree_info import (
5+
_GroupedNodeInfo,
6+
ResourceGroupNodeInfo,
7+
RootResourceNodeInfo,
8+
)
49
from crystal.browser.icons import (
510
BADGED_ART_PROVIDER_TREE_NODE_ICON, BADGED_TREE_NODE_ICON, TREE_NODE_ICONS,
611
)
@@ -1203,18 +1208,7 @@ def calculate_title(self) -> str:
12031208

12041209
@staticmethod
12051210
def calculate_title_of(root_resource: RootResource) -> str:
1206-
project = root_resource.project
1207-
display_url = project.get_display_url(root_resource.url)
1208-
if root_resource.name != '':
1209-
entity_title_format = project.entity_title_format # cache
1210-
if entity_title_format == 'name_url':
1211-
return f'{root_resource.name} - {display_url}'
1212-
elif entity_title_format == 'url_name':
1213-
return f'{display_url} - {root_resource.name}'
1214-
else:
1215-
assert_never(entity_title_format)
1216-
else:
1217-
return f'{display_url}'
1211+
return RootResourceNodeInfo.calculate_title_of(root_resource)
12181212

12191213
@override
12201214
@property
@@ -1376,8 +1370,8 @@ def __hash__(self):
13761370
class _GroupedNode(Node): # abstract
13771371
entity_tooltip: str # abstract
13781372

1379-
ICON = '📁'
1380-
ICON_TRUNCATION_FIX = ' '
1373+
ICON = _GroupedNodeInfo.ICON
1374+
ICON_TRUNCATION_FIX = _GroupedNodeInfo.ICON_TRUNCATION_FIX
13811375

13821376
def __init__(self,
13831377
resource_group: ResourceGroup,
@@ -1460,18 +1454,7 @@ def calculate_title(self) -> str:
14601454

14611455
@staticmethod
14621456
def calculate_title_of(resource_group: ResourceGroup) -> str:
1463-
project = resource_group.project
1464-
display_url = project.get_display_url(resource_group.url_pattern)
1465-
if resource_group.name != '':
1466-
entity_title_format = project.entity_title_format # cache
1467-
if entity_title_format == 'name_url':
1468-
return f'{resource_group.name} - {display_url}'
1469-
elif entity_title_format == 'url_name':
1470-
return f'{display_url} - {resource_group.name}'
1471-
else:
1472-
assert_never(entity_title_format)
1473-
else:
1474-
return f'{display_url}'
1457+
return ResourceGroupNodeInfo.calculate_title_of(resource_group)
14751458

14761459
@override
14771460
@property
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
Data and algorithms extracted from the "entitytree" module that must be
3+
usable even if wx is not available.
4+
"""
5+
6+
from typing import assert_never, TYPE_CHECKING
7+
8+
if TYPE_CHECKING:
9+
from crystal.model.resource_group import ResourceGroup, RootResource
10+
11+
12+
class RootResourceNodeInfo:
13+
ICON = '⚓️'
14+
ICON_TRUNCATION_FIX = ''
15+
16+
# === Properties ===
17+
18+
@staticmethod
19+
def calculate_title_of(root_resource: 'RootResource') -> str:
20+
project = root_resource.project
21+
display_url = project.get_display_url(root_resource.url)
22+
if root_resource.name != '':
23+
entity_title_format = project.entity_title_format # cache
24+
if entity_title_format == 'name_url':
25+
return f'{root_resource.name} - {display_url}'
26+
elif entity_title_format == 'url_name':
27+
return f'{display_url} - {root_resource.name}'
28+
else:
29+
assert_never(entity_title_format)
30+
else:
31+
return f'{display_url}'
32+
33+
34+
class _GroupedNodeInfo:
35+
ICON = '📁'
36+
ICON_TRUNCATION_FIX = ' '
37+
38+
39+
class ResourceGroupNodeInfo(_GroupedNodeInfo):
40+
# === Properties ===
41+
42+
@staticmethod
43+
def calculate_title_of(resource_group: 'ResourceGroup') -> str:
44+
project = resource_group.project
45+
display_url = project.get_display_url(resource_group.url_pattern)
46+
if resource_group.name != '':
47+
entity_title_format = project.entity_title_format # cache
48+
if entity_title_format == 'name_url':
49+
return f'{resource_group.name} - {display_url}'
50+
elif entity_title_format == 'url_name':
51+
return f'{display_url} - {resource_group.name}'
52+
else:
53+
assert_never(entity_title_format)
54+
else:
55+
return f'{display_url}'

0 commit comments

Comments
 (0)