Associates or launches an action to be performed by the os based on filename extension. For example, assoc.py -e test.c will launch emacs.
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 | #!/usr/local/bin/python
"""
Make this scriptfile executible
by issuing
chmod +x assoc.py
and include the path to assoc.py to the PATH environment string.
To use, type
assoc.py flag filename extra_args
where flag may be absent (default would then be -v) or takes on the
following suggested values in the table.
flag action
-----------------------
view
-v view
-e edit
-c compile
-r run or execute
-p print
-----------------------
Example:
assoc.py -e test.c
should fire-up emacs.
assoc.py -D
will dump the dictionary.
The optional extra_args allows you to add additional settings to the
command associated with the file extension. More complicated
actions may require Python functions instead of simple strings as dictionary
values.
"""
import os
# Up to two dots in extension are allowed.
# Modify the dictionary to your needs.
Dassoc = {
('.doc', '-e'): 'soffice %s ',
('.jpg', '-v'): 'display %s ',
('.tex', '-e'): 'kile %s ',
('.pdf', '-v'): 'acroread %s ',
('.c', '-e'): 'emacs %s ',
('.c', '-c'): 'gcc -Wall -c %s ',
('.c', '-v'): 'emacs %s ',
('.py', '-r'): 'python %s ',
('.py', '-e'): 'emacs %s ',
('.py', '-v'): 'emacs %s ',
('.ps.gz', '-v'): 'gv %s '
}
if __name__ == "__main__":
argv = os.sys.argv
argc = len(argv)
flag = ""
filename = ""
args = ""
isOk = True
if (argc > 1):
if argv[1][0] == '-':
flag = argv[1]
if argc > 2:
filename = argv[2]
for i in range(3, argc):
args += ' ' + argv[i]
else:
if flag == "-D": # Dump dictionary.
print "Dictionary contents:"
for key in Dassoc:
print key, Dassoc[key]
# No filename ?
isOk = False
else:
flag = "-v" # default viewer.
filename = argv[1]
for i in range(2, argc):
args += ' ' + argv[i]
else:
# no arguments ?
isOk = False
if isOk:
done = False
extension = ""
for i in range(2, 0, -1):
fparts = filename.rsplit(".", i)
if len(fparts) == 3:
extension = "." + fparts[1] + "." + fparts[2]
elif len(fparts) == 2:
extension = "." + fparts[1]
key = (extension, flag)
if Dassoc.has_key(key):
cmdstr = (Dassoc[key] % filename) + args
os.system(cmdstr)
done = True
break
if not done:
print "error in assoc.py, no ", key, "found."
|
Allows an ordinary user to just call one script (assoc.py) to view, edit, print, run a file based on its extension from the command line with the actual program which may be suggested by administrators.
Tags: sysadmin
Great. Thanks. I was just about to write my own when found this.
Added extension conversation to lowercase and trying to solve issues with filenames with spaces.