This file is indexed.

/usr/lib/python2.7/dist-packages/xapers/nci/search.py is in xapers 0.7.1-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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
"""
This file is part of xapers.

Xapers is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.

Xapers is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with xapers.  If not, see <http://www.gnu.org/licenses/>.

Copyright 2012-2017
Jameson Rollins <jrollins@finestructure.net>
"""

import os
import urwid
import subprocess
import collections

from ..cli import initdb
from ..database import DatabaseLockError

PALETTE = [
    ('head', 'dark blue, bold', ''),
    ('head focus', 'white, bold', 'dark blue'),
    ('field', 'light gray', ''),
    ('field focus', '', 'dark gray', '', '', 'g19'),
    ('tags', 'dark green', ''),
    ('tags focus', 'light green', 'dark blue'),
    ('title', 'yellow', ''),
    ('title focus', 'yellow', 'dark gray', '', 'yellow', 'g19'),
    ('authors', 'light cyan', ''),
    ('authors focus', 'light cyan', 'dark gray', '', 'light cyan', 'g19'),
    ('journal', 'dark magenta', '',),
    ('journal focus', 'dark magenta', 'dark gray', '', 'dark magenta', 'g19'),
    ('bibkey', 'dark magenta', '',),
    ('bibkey focus', 'dark magenta', 'dark gray', '', 'dark magenta', 'g19'),
    ]

############################################################

def xdg_open(path):
    """open document file"""
    with open(os.devnull) as devnull:
        subprocess.Popen(['xdg-open', path],
                         stdin=devnull,
                         stdout=devnull,
                         stderr=devnull)

def xclip(text):
    """Copy text into X clipboard."""
    p = subprocess.Popen(["xclip", "-i"],
                         stdin=subprocess.PIPE)
    p.communicate(text.encode('utf-8'))

############################################################

