Created
July 4, 2009 14:38
-
-
Save skriticos/140597 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python3 | |
""" Terminal command shortcut scirpt. | |
This script reads a command data file (~/.scdata) and executes commands from it. | |
There are two command execution models: | |
1. by argement, if you invoke it with a number, it executes this command | |
and exits. If you invoke it with -e opion, it opens the command file | |
for editing and then enters the main loop. | |
2. by loop, if you invoke it withouth an argument, it prints the | |
availible commands and promts for command number (id) to execute. | |
There are also 2 special non-numeric loop commands: | |
e: edit command file and return to main loop | |
q: quit program | |
""" | |
import os, sys, time | |
def get_editor(): | |
try: | |
editor = os.environ['EDITOR'] | |
except: | |
editor = 'vim' | |
return editor | |
def read_data(editor, data_file): | |
try: | |
with open(data_file, 'r') as f: | |
data = f.readlines() | |
data = [item.strip() for item in data] | |
data = dict(enumerate(data)) | |
return data | |
except: | |
print('Data file not found, first start?.. Creating data file') | |
print('Please add some commands to it..') | |
time.sleep(3) | |
with open(data_file, 'w') as f: | |
pass | |
os.system(editor + ' ' + data_file) | |
return read_data() | |
if __name__ == '__main__': | |
editor = get_editor() | |
data_file = os.path.join(os.path.expanduser('~'), '.scdata') | |
data = read_data(editor, data_file) | |
if len(sys.argv) > 1: | |
if sys.argv[1] == '-e': | |
print('Opening data file for editing..') | |
time.sleep(1) | |
os.system(editor + ' ' + data_file) | |
try: | |
cmd_task = int(sys.argv[1]) | |
os.system('clear') | |
print('executing ', data[cmd_task], '\n') | |
os.system(data[cmd_task]) | |
print('\nexecution done, exiting..') | |
sys.exit() | |
except (IndexError, ValueError): | |
pass | |
while True: | |
os.system('clear') | |
print('Shell command shortcut utility\n' + | |
'==============================\n\n' + | |
'Currently availible commands are:\n') | |
for cmd_task, cmd in data.items(): | |
print(cmd_task, '\t', cmd) | |
print('\nPlease enter a command (e, q, or number)\n') | |
try: | |
cmd_task = input('% ') | |
if cmd_task == 'e': | |
os.system(editor + ' ' + data_file) | |
data = read_data(editor, data_file) | |
continue | |
elif cmd_task == 'q': | |
print('Quit.. bye!') | |
sys.exit() | |
else: | |
cmd_task = int(cmd_task) | |
assert(cmd_task <= len(data)) | |
except (AssertionError, ValueError, KeyError): | |
print('Please enter a valid input ' + | |
'(e, q or number)') | |
time.sleep(1) | |
else: | |
os.system('clear') | |
print('executing \'{1}\' ..\n'.format(data[cmd_task])) | |
time.sleep(1) | |
os.system(data[cmd_task]) | |
print('\nexecution done, hit a key to return..') | |
input() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment