/usr/share/pyshared/xapers/nci/search.py is in xapers 0.5.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | import os
import subprocess
import urwid
from xapers.database import Database, DatabaseLockError
############################################################
def xclip(text, isfile=False):
"""Copy text or file contents into X clipboard."""
f = None
if isfile:
f = open(text, 'r')
sin = f
else:
sin = subprocess.PIPE
p = subprocess.Popen(' '.join(["xclip", "-i"]),
shell=True,
stdin=sin)
p.communicate(text)
if f:
f.close()
############################################################
class DocListItem(urwid.WidgetWrap):
def __init__(self, doc):
self.doc = doc
self.matchp = doc.matchp
self.docid = self.doc.docid
# fill the default attributes for the fields
self.fields = {}
for field in ['sources', 'tags', 'title', 'authors', 'year', 'summary']:
self.fields[field] = urwid.Text('')
self.fields['sources'].set_text(' '.join(self.doc.get_sids()))
self.fields['tags'].set_text(' '.join(self.doc.get_tags()))
data = self.doc.get_bibdata()
if data:
if 'title' in data:
self.fields['title'].set_text(data['title'])
if 'authors' in data:
astring = ' and '.join(data['authors'][:10])
if len(data['authors']) > 10:
astring = astring + ' et al.'
self.fields['authors'].set_text(astring)
if 'year' in data:
self.fields['year'].set_text(data['year'])
self.fields['summary'].set_text(self.doc.get_data())
self.c1width = 10
self.rowHeader = urwid.AttrMap(
urwid.Text('id:%s (%s)' % (self.docid, self.matchp)),
'head', 'head_focus')
# FIXME: how do we hightlight everything in pile during focus?
w = urwid.Pile(
[
urwid.Divider('-'),
self.rowHeader,
self.docfield('sources'),
self.docfield('tags'),
self.docfield('title'),
self.docfield('authors'),
self.docfield('year'),
self.docfield('summary'),
]
,
focus_item=1)
self.__super.__init__(w)
def docfield(self, field):
attr_map = field
return urwid.Columns(
[
('fixed', self.c1width,
urwid.AttrMap(
urwid.Text(field + ':'),
'field', 'field_focus')),
urwid.AttrMap(
self.fields[field],
attr_map)
]
)
def selectable(self):
return True
def keypress(self, size, key):
return key
############################################################
class Search(urwid.WidgetWrap):
palette = [
('head', 'dark blue, bold', ''),
('head_focus', 'white, bold', 'dark blue'),
('field', 'light gray', ''),
('field_focus', '', 'light gray'),
('sources', 'light magenta, bold', ''),
('tags', 'dark green, bold', ''),
('title', 'yellow', ''),
('authors', 'dark cyan, bold', ''),
('year', 'dark red', '',),
]
keys = {
'n': "nextEntry",
'p': "prevEntry",
'down': "nextEntry",
'up': "prevEntry",
'enter': "viewFile",
'u': "viewURL",
'b': "viewBibtex",
'+': "addTags",
'-': "removeTags",
'a': "archive",
'meta i': "copyID",
'meta f': "copyPath",
'meta u': "copyURL",
'meta b': "copyBibtex",
}
def __init__(self, ui, query=None):
self.ui = ui
self.ui.set_header("Search: " + query)
docs = self.ui.db.search(query, limit=20)
if len(docs) == 0:
self.ui.set_status('No documents found.')
items = []
for doc in docs:
items.append(DocListItem(doc))
self.lenitems = len(items)
self.listwalker = urwid.SimpleListWalker(items)
self.listbox = urwid.ListBox(self.listwalker)
w = self.listbox
self.__super.__init__(w)
##########
def nextEntry(self):
"""next entry"""
entry, pos = self.listbox.get_focus()
if not entry: return
if pos + 1 >= self.lenitems: return
self.listbox.set_focus(pos + 1)
def prevEntry(self):
"""previous entry"""
entry, pos = self.listbox.get_focus()
if not entry: return
if pos == 0: return
self.listbox.set_focus(pos - 1)
def viewFile(self):
"""open document file"""
entry = self.listbox.get_focus()[0]
if not entry: return
path = entry.doc.get_fullpaths()
if not path:
self.ui.set_status('No file for document id:%s.' % entry.docid)
return
path = path[0].replace(' ','\ ')
if not os.path.exists(path):
self.ui.set_status('ERROR: id:%s: file not found.' % entry.docid)
return
self.ui.set_status('opening file: %s...' % path)
subprocess.call(' '.join(['nohup', 'xdg-open', path, '&']),
shell=True,
stdout=open('/dev/null','w'),
stderr=open('/dev/null','w'))
def viewURL(self):
"""open document URL in browser"""
entry = self.listbox.get_focus()[0]
if not entry: return
urls = entry.doc.get_urls()
if not urls:
self.ui.set_status('ERROR: id:%s: no URLs found.' % entry.docid)
return
# FIXME: open all instead of just first?
url = urls[0]
self.ui.set_status('opening url: %s...' % url)
subprocess.call(' '.join(['nohup', 'xdg-open', url, '&']),
shell=True,
stdout=open('/dev/null','w'),
stderr=open('/dev/null','w'))
def viewBibtex(self):
"""view document bibtex"""
entry = self.listbox.get_focus()[0]
if not entry: return
self.ui.newbuffer(['bibview', 'id:' + entry.docid])
def copyID(self):
"""copy document ID to clipboard"""
entry = self.listbox.get_focus()[0]
if not entry: return
docid = "id:%s" % entry.docid
xclip(docid)
self.ui.set_status('docid yanked: %s' % docid)
def copyPath(self):
"""copy document file path to clipboard"""
entry = self.listbox.get_focus()[0]
if not entry: return
path = entry.doc.get_fullpaths()[0]
if not path:
self.ui.set_status('ERROR: id:%s: file path not found.' % entry.docid)
return
xclip(path)
self.ui.set_status('path yanked: %s' % path)
def copyURL(self):
"""copy document URL to clipboard"""
entry = self.listbox.get_focus()[0]
if not entry: return
urls = entry.doc.get_urls()
if not urls:
self.ui.set_status('ERROR: id:%s: URL not found.' % entry.docid)
return
# FIXME: copy all instead of just first?
url = urls[0]
xclip(url)
self.ui.set_status('url yanked: %s' % url)
def copyBibtex(self):
"""copy document bibtex to clipboard"""
entry = self.listbox.get_focus()[0]
if not entry: return
bibtex = entry.doc.get_bibpath()
if not bibtex:
self.ui.set_status('ERROR: id:%s: bibtex not found.' % entry.docid)
return
xclip(bibtex, isfile=True)
self.ui.set_status('bibtex yanked: %s' % bibtex)
def addTags(self):
"""add tags from document (space separated)"""
self.promptTag('+')
def removeTags(self):
"""remove tags from document (space separated)"""
self.promptTag('-')
def promptTag(self, sign):
entry = self.listbox.get_focus()[0]
if not entry: return
if sign is '+':
# FIXME: autocomplete to existing tags
prompt = 'add tags: '
elif sign is '-':
# FIXME: autocomplete to doc tags only
prompt = 'remove tags: '
urwid.connect_signal(self.ui.prompt(prompt), 'done', self._promptTag_done, sign)
def _promptTag_done(self, tag_string, sign):
self.ui.view.set_focus('body')
urwid.disconnect_signal(self, self.ui.prompt, 'done', self._promptTag_done)
if not tag_string:
self.ui.set_status('No tags set.')
return
entry = self.listbox.get_focus()[0]
try:
with Database(self.ui.xroot, writable=True) as db:
doc = db[entry.docid]
tags = tag_string.split()
if sign is '+':
doc.add_tags(tags)
msg = "Added tags: %s" % (tag_string)
elif sign is '-':
doc.remove_tags(tags)
msg = "Removed tags: %s" % (tag_string)
doc.sync()
tags = doc.get_tags()
entry.fields['tags'].set_text(' '.join(tags))
except DatabaseLockError as e:
msg = e.msg
self.ui.set_status(msg)
def archive(self):
"""archive document (remove 'new' tag)"""
self._promptTag_done('new', '-')
def keypress(self, size, key):
if key in self.keys:
cmd = "self.%s()" % (self.keys[key])
eval(cmd)
else:
self.ui.keypress(key)
|