/usr/lib/python2.7/dist-packages/xapers/cli.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 | """
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 sys
import sets
import shutil
import readline
from . import database
from .documents import Document
from .source import Sources, SourceError
from .parser import ParseError
from .bibtex import Bibtex, BibtexError
############################################################
def initdb(writable=False, create=False, force=False):
xroot = os.getenv('XAPERS_ROOT',
os.path.expanduser(os.path.join('~','.xapers','docs')))
try:
return database.Database(xroot, writable=writable, create=create, force=force)
except database.DatabaseUninitializedError as e:
print >>sys.stderr, e
print >>sys.stderr, "Import a document to initialize."
sys.exit(1)
except database.DatabaseInitializationError as e:
print >>sys.stderr, e
print >>sys.stderr, "Either clear the directory and add new files, or use 'retore' to restore from existing data."
sys.exit(1)
except database.DatabaseError as e:
print >>sys.stderr, e
sys.exit(1)
############################################################
# readline completion class
class Completer:
def __init__(self, words):
self.words = words
def terms(self, prefix, index):
matching_words = [
w for w in self.words if w.startswith(prefix)
]
try:
return matching_words[index]
except IndexError:
return None
def prompt_for_file(infile):
if infile:
print >>sys.stderr, 'file: %s' % infile
else:
readline.set_startup_hook()
readline.parse_and_bind('')
readline.set_completer()
infile = raw_input('file: ')
if infile == '':
infile = None
return infile
def prompt_for_source(db, sources):
if sources:
readline.set_startup_hook(lambda: readline.insert_text(sources[0]))
elif db:
sources = list(db.term_iter('source'))
readline.parse_and_bind("tab: complete")
completer = Completer(sources)
readline.set_completer(completer.terms)
readline.set_completer_delims(' ')
source = raw_input('source: ')
if source == '':
source = None
return source
def prompt_for_tags(db, tags):
# always prompt for tags, and append to initial
if tags:
print >>sys.stderr, 'initial tags: %s' % ' '.join(tags)
else:
tags = []
if db:
itags = list(db.term_iter('tag'))
else:
itags = None
readline.set_startup_hook()
readline.parse_and_bind("tab: complete")
completer = Completer(itags)
readline.set_completer(completer.terms)
readline.set_completer_delims(' ')
while True:
tag = raw_input('tag: ')
if tag and tag != '':
tags.append(tag.strip())
else:
break
return tags
############################################################
def print_doc_summary(doc):
docid = doc.docid
title = doc.get_title()
if not title:
title = ''
tags = doc.get_tags()
sources = doc.get_sids()
key = doc.get_key()
if not key:
key = ''
print "id:%d [%s] {%s} (%s) \"%s\"" % (
docid,
' '.join(sources),
key,
' '.join(tags),
title,
)
############################################################
def add(db, query_string, infile=None, sid=None, tags=None, prompt=False):
doc = None
bibtex = None
sources = Sources()
doc_sid = sid
source = None
file_data = None
if infile and infile is not True:
infile = os.path.expanduser(infile)
##################################
# if query provided, find single doc to update
if query_string:
if db.count(query_string) != 1:
print >>sys.stderr, "Search '%s' did not match a single document." % query_string
print >>sys.stderr, "Aborting."
sys.exit(1)
for doc in db.search(query_string):
break
##################################
# do fancy option prompting
if prompt:
doc_sids = []
if doc_sid:
doc_sids = [doc_sid]
# scan the file for source info
if infile is not True:
infile = prompt_for_file(infile)
print >>sys.stderr, "Scanning document for source identifiers..."
try:
ss = sources.scan_file(infile)
except ParseError as e:
print >>sys.stderr, "\n"
print >>sys.stderr, "Parse error: %s" % e
sys.exit(1)
if len(ss) == 0:
print >>sys.stderr, "0 source ids found."
else:
if len(ss) == 1:
print >>sys.stderr, "1 source id found:"
else:
print >>sys.stderr, "%d source ids found:" % (len(ss))
for sid in ss:
print >>sys.stderr, " %s" % (sid)
doc_sids += [s.sid for s in ss]
doc_sid = prompt_for_source(db, doc_sids)
tags = prompt_for_tags(db, tags)
if not query_string and not infile and not doc_sid:
print >>sys.stderr, "Must specify file or source to import, or query to update existing document."
sys.exit(1)
##################################
# process source and get bibtex
# check if source is a file, in which case interpret it as bibtex
if doc_sid and os.path.exists(doc_sid):
bibtex = doc_sid
elif doc_sid:
# get source object for sid string
try:
source = sources.match_source(doc_sid)
except SourceError as e:
print >>sys.stderr, e
sys.exit(1)
# check that the source doesn't match an existing doc
sdoc = db.doc_for_source(source.sid)
if sdoc:
if doc and sdoc != doc:
print >>sys.stderr, "A different document already exists for source '%s'." % (doc_sid)
print >>sys.stderr, "Aborting."
sys.exit(1)
print >>sys.stderr, "Source '%s' found in database. Updating existing document..." % (doc_sid)
doc = sdoc
try:
print >>sys.stderr, "Retrieving bibtex...",
bibtex = source.fetch_bibtex()
print >>sys.stderr, "done."
except SourceError as e:
print >>sys.stderr, "\n"
print >>sys.stderr, "Could not retrieve bibtex: %s" % e
sys.exit(1)
if infile is True:
try:
print >>sys.stderr, "Retrieving file...",
file_name, file_data = source.fetch_file()
print >>sys.stderr, "done."
except SourceError as e:
print >>sys.stderr, "\n"
print >>sys.stderr, "Could not retrieve file: %s" % e
sys.exit(1)
elif infile is True:
print >>sys.stderr, "Must specify source with retrieve file option."
sys.exit(1)
if infile and not file_data:
with open(infile, 'r') as f:
file_data = f.read()
file_name = os.path.basename(infile)
##################################
# if we still don't have a doc, create a new one
if not doc:
doc = Document(db)
##################################
# add stuff to the doc
if bibtex:
try:
print >>sys.stderr, "Adding bibtex...",
doc.add_bibtex(bibtex)
print >>sys.stderr, "done."
except BibtexError as e:
print >>sys.stderr, "\n"
print >>sys.stderr, e
print >>sys.stderr, "Bibtex must be a plain text file with a single bibtex entry."
sys.exit(1)
except:
print >>sys.stderr, "\n"
raise
# add source sid if it hasn't been added yet
if source and not doc.get_sids():
doc.add_sid(source.sid)
if infile:
try:
print >>sys.stderr, "Adding file...",
doc.add_file_data(file_name, file_data)
print >>sys.stderr, "done."
except ParseError as e:
print >>sys.stderr, "\n"
print >>sys.stderr, "Parse error: %s" % e
sys.exit(1)
except:
print >>sys.stderr, "\n"
raise
if tags:
try:
print >>sys.stderr, "Adding tags...",
doc.add_tags(tags)
print >>sys.stderr, "done."
except:
print >>sys.stderr, "\n"
raise
##################################
# sync the doc to db and disk
try:
print >>sys.stderr, "Syncing document...",
doc.sync()
print >>sys.stderr, "done.\n",
except:
print >>sys.stderr, "\n"
raise
print_doc_summary(doc)
return doc.docid
############################################
def importbib(db, bibfile, tags=[], overwrite=False):
errors = []
sources = Sources()
for entry in sorted(Bibtex(bibfile), key=lambda entry: entry.key):
print >>sys.stderr, entry.key
try:
docs = []
# check for doc with this bibkey
bdoc = db.doc_for_bib(entry.key)
if bdoc:
docs.append(bdoc)
# check for known sids
for source in sources.scan_bibentry(entry):
sdoc = db.doc_for_source(source.sid)
# FIXME: why can't we match docs in list?
if sdoc and sdoc.docid not in [doc.docid for doc in docs]:
docs.append(sdoc)
if len(docs) == 0:
doc = Document(db)
elif len(docs) > 0:
if len(docs) > 1:
print >>sys.stderr, " Multiple distinct docs found for entry. Using first found."
doc = docs[0]
print >>sys.stderr, " Updating id:%d..." % (doc.docid)
doc.add_bibentry(entry)
filepath = entry.get_file()
if filepath:
print >>sys.stderr, " Adding file: %s" % filepath
doc.add_file(filepath)
doc.add_tags(tags)
doc.sync()
except BibtexError as e:
print >>sys.stderr, " Error processing entry %s: %s" % (entry.key, e)
print >>sys.stderr
errors.append(entry.key)
if errors:
print >>sys.stderr
print >>sys.stderr, "Failed to import %d" % (len(errors)),
if len(errors) == 1:
print >>sys.stderr, "entry",
else:
print >>sys.stderr, "entries",
print >>sys.stderr, "from bibtex:"
for error in errors:
print >>sys.stderr, " %s" % (error)
sys.exit(1)
else:
sys.exit(0)
############################################
def search(db, query_string, oformat='summary', sort='relevance', limit=None):
if query_string == '*' and oformat in ['tags','sources','keys']:
if oformat == 'tags':
for tag in db.tag_iter():
print tag
elif oformat == 'sources':
for sid in db.sid_iter():
print sid
elif oformat == 'keys':
for key in db.term_iter('key'):
print key
return
otags = set([])
osources = set([])
okeys = set([])
for doc in db.search(query_string, sort=sort, limit=limit):
if oformat in ['summary']:
print_doc_summary(doc)
continue
elif oformat in ['file','files']:
for path in doc.get_fullpaths():
print "%s" % (path)
continue
elif oformat == 'bibtex':
bibtex = doc.get_bibtex()
if not bibtex:
print >>sys.stderr, "No bibtex for doc id:%d." % doc.docid
else:
print bibtex
print
continue
if oformat == 'tags':
otags = otags | set(doc.get_tags())
elif oformat == 'sources':
osources = osources | set(doc.get_sids())
elif oformat == 'keys':
key = doc.get_key()
if key:
print key
if oformat == 'tags':
for tag in otags:
print tag
elif oformat == 'sources':
for source in osources:
print source
############################################
def export(db, outdir, query_string):
try:
os.makedirs(outdir)
except:
pass
import pipes
for doc in db.search(query_string):
title = doc.get_title()
origpaths = doc.get_fullpaths()
nfiles = len(origpaths)
for path in origpaths:
if not title:
name = os.path.basename(os.path.splitext(path)[0])
else:
name = '%s' % (title.replace(' ','_'))
ind = 0
if nfiles > 1:
name += '.%s' % ind
ind += 1
name += '.pdf'
outpath = os.path.join(outdir,name)
print outpath
shutil.copyfile(path, outpath.encode('utf-8'))
|