Skip to content

Commit ca633d8

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

File tree

2 files changed

+42
-25
lines changed

2 files changed

+42
-25
lines changed

src/crystal/browser/new_group.py

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# TODO: Consider extracting functions shared between dialogs to own module
22
from collections.abc import Callable
33
from crystal.browser.entitytree import ResourceGroupNode, RootResourceNode
4+
from crystal.browser.new_group_info import NewGroupDialogInfo
45
from crystal.browser.new_root_url import (
56
fields_hide_hint_when_focused, NewRootUrlDialog,
67
)
@@ -32,7 +33,7 @@
3233

3334
class NewGroupDialog:
3435
_INITIAL_URL_PATTERN_WIDTH = NewRootUrlDialog._INITIAL_URL_WIDTH
35-
_MAX_VISIBLE_PREVIEW_URLS = 100
36+
_MAX_VISIBLE_PREVIEW_URLS = NewGroupDialogInfo._MAX_VISIBLE_PREVIEW_URLS
3637

3738
# === Init ===
3839

@@ -458,30 +459,7 @@ def _update_preview_urls(self) -> None:
458459

459460
@classmethod
460461
def _calculate_preview_urls(cls, project: Project, url_pattern: str) -> list[str]:
461-
url_pattern_re = ResourceGroup.create_re_for_url_pattern(url_pattern)
462-
literal_prefix = ResourceGroup.literal_prefix_for_url_pattern(url_pattern)
463-
464-
url_pattern_is_literal = (len(literal_prefix) == len(url_pattern))
465-
if url_pattern_is_literal:
466-
member = project.get_resource(literal_prefix)
467-
if member is None:
468-
(matching_urls, approx_match_count) = ([], 0)
469-
else:
470-
(matching_urls, approx_match_count) = ([member.url], 1)
471-
else:
472-
(matching_urls, approx_match_count) = project.urls_matching_pattern(
473-
url_pattern_re, literal_prefix, limit=cls._MAX_VISIBLE_PREVIEW_URLS)
474-
475-
if len(matching_urls) == 0:
476-
return []
477-
else:
478-
more_count = approx_match_count - len(matching_urls)
479-
more_items = (
480-
[f'... about {more_count:n} more']
481-
if more_count != 0
482-
else []
483-
)
484-
return matching_urls + more_items
462+
return NewGroupDialogInfo._calculate_preview_urls(project, url_pattern)
485463

486464
# === Events ===
487465

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
Data and algorithms extracted from the "new_group" module that must be
3+
usable even if wx is not available.
4+
"""
5+
6+
from crystal.model import Project, ResourceGroup
7+
8+
9+
class NewGroupDialogInfo:
10+
_MAX_VISIBLE_PREVIEW_URLS = 100
11+
12+
# === Operations ===
13+
14+
@classmethod
15+
def _calculate_preview_urls(cls, project: Project, url_pattern: str) -> list[str]:
16+
url_pattern_re = ResourceGroup.create_re_for_url_pattern(url_pattern)
17+
literal_prefix = ResourceGroup.literal_prefix_for_url_pattern(url_pattern)
18+
19+
url_pattern_is_literal = (len(literal_prefix) == len(url_pattern))
20+
if url_pattern_is_literal:
21+
member = project.get_resource(literal_prefix)
22+
if member is None:
23+
(matching_urls, approx_match_count) = ([], 0)
24+
else:
25+
(matching_urls, approx_match_count) = ([member.url], 1)
26+
else:
27+
(matching_urls, approx_match_count) = project.urls_matching_pattern(
28+
url_pattern_re, literal_prefix, limit=cls._MAX_VISIBLE_PREVIEW_URLS)
29+
30+
if len(matching_urls) == 0:
31+
return []
32+
else:
33+
more_count = approx_match_count - len(matching_urls)
34+
more_items = (
35+
[f'... about {more_count:n} more']
36+
if more_count != 0
37+
else []
38+
)
39+
return matching_urls + more_items

0 commit comments

Comments
 (0)