class DocItem(urwid.WidgetWrap):

    FIELDS = ['title',
              'authors',
              'journal',
              'year',
              'source',
              'bibkey',
              #'tags',
              #'file',
              #'summary',
              ]

    keys = collections.OrderedDict([
        ('enter', "viewFile"),
        ('u', "viewURL"),
        ('b', "viewBibtex"),
        ('t', "promptTag"),
        ('+', "addTags"),
        ('-', "removeTags"),
        ('meta i', "copyID"),
        ('meta u', "copyURL"),
        ('meta b', "copyBibtex"),
        ('meta k', "copyKey"),
        ('meta f', "copyPath"),
        ])

    def __init__(self, ui, doc, doc_ind, total_docs):
        self.ui = ui
        self.doc = doc
        self.docid = 'id:{}'.format(doc.docid)

        c1width = 10

        field_data = dict.fromkeys(self.FIELDS, '')

        field_data['tags'] = ' '.join(doc.get_tags())
        field_data['bibkey'] = doc.get_key() or ''

        bibdata = doc.get_bibdata()
        if bibdata:
            for field, value in bibdata.iteritems():
                if 'title' == field:
                    field_data[field] = value
                elif 'authors' == field:
                    field_data[field] = ' and '.join(value[:4])
                    if len(value) > 4:
                        field_data[field] += ' et al.'
                elif 'year' == field:
                    field_data[field] = value

                # FIXME: this translation should not be done here
                if field_data['journal'] == '':
                    if 'journal' == field:
                        field_data['journal'] = value
                    elif 'container-title' == field:
                        field_data['journal'] = value
                    elif 'arxiv' == field:
                        field_data['journal'] = 'arXiv.org'
                    elif 'dcc' == field:
                        field_data['journal'] = 'LIGO DCC'

        urls = doc.get_urls()
        if urls:
            field_data['source'] = urls[0]

        summary = doc.get_data()
        if not summary:
            summary = 'NO FILE'
        field_data['summary'] = summary

        def gen_field_row(field, value):
            if field in ['journal', 'year', 'source', 'bibkey']:
                color = 'journal'
            elif field in ['file']:
                color = 'field'
            else:
                color = field
            return urwid.Columns([
                ('fixed', c1width, urwid.Text(('field', field + ':'))),
                urwid.Text((color, value)),
                ])

        self.tag_field = urwid.Text(field_data['tags'])
        header = urwid.AttrMap(urwid.Columns([
            ('fixed', c1width, urwid.Text('%s' % (self.docid))),
            urwid.AttrMap(self.tag_field, 'tags'),
            urwid.Text('%s%% match (%s/%s)' % (doc.matchp, doc_ind, total_docs), align='right'),
            ]),
            'head')
        pile = [urwid.AttrMap(urwid.Divider(' '), '', ''), header] + \
               [gen_field_row(field, field_data[field]) for field in self.FIELDS]
        for f in doc.get_files():
            pile += [gen_field_row('file', os.path.basename(f))]
        w = urwid.AttrMap(urwid.AttrMap(urwid.Pile(pile), 'field'),
                          '',
                          {'head': 'head focus',
                           'field': 'field focus',
                           'tags': 'tags focus',
                           'title': 'title focus',
                           'authors': 'authors focus',
                           'journal': 'journal focus',
                           },
                          )

        self.__super.__init__(w)

    def keypress(self, size, key):
        if key in self.keys:
            cmd = eval("self.{}".format(self.keys[key]))
            cmd()
        else:
            return key

    ####################

    def viewFile(self):
        """open document file"""
        paths = self.doc.get_fullpaths()
        if not paths:
            self.ui.set_status('No file for document {}.'.format(self.docid))
            return
        for path in paths:
            if not os.path.exists(path):
                self.ui.error('{}: file not found.'.format(self.docid))
            else:
                self.ui.set_status('opening file: {}...'.format(path))
            xdg_open(path)

    def viewURL(self):
        """open document source URL in browser"""
        urls = self.doc.get_urls()
        if not urls:
            self.ui.set_status('No URLs for document {}.'.format(self.docid))
            return
        # FIXME: open all instead of just first?
        url = urls[0]
        self.ui.set_status('opening url: {}...'.format(url))
        xdg_open(url)

    def viewBibtex(self):
        """view document bibtex"""
        self.ui.newbuffer(['bibview', self.docid])

    def copyID(self):
        """copy document ID to clipboard"""
        xclip(self.docid)
        self.ui.set_status('yanked docid: {}'.format(self.docid))

    def copyKey(self):
        """copy document bibtex key to clipboard"""
        bibkey = self.doc.get_key()
        if not bibkey:
            self.ui.set_status('No bibtex key for document {}.'.format(self.docid))
            return
        xclip(bibkey)
        self.ui.set_status('yanked bibtex key: {}'.format(bibkey))

    def copyPath(self):
        """copy document file path to clipboard"""
        path = self.doc.get_fullpaths()[0]
        if not path:
            self.ui.set_status('No files for document {}.'.format(self.docid))
            return
        xclip(path)
        self.ui.set_status('yanked file path: {}'.format(path))

    def copyURL(self):
        """copy document source URL to clipboard"""
        urls = self.doc.get_urls()
        if not urls:
            self.ui.set_status('No URLs for document {}.'.format(self.docid))
            return
        # FIXME: copy all instead of just first?
        url = urls[0]
        xclip(url)
        self.ui.set_status('yanked source url: {}'.format(url))

    def copyBibtex(self):
        """copy document bibtex to clipboard"""
        bibtex = self.doc.get_bibtex()
        if not bibtex:
            self.ui.set_status('No bibtex for document {}.'.format(self.docid))
            return
        xclip(bibtex)
        self.ui.set_status('yanked bibtex: %s...' % bibtex.split('\n')[0])

    def addTags(self):
        """add tags to document (space separated)"""
        self.promptTag('+')

    def removeTags(self):
        """remove tags from document (space separated)"""
        self.promptTag('-')

    def promptTag(self, sign=None):
        """apply tags to document (space separated, +add/-remove)"""
        prompt = "tag document {} (+add -remove): ".format(self.docid)
        initial = ''
        if sign is '+':
            initial = '+'
        elif sign is '-':
            initial = '-'
        elif sign:
            raise ValueError("sign must be '+' or '-'.")
        if sign is '-':
            completions = self.doc.get_tags()
        else:
            completions = self.ui.db.get_tags()
        self.ui.prompt((self.applyTags, []),
                       prompt, initial=initial,
                       completions=completions,
                       history=self.ui.tag_history)

    def applyTags(self, tag_string):
        if not tag_string:
            self.ui.set_status("No tags set.")
            return
        try:
            with initdb(writable=True) as db:
                doc = db[self.doc.docid]
                for tag in tag_string.split():
                    if tag[0] == '+':
                        if tag[1:]:
                            doc.add_tags([tag[1:]])
                    elif tag[0] == '-':
                        doc.remove_tags([tag[1:]])
                    else:
                        doc.add_tags([tag])
                doc.sync()
                msg = "applied tags: {}".format(tag_string)
            tags = doc.get_tags()
            self.tag_field.set_text(' '.join(tags))
            if self.ui.tag_history and tag_string == self.ui.tag_history[-1]:
                pass
            else:
                self.ui.tag_history.append(tag_string)
        except DatabaseLockError as e:
            msg = e.msg
        self.ui.db.reopen()
        self.ui.set_status(msg)

