add support for custom path locators
This commit is contained in:
parent
9e6e2540a9
commit
9ef6c03998
30
do_what.py
30
do_what.py
@ -14,6 +14,7 @@ list_directory="ls --color=auto"
|
|||||||
change_dir="cd"
|
change_dir="cd"
|
||||||
print_dir="pwd"
|
print_dir="pwd"
|
||||||
help_command="man"
|
help_command="man"
|
||||||
|
path_locator="which"
|
||||||
|
|
||||||
# general function for setting a shell's environment variable
|
# general function for setting a shell's environment variable
|
||||||
def set_runtime_var(name, value, options=""):
|
def set_runtime_var(name, value, options=""):
|
||||||
@ -34,8 +35,20 @@ def is_binary_file(filepathname):
|
|||||||
|
|
||||||
# check if a given program is in the path environment using which
|
# check if a given program is in the path environment using which
|
||||||
def env_has(program):
|
def env_has(program):
|
||||||
whichcall = subprocess.Popen(['which', program], stdout=subprocess.PIPE)
|
def is_exe(fpath):
|
||||||
return whichcall.returncode
|
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
|
||||||
|
|
||||||
|
fpath, fname = os.path.split(program)
|
||||||
|
if fpath:
|
||||||
|
if is_exe(program):
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
for path in os.environ["PATH"].split(os.pathsep):
|
||||||
|
exe_file = os.path.join(path, program)
|
||||||
|
if is_exe(exe_file):
|
||||||
|
return True
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
# get the number of lines in a file
|
# get the number of lines in a file
|
||||||
def file_len(fname):
|
def file_len(fname):
|
||||||
@ -62,6 +75,8 @@ if ( os.path.isfile(ENV_HOME+'/.config/do_what/what.config') ):
|
|||||||
list_directory = config['DEFAULT']['list_directory']
|
list_directory = config['DEFAULT']['list_directory']
|
||||||
if ( 'help_command' in config['DEFAULT'] ):
|
if ( 'help_command' in config['DEFAULT'] ):
|
||||||
help_command = config['DEFAULT']['help_command']
|
help_command = config['DEFAULT']['help_command']
|
||||||
|
if ( 'path_locator' in config['DEFAULT'] ):
|
||||||
|
path_locator = config['DEFAULT']['path_locator']
|
||||||
else:
|
else:
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
config['DEFAULT'] = {
|
config['DEFAULT'] = {
|
||||||
@ -70,6 +85,7 @@ else:
|
|||||||
'pretty_print_file': pretty_print_file,
|
'pretty_print_file': pretty_print_file,
|
||||||
'print_file': print_file,
|
'print_file': print_file,
|
||||||
'help_command': help_command,
|
'help_command': help_command,
|
||||||
|
'path_locator': path_locator,
|
||||||
}
|
}
|
||||||
print("echo Default config loaded.")
|
print("echo Default config loaded.")
|
||||||
with open(ENV_HOME+'/.config/do_what/what.config', 'w') as configfile:
|
with open(ENV_HOME+'/.config/do_what/what.config', 'w') as configfile:
|
||||||
@ -129,14 +145,12 @@ elif ( (not active and len(sys.argv) == 2) or (active and len(sys.argv) == 3) ):
|
|||||||
|
|
||||||
else:
|
else:
|
||||||
# check if file is a path program, then list using which/man on active
|
# check if file is a path program, then list using which/man on active
|
||||||
whichcall = subprocess.Popen(['which', filearg], stdout=subprocess.PIPE)
|
path_has = env_has(filearg)
|
||||||
result = whichcall.communicate()[0]
|
|
||||||
statuscode = whichcall.returncode
|
|
||||||
# print the location of the program
|
# print the location of the program
|
||||||
if ( int(statuscode) == 0 and not active ):
|
if ( path_has and not active ):
|
||||||
print("echo "+result.decode('utf-8'))
|
print(path_locator+" "+filearg)
|
||||||
# display the help page for the program (active)
|
# display the help page for the program (active)
|
||||||
elif ( int(statuscode) == 0 and active ):
|
elif ( path_has and active ):
|
||||||
print(help_command+" "+filearg)
|
print(help_command+" "+filearg)
|
||||||
else:
|
else:
|
||||||
print("echo Unknown file/directory.")
|
print("echo Unknown file/directory.")
|
||||||
|
Loading…
Reference in New Issue
Block a user