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 specific keywords (year and author by default).
14 # If found, it returns the path(s), separated by \n.
16 import os, sys, subprocess
19 sys.stderr.write("lyxpaperview: %s\n" % message)
22 sys.stderr.write("lyxpaperview error: %s\n" % message)
26 msg = "Usage: %s titletoken-1 [titletoken-2] ... [titletoken-n]\n" \
27 " Each title token must occur in the filename (at an arbitrary position).\n" \
28 " You might use quotes to enter multi-word tokens"
29 return msg % prog_name
31 # Copied from lyxpreview_tools.py
32 # PATH and PATHEXT environment variables
33 path = os.environ["PATH"].split(os.pathsep)
35 if "PATHEXT" in os.environ:
36 extlist += os.environ["PATHEXT"].split(os.pathsep)
39 def find_exe(candidates):
42 for command in candidates:
43 prog = command.split()[0]
44 for directory in path:
46 full_path = os.path.join(directory, prog + ext)
47 if os.access(full_path, os.X_OK):
48 # The thing is in the PATH already (or we wouldn't
49 # have found it). Return just the basename to avoid
50 # problems when the path to the executable contains
52 if full_path.lower().endswith('.py'):
53 return command.replace(prog, '"%s" "%s"'
54 % (sys.executable, full_path))
62 # use locate if possible (faster)
63 if find_exe(['locate']):
64 p1 = subprocess.Popen(['locate', '-i', args[0].lower()], stdout=subprocess.PIPE)
65 px = subprocess.Popen(['grep', '-Ei', r'\.pdf$|\.ps$'], stdin=p1.stdout, stdout=subprocess.PIPE)
70 px = subprocess.Popen(['grep', '-i', arg], stdin=px.stdout, stdout=subprocess.PIPE)
72 output = px.communicate()
73 return output[0].decode("utf8").strip('\n')
74 # FIXME add something for windows as well?
75 # Maybe dir /s /b %WINDIR%\*author* | findstr .*year.*\."ps pdf"
77 for root, dirs, files in os.walk(path):
79 lfname = fname.lower()
80 if lfname.endswith(('.pdf', '.ps')):
83 if lfname.find(arg.lower()) == -1:
87 return os.path.join(root, fname)
96 error(usage(progname))
98 result = find(args, path = os.environ["HOME"])
103 if __name__ == "__main__":