############################################################

class DocWalker(urwid.ListWalker):
    def __init__(self, ui, docs):
        self.ui = ui
        self.docs = docs
        self.ndocs = len(docs)
        self.focus = 0
        self.items = {}

    def __getitem__(self, pos):
        if pos < 0:
            raise IndexError
        if pos not in self.items:
            self.items[pos] = DocItem(self.ui, self.docs[pos], pos+1, self.ndocs)
        return self.items[pos]

    def set_focus(self, focus):
        if focus == -1:
            focus = self.ndocs - 1
        self.focus = focus
        self._modified()

    def next_position(self, pos):
        return pos + 1

    def prev_position(self, pos):
        return pos - 1
        
############################################################

class Search(urwid.Frame):

    keys = collections.OrderedDict([
        ('n', "nextEntry"),
        ('down', "nextEntry"),
        ('p', "prevEntry"),
        ('up', "prevEntry"),
        ('N', "pageDown"),
        ('page down', "pageDown"),
        (' ', "pageDown"),
        ('P', "pageUp"),
        ('page up', "pageUp"),
        ('<', "firstEntry"),
        ('>', "lastEntry"),
        ('a', "archive"),
        ('o', "toggleSort"),
        ('l', "filterSearch"),
        ('meta S', "copySearch"),
        ('B', "viewBibtex"),
        ('T', "promptTag"),
        ('=', "refresh"),
        ])

    __sort = collections.deque(['relevance', 'year'])

    def __init__(self, ui, query=None):
        self.ui = ui
        self.query = query
        super(Search, self).__init__(urwid.SolidFill())

        if not self.ui.search_history or query != self.ui.search_history[-1]:
            self.ui.search_history.append(query)

        self.__set_search()

    @property
    def sort_order(self):
        return self.__sort[0]

    def __set_search(self):
        count = self.ui.db.count(self.query)
        if count == 0:
            self.ui.set_status('No documents found.')
            docs = []
        else:
            docs = [doc for doc in self.ui.db.search(self.query, sort=self.sort_order)]
        if count == 1:
            cstring = "%d result" % (count)
        else:
            cstring = "%d results" % (count)

        htxt = [('pack', urwid.Text("Search: ")),
                ('pack', urwid.AttrMap(urwid.Text("%s" % (self.query), align='left'), 'header_args')),
                ('pack', urwid.Text(" [{}]".format(self.sort_order))),
                urwid.Text(cstring, align='right'),
                ]
        header = urwid.AttrMap(urwid.Columns(htxt), 'header')
        self.set_header(header)

        self.lenitems = count
        self.docwalker = DocWalker(self.ui, docs)
        self.listbox = urwid.ListBox(self.docwalker)
        body = self.listbox
        self.set_body(body)

    def keypress(self, size, key):
        # reset the status on key presses
        self.ui.set_status()
        entry, pos = self.listbox.get_focus()
        # key used if keypress returns None
        if entry and not entry.keypress(size, key):
            return
        # check if we can use key
        elif key in self.keys:
            cmd = eval("self.%s" % (self.keys[key]))
            cmd(size, key)
        # else we didn't use key so return
        else:
            return key

    def help(self):
        lines = []
        def get_keys(o):
            for k, cmd in o.keys.items():
                yield (k, str(getattr(getattr(o, cmd), '__doc__')))
        yield (None, "Document commands:")
        for o in get_keys(DocItem):
            yield o
        yield (None, "Search commands:")
        for o in get_keys(Search):
            yield o

    ##########

    def refresh(self, size, key):
        """refresh current search"""
        self.ui.db.reopen()
        self.__set_search()
        # FIXME: try to reset position to closet place in search,
        # rather than resetting to the top

    def toggleSort(self, size, key):
        """toggle search sort order between year/relevance"""
        self.__sort.rotate()
        self.__set_search()

    def filterSearch(self, size, key):
        """modify current search or add additional terms"""
        prompt = 'filter search: {} '.format(self.query)
        self.ui.prompt((self.filterSearch_done, []),
                       prompt)

    def filterSearch_done(self, newquery):
        if not newquery:
            self.ui.set_status()
            return
        self.ui.newbuffer(['search', self.query, newquery])

    def nextEntry(self, size, key):
        """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, size, key):
        """previous entry"""
        entry, pos = self.listbox.get_focus()
        if not entry: return
        if pos == 0: return
        self.listbox.set_focus(pos - 1)

    def pageDown(self, size, key):
        """page down"""
        self.listbox.keypress(size, 'page down')
        # self.listbox.set_focus_valign('bottom')
        # self.prevEntry(None, None)

    def pageUp(self, size, key):
        """page up"""
        self.listbox.keypress(size, 'page up')
        # self.listbox.set_focus_valign('top')

    def lastEntry(self, size, key):
        """last entry"""
        self.listbox.set_focus(-1)

    def firstEntry(self, size, key):
        """first entry"""
        self.listbox.set_focus(0)

    def archive(self, size, key):
        """archive document (remove 'new' tag) and advance"""
        entry = self.listbox.get_focus()[0]
        if not entry:
            return
        entry.applyTags('-new')
        self.nextEntry(None, None)

    def copySearch(self, size, key):
        """copy current search string to clipboard"""
        xclip(self.query)
        self.ui.set_status('yanked search: {}'.format(self.query))

    def viewBibtex(self, size, key):
        """view bibtex for all documents in current search"""
        self.ui.newbuffer(['bibview', self.query])

    def promptTag(self, size, key):
        """tag all documents in current search"""
        prompt = "tag all (+add -remove): "
        initial = ''
        completions = self.ui.db.get_tags()
        self.ui.prompt((self.applyTags, []),
                       prompt, initial=initial,
                       completions=completions,
                       history=self.ui.tag_history)

    def applyTags(self, tag_string):
        """apply tags to current search (space separated, +add/-remove)"""
        if not tag_string:
            self.ui.set_status("No tags set.")
            return
        tags_add = []
        tags_sub = []
        for tag in tag_string.split():
            if tag[0] == '+':
                tags_add.append(tag[1:])
            elif tag[0] == '-':
                tags_sub.append(tag[1:])
            else:
                tags_add.append(tag)
        try:
            with initdb(writable=True) as db:
                count = db.count(self.query)
                for doc in db.search(self.query):
                    doc.add_tags(tags_add)
                    doc.remove_tags(tags_sub)
                    doc.sync()
            msg = "applied tags to {} docs: {}".format(count, tag_string)
            if not self.ui.tag_history or tag_string != self.ui.tag_history[-1]:
                self.ui.tag_history.append(tag_string)
        except DatabaseLockError as e:
            msg = e.msg
        self.refresh(None, None)
        self.ui.set_status(msg)