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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
# Copyright (C) 2011-2018 Patrick Totzke <patricktotzke@gmail.com>
# This file is released under the GNU GPL, version 3 or a later revision.
# For further details see the COPYING file
import urwid
import logging
from notmuch import NotmuchError
from .buffer import Buffer
from ..settings.const import settings
from ..widgets.search import ThreadlineWidget
class PipeWalker(urwid.ListWalker):
"""urwid.ListWalker that reads next items from a pipe and wraps them in
`containerclass` widgets for displaying
Attributes that should be considered publicly readable:
:attr lines: the lines obtained from the pipe
:type lines: list(`containerclass`)
"""
def __init__(self, pipe, containerclass, reverse=False, **kwargs):
self.pipe = pipe
self.kwargs = kwargs
self.containerclass = containerclass
self.lines = []
self.focus = 0
self.empty = False
self.direction = -1 if reverse else 1
def __len__(self):
while not self.empty:
self._get_next_item()
return len(self.lines)
def get_focus(self):
return self._get_at_pos(self.focus)
def set_focus(self, focus):
self.focus = focus
self._modified()
def get_next(self, start_from):
return self._get_at_pos(start_from + self.direction)
def get_prev(self, start_from):
return self._get_at_pos(start_from - self.direction)
def remove(self, obj):
next_focus = self.focus % len(self.lines)
if self.focus == len(self.lines) - 1 and self.empty:
next_focus = self.focus - 1
self.lines.remove(obj)
if self.lines:
self.set_focus(next_focus)
self._modified()
def _get_at_pos(self, pos):
if pos < 0: # pos too low
return (None, None)
elif pos > len(self.lines): # pos too high
return (None, None)
elif len(self.lines) > pos: # pos already cached
return (self.lines[pos], pos)
else: # pos not cached yet, look at next item from iterator
if self.empty: # iterator is empty
return (None, None)
else:
widget = self._get_next_item()
if widget:
return (widget, pos)
else:
return (None, None)
def _get_next_item(self):
if self.empty:
return None
try:
# the next line blocks until it can read from the pipe or
# EOFError is raised. No races here.
next_obj = self.pipe.recv()
next_widget = self.containerclass(next_obj, **self.kwargs)
self.lines.append(next_widget)
except EOFError:
logging.debug('EMPTY PIPE')
next_widget = None
self.empty = True
return next_widget
def get_lines(self):
return self.lines
class SearchBuffer(Buffer):
"""shows a result list of threads for a query"""
modename = 'search'
threads = []
def __init__(self, ui, initialquery='', sort_order=None):
self.dbman = ui.dbman
self.ui = ui
self.querystring = initialquery
default_order = settings.get('search_threads_sort_order')
self.sort_order = sort_order or default_order
self.result_count = 0
self.thread_count = 0
self.proc = None # process that fills our pipe
self.rebuild()
Buffer.__init__(self, ui, self.body)
def __str__(self):
formatstring = '[search] for "%s" (%d message%s in %d thread%s)'
return formatstring % (self.querystring,
self.result_count, 's' if self.result_count > 1 else '',
self.thread_count, 's' if self.thread_count > 1 else '')
def get_info(self):
info = {}
info['querystring'] = self.querystring
info['result_count'] = self.result_count
info['thread_count'] = self.thread_count
info['result_count_positive'] = 's' if self.result_count > 1 else ''
return info
def cleanup(self):
self.kill_filler_process()
def kill_filler_process(self):
"""
terminates the process that fills this buffers
:class:`~alot.walker.PipeWalker`.
"""
if self.proc:
if self.proc.is_alive():
self.proc.terminate()
def rebuild(self):
self.kill_filler_process()
order = self.sort_order
exclude_tags = settings.get_notmuch_setting('search', 'exclude_tags')
if exclude_tags:
exclude_tags = [t for t in exclude_tags.split(';') if t]
try:
self.result_count = self.dbman.count_messages(self.querystring)
self.thread_count = self.dbman.count_threads(self.querystring)
self.pipe, self.proc = self.dbman.get_threads(self.querystring,
order,
exclude_tags)
except NotmuchError:
self.ui.notify('malformed query string: %s' % self.querystring,
'error')
self.listbox = urwid.ListBox([])
self.body = self.listbox
return
self.threadlist = PipeWalker(self.pipe, ThreadlineWidget,
dbman=self.dbman)
self.listbox = urwid.ListBox(self.threadlist)
self.body = self.listbox
def get_selected_threadline(self):
"""
returns curently focussed :class:`alot.widgets.ThreadlineWidget`
from the result list.
"""
threadlinewidget, _ = self.threadlist.get_focus()
return threadlinewidget
def get_selected_thread(self):
"""returns currently selected :class:`~alot.db.Thread`"""
threadlinewidget = self.get_selected_threadline()
thread = None
if threadlinewidget:
thread = threadlinewidget.get_thread()
return thread
def focus_first(self):
self.body.set_focus(0)
def focus_last(self):
self.body.set_focus(len(self.threadlist) - 1)
|