omdev 0.0.0.dev463__py3-none-any.whl → 0.0.0.dev465__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of omdev might be problematic. Click here for more details.

@@ -0,0 +1,33 @@
1
+ # MIT License
2
+ #
3
+ # Copyright (c) 2023 Darren Burns
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
6
+ # documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
7
+ # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
8
+ # persons to whom the Software is furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
11
+ # Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
14
+ # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
15
+ # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16
+ # OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17
+ #
18
+ # https://github.com/darrenburns/textual-autocomplete/tree/0344cd3eb3383cbbd80e01b035ed808ce53cef4d
19
+ from .matching import ( # noqa
20
+ FuzzySearch,
21
+ FuzzyMatcher,
22
+ )
23
+
24
+ from .paths import ( # noqa
25
+ PathAutoComplete,
26
+ )
27
+
28
+ from .widget import ( # noqa
29
+ AutoCompleteItem,
30
+ AutoCompleteItemHit,
31
+ AutoCompleteList,
32
+ AutoComplete,
33
+ )
@@ -0,0 +1,226 @@
1
+ import functools
2
+ import operator
3
+ import re
4
+ import typing as ta
5
+
6
+ import rich.repr
7
+ from textual.cache import LRUCache
8
+ from textual.content import Content
9
+ from textual.style import Style
10
+
11
+
12
+ ##
13
+
14
+
15
+ class FuzzySearch:
16
+ """
17
+ Performs a fuzzy search.
18
+
19
+ Unlike a regex solution, this will finds all possible matches.
20
+ """
21
+
22
+ def __init__(
23
+ self,
24
+ case_sensitive: bool = False,
25
+ *,
26
+ cache_size: int = 1024 * 4,
27
+ ) -> None:
28
+ """
29
+ Initialize fuzzy search.
30
+
31
+ Args:
32
+ case_sensitive: Is the match case sensitive?
33
+ cache_size: Number of queries to cache.
34
+ """
35
+
36
+ self.case_sensitive = case_sensitive
37
+ self.cache: LRUCache[tuple[str, str], tuple[float, ta.Sequence[int]]] = LRUCache(cache_size)
38
+
39
+ def match(self, query: str, candidate: str) -> tuple[float, ta.Sequence[int]]:
40
+ """
41
+ Match against a query.
42
+
43
+ Args:
44
+ query: The fuzzy query.
45
+ candidate: A candidate to check,.
46
+
47
+ Returns:
48
+ A pair of (score, tuple of offsets). `(0, ())` for no result.
49
+ """
50
+
51
+ cache_key = (query, candidate)
52
+ if cache_key in self.cache:
53
+ return self.cache[cache_key]
54
+ default: tuple[float, ta.Sequence[int]] = (0.0, [])
55
+ result = max(self._match(query, candidate), key=operator.itemgetter(0), default=default)
56
+ self.cache[cache_key] = result
57
+ return result
58
+
59
+ @classmethod
60
+ @functools.lru_cache(maxsize=1024)
61
+ def get_first_letters(cls, candidate: str) -> frozenset[int]:
62
+ return frozenset({match.start() for match in re.finditer(r'\w+', candidate)})
63
+
64
+ def score(self, candidate: str, positions: ta.Sequence[int]) -> float:
65
+ """
66
+ Score a search.
67
+
68
+ Args:
69
+ search: Search object.
70
+
71
+ Returns:
72
+ Score.
73
+ """
74
+
75
+ first_letters = self.get_first_letters(candidate)
76
+ # This is a heuristic, and can be tweaked for better results.
77
+ # Boost first letter matches.
78
+ offset_count = len(positions)
79
+ score: float = offset_count + len(first_letters.intersection(positions))
80
+
81
+ groups = 1
82
+ last_offset, *offsets = positions
83
+ for offset in offsets:
84
+ if offset != last_offset + 1:
85
+ groups += 1
86
+ last_offset = offset
87
+
88
+ # Boost to favor less groups
89
+ normalized_groups = (offset_count - (groups - 1)) / offset_count
90
+ score *= 1 + (normalized_groups * normalized_groups)
91
+ return score
92
+
93
+ def _match(self, query: str, candidate: str) -> ta.Iterable[tuple[float, ta.Sequence[int]]]:
94
+ letter_positions: list[list[int]] = []
95
+ position = 0
96
+
97
+ if not self.case_sensitive:
98
+ candidate = candidate.lower()
99
+ query = query.lower()
100
+ score = self.score
101
+ if query in candidate:
102
+ # Quick exit when the query exists as a substring
103
+ query_location = candidate.rfind(query)
104
+ offsets = list(range(query_location, query_location + len(query)))
105
+ yield (
106
+ score(candidate, offsets) * (2.0 if candidate == query else 1.5),
107
+ offsets,
108
+ )
109
+ return
110
+
111
+ for offset, letter in enumerate(query):
112
+ last_index = len(candidate) - offset
113
+ positions: list[int] = []
114
+ letter_positions.append(positions)
115
+ index = position
116
+ while (location := candidate.find(letter, index)) != -1:
117
+ positions.append(location)
118
+ index = location + 1
119
+ if index >= last_index:
120
+ break
121
+ if not positions:
122
+ yield (0.0, ())
123
+ return
124
+ position = positions[0] + 1
125
+
126
+ possible_offsets: list[list[int]] = []
127
+ query_length = len(query)
128
+
129
+ def get_offsets(offsets: list[int], positions_index: int) -> None:
130
+ """
131
+ Recursively match offsets.
132
+
133
+ Args:
134
+ offsets: A list of offsets.
135
+ positions_index: Index of query letter.
136
+ """
137
+
138
+ for offset in letter_positions[positions_index]:
139
+ if not offsets or offset > offsets[-1]:
140
+ new_offsets = [*offsets, offset]
141
+ if len(new_offsets) == query_length:
142
+ possible_offsets.append(new_offsets)
143
+ else:
144
+ get_offsets(new_offsets, positions_index + 1)
145
+
146
+ get_offsets([], 0)
147
+
148
+ for offsets in possible_offsets:
149
+ yield score(candidate, offsets), offsets
150
+
151
+
152
+ @rich.repr.auto
153
+ class FuzzyMatcher:
154
+ """A fuzzy matcher."""
155
+
156
+ def __init__(
157
+ self,
158
+ query: str,
159
+ *,
160
+ match_style: Style | None = None,
161
+ case_sensitive: bool = False,
162
+ ) -> None:
163
+ """
164
+ Initialize the fuzzy matching object.
165
+
166
+ Args:
167
+ query: A query as typed in by the user.
168
+ match_style: The style to use to highlight matched portions of a string.
169
+ case_sensitive: Should matching be case sensitive?
170
+ """
171
+
172
+ self._query = query
173
+ self._match_style = Style(reverse=True) if match_style is None else match_style
174
+ self._case_sensitive = case_sensitive
175
+ self.fuzzy_search = FuzzySearch()
176
+
177
+ @property
178
+ def query(self) -> str:
179
+ """The query string to look for."""
180
+
181
+ return self._query
182
+
183
+ @property
184
+ def match_style(self) -> Style:
185
+ """The style that will be used to highlight hits in the matched text."""
186
+
187
+ return self._match_style
188
+
189
+ @property
190
+ def case_sensitive(self) -> bool:
191
+ """Is this matcher case sensitive?"""
192
+
193
+ return self._case_sensitive
194
+
195
+ def match(self, candidate: str) -> float:
196
+ """
197
+ Match the candidate against the query.
198
+
199
+ Args:
200
+ candidate: Candidate string to match against the query.
201
+
202
+ Returns:
203
+ Strength of the match from 0 to 1.
204
+ """
205
+
206
+ return self.fuzzy_search.match(self.query, candidate)[0]
207
+
208
+ def highlight(self, candidate: str) -> Content:
209
+ """
210
+ Highlight the candidate with the fuzzy match.
211
+
212
+ Args:
213
+ candidate: The candidate string to match against the query.
214
+
215
+ Returns:
216
+ A [`Text`][rich.text.Text] object with highlighted matches.
217
+ """
218
+
219
+ content = Content.from_markup(candidate)
220
+ score, offsets = self.fuzzy_search.match(self.query, candidate)
221
+ if not score:
222
+ return content
223
+ for offset in offsets:
224
+ if not candidate[offset].isspace():
225
+ content = content.stylize(self._match_style, offset, offset + 1)
226
+ return content
@@ -0,0 +1,202 @@
1
+ import os
2
+ import pathlib
3
+ import typing as ta
4
+
5
+ from textual.cache import LRUCache
6
+ from textual.content import Content
7
+ from textual.widgets import Input
8
+
9
+ from .widget import AutoComplete
10
+ from .widget import AutoCompleteItem
11
+
12
+
13
+ ##
14
+
15
+
16
+ class PathAutoCompleteItem(AutoCompleteItem):
17
+ def __init__(self, completion: str, path: pathlib.Path) -> None:
18
+ super().__init__(completion)
19
+
20
+ self.path = path
21
+
22
+
23
+ def default_path_input_sort_key(item: PathAutoCompleteItem) -> tuple[bool, bool, str]:
24
+ """
25
+ Sort key function for results within the dropdown.
26
+
27
+ Args:
28
+ item: The PathAutoCompleteItem to get a sort key for.
29
+
30
+ Returns:
31
+ A tuple of (is_dotfile, is_file, lowercase_name) for sorting.
32
+ """
33
+
34
+ name = item.path.name
35
+ is_dotfile = name.startswith('.')
36
+ return (not item.path.is_dir(), not is_dotfile, name.lower())
37
+
38
+
39
+ class PathAutoComplete(AutoComplete):
40
+ def __init__(
41
+ self,
42
+ target: Input | str,
43
+ path: str | pathlib.Path = '.',
44
+ *,
45
+ show_dotfiles: bool = True,
46
+ sort_key: ta.Callable[[PathAutoCompleteItem], ta.Any] = default_path_input_sort_key,
47
+ folder_prefix: Content = Content('📂'),
48
+ file_prefix: Content = Content('📄'),
49
+ prevent_default_enter: bool = True,
50
+ prevent_default_tab: bool = True,
51
+ cache_size: int = 100,
52
+ name: str | None = None,
53
+ id: str | None = None, # noqa
54
+ classes: str | None = None,
55
+ disabled: bool = False,
56
+ ) -> None:
57
+ """
58
+ An autocomplete widget for filesystem paths.
59
+
60
+ Args:
61
+ target: The target input widget to autocomplete.
62
+ path: The base path to autocomplete from.
63
+ show_dotfiles: Whether to show dotfiles (files/dirs starting with ".").
64
+ sort_key: Function to sort the dropdown items.
65
+ folder_prefix: The prefix for folder items (e.g. 📂).
66
+ file_prefix: The prefix for file items (e.g. 📄).
67
+ prevent_default_enter: Whether to prevent the default enter behavior.
68
+ prevent_default_tab: Whether to prevent the default tab behavior.
69
+ cache_size: The number of directories to cache.
70
+ name: The name of the widget.
71
+ id: The DOM node id of the widget.
72
+ classes: The CSS classes of the widget.
73
+ disabled: Whether the widget is disabled.
74
+ """
75
+
76
+ super().__init__(
77
+ target,
78
+ None,
79
+ prevent_default_enter=prevent_default_enter,
80
+ prevent_default_tab=prevent_default_tab,
81
+ name=name,
82
+ id=id,
83
+ classes=classes,
84
+ disabled=disabled,
85
+ )
86
+
87
+ self.path = pathlib.Path(path) if isinstance(path, str) else path
88
+ self.show_dotfiles = show_dotfiles
89
+ self.sort_key = sort_key
90
+ self.folder_prefix = folder_prefix
91
+ self.file_prefix = file_prefix
92
+ self._directory_cache: LRUCache[str, list[os.DirEntry[str]]] = LRUCache(cache_size)
93
+
94
+ def get_candidates(self, target_state: AutoComplete.TargetState) -> list[AutoCompleteItem]:
95
+ """
96
+ Get the candidates for the current path segment.
97
+
98
+ This is called each time the input changes or the cursor position changes/
99
+ """
100
+
101
+ current_input = target_state.text[: target_state.cursor_position]
102
+
103
+ if '/' in current_input:
104
+ last_slash_index = current_input.rindex('/')
105
+ path_segment = current_input[:last_slash_index] or '/'
106
+ directory = self.path / path_segment if path_segment != '/' else self.path
107
+ else:
108
+ directory = self.path
109
+
110
+ # Use the directory path as the cache key
111
+ cache_key = str(directory)
112
+ cached_entries = self._directory_cache.get(cache_key)
113
+
114
+ if cached_entries is not None:
115
+ entries = cached_entries
116
+ else:
117
+ try:
118
+ entries = list(os.scandir(directory))
119
+ self._directory_cache[cache_key] = entries
120
+ except OSError:
121
+ return []
122
+
123
+ results: list[PathAutoCompleteItem] = []
124
+ for entry in entries:
125
+ # Only include the entry name, not the full path
126
+ completion = entry.name
127
+ if not self.show_dotfiles and completion.startswith('.'):
128
+ continue
129
+ if entry.is_dir():
130
+ completion += '/'
131
+ results.append(PathAutoCompleteItem(completion, path=pathlib.Path(entry.path)))
132
+
133
+ results.sort(key=self.sort_key)
134
+ folder_prefix = self.folder_prefix
135
+ file_prefix = self.file_prefix
136
+ return [
137
+ AutoCompleteItem(
138
+ item.main,
139
+ prefix=folder_prefix if item.path.is_dir() else file_prefix,
140
+ )
141
+ for item in results
142
+ ]
143
+
144
+ def get_search_string(self, target_state: AutoComplete.TargetState) -> str:
145
+ """Return only the current path segment for searching in the dropdown."""
146
+
147
+ current_input = target_state.text[: target_state.cursor_position]
148
+
149
+ if '/' in current_input:
150
+ last_slash_index = current_input.rindex('/')
151
+ search_string = current_input[last_slash_index + 1:]
152
+ return search_string
153
+ else:
154
+ return current_input
155
+
156
+ def apply_completion(self, value: str, state: AutoComplete.TargetState) -> None:
157
+ """Apply the completion by replacing only the current path segment."""
158
+
159
+ target = self.target
160
+ current_input = state.text
161
+ cursor_position = state.cursor_position
162
+
163
+ # There's a slash before the cursor, so we only want to replace the text after the last slash with the selected
164
+ # value
165
+ try:
166
+ replace_start_index = current_input.rindex('/', 0, cursor_position)
167
+ except ValueError:
168
+ # No slashes, so we do a full replacement
169
+ new_value = value
170
+ new_cursor_position = len(value)
171
+ else:
172
+ # Keep everything before and including the slash before the cursor.
173
+ path_prefix = current_input[: replace_start_index + 1]
174
+ new_value = path_prefix + value
175
+ new_cursor_position = len(path_prefix) + len(value)
176
+
177
+ with self.prevent(Input.Changed):
178
+ target.value = new_value
179
+ target.cursor_position = new_cursor_position
180
+
181
+ def post_completion(self) -> None:
182
+ if not self.target.value.endswith('/'):
183
+ self.action_hide()
184
+
185
+ def should_show_dropdown(self, search_string: str) -> bool:
186
+ return (
187
+ super().should_show_dropdown(search_string) or
188
+ (
189
+ (search_string == '' and self.target.value != '') and
190
+ self.option_list.option_count > 1
191
+ )
192
+ )
193
+
194
+ def clear_directory_cache(self) -> None:
195
+ """
196
+ Clear the directory cache. If you know that the contents of the directory have changed, you can call this method
197
+ to invalidate the cache.
198
+ """
199
+
200
+ self._directory_cache.clear()
201
+ target_state = self._get_target_state()
202
+ self._rebuild_options(target_state, self.get_search_string(target_state))