2 # -*- coding: utf-8 -*-
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
8 # author Jürgen Spitzmüller
9 # This draws on a bash script and conceptual idea by Pavel Sanda
10 # Full author contact details are available in file CREDITS
12 # This script searches the home directory for a PDF or PS
13 # file with a name containing year and author. If found,
14 # it opens the file in a viewer.
16 import getopt, os, sys, subprocess
18 pdf_viewers = ('pdfview', 'kpdf', 'okular', 'qpdfview --unique',
19 'evince', 'xreader', 'kghostview', 'xpdf', 'SumatraPDF',
20 'acrobat', 'acroread', 'mupdf',
21 'gv', 'ghostview', 'AcroRd32', 'gsview64', 'gsview32')
23 ps_viewers = ("kghostview", "okular", "qpdfview --unique",
24 "evince", "xreader", "gv", "ghostview -swap",
25 "gsview64", "gsview32")
28 sys.stderr.write("lyxpaperview: %s\n" % message)
31 sys.stderr.write("lyxpaperview error: %s\n" % message)
35 msg = "Usage: %s [-v pdfviewer] [-w psviewer] titletoken-1 [titletoken-2] ... [titletoken-n]\n" \
36 " Each title token must occur in the filename (at an arbitrary position).\n" \
37 " You might use quotes to enter multi-word tokens"
38 return msg % prog_name
40 # Copied from lyxpreview_tools.py
41 # PATH and PATHEXT environment variables
42 path = os.environ["PATH"].split(os.pathsep)
44 if "PATHEXT" in os.environ:
45 extlist += os.environ["PATHEXT"].split(os.pathsep)
48 def find_exe(candidates):
51 for command in candidates:
52 prog = command.split()[0]
53 for directory in path:
55 full_path = os.path.join(directory, prog + ext)
56 if os.access(full_path, os.X_OK):
57 # The thing is in the PATH already (or we wouldn't
58 # have found it). Return just the basename to avoid
59 # problems when the path to the executable contains
61 if full_path.lower().endswith('.py'):
62 return command.replace(prog, '"%s" "%s"'
63 % (sys.executable, full_path))
69 def find_exe_or_terminate(candidates):
70 exe = find_exe(candidates)
72 error("Unable to find executable from '%s'" % " ".join(candidates))
78 # use locate if possible (faster)
79 if find_exe(['locate']):
80 p1 = subprocess.Popen(['locate', '-i', args[0].lower()], stdout=subprocess.PIPE)
81 px = subprocess.Popen(['grep', '-Ei', '\.pdf$|\.ps$'], stdin=p1.stdout, stdout=subprocess.PIPE)
86 px = subprocess.Popen(['grep', '-i', arg], stdin=px.stdout, stdout=subprocess.PIPE)
87 p4 = subprocess.Popen(['head', '-n 1'], stdin=px.stdout, stdout=subprocess.PIPE)
89 output = p4.communicate()
90 return output[0].decode("utf8")[:-1]# strip trailing '\n'
91 # FIXME add something for windows as well?
92 # Maybe dir /s /b %WINDIR%\*author* | findstr .*year.*\."ps pdf"
94 for root, dirs, files in os.walk(path):
96 lfname = fname.lower()
97 if lfname.endswith(('.pdf', '.ps')):
100 if lfname.find(arg.lower()) == -1:
104 return os.path.join(root, fname)
110 opts, args = getopt.getopt(sys.argv[1:], "v:w:")
120 error(usage(progname))
122 result = find(args, path = os.environ["HOME"])
124 message("no document found!")
127 message("found document %s" % result)
130 if result.lower().endswith('.ps'):
132 viewer = find_exe_or_terminate(ps_viewers)
137 viewer = find_exe_or_terminate(pdf_viewers)
141 subprocess.call([viewer, result])
145 if __name__ == "__main__":