Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
"""
[JS] Jump Start: Your commandline companion
Commands:
js add <name> <file>
js rm (<name> | --all)
js show (--all | -a)
js <name>
Options:
--version -v: Print program version and exit
--help -h : Print this help message and exit
--db : Print the db location
"""
import sqlite3
import os
import subprocess
import sys
from __future__ import print_function
COMMANDS = {
'create':'''
CREATE TABLE IF NOT EXISTS path(id INTEGER PRIMARY KEY, name TEXT,
path TEXT, filename TEXT)
''',
'insert':'''
INSERT INTO path(name, path, filename)
VALUES (?, ?, ?)
''',
'select':'''
SELECT path,filename FROM path WHERE name=?
''',
'delete','''
DELETE FROM path WHERE name=?
'''
}
DB = os.path.expanduser("~/.js")
class JS(object):
def __init__(self,options):
self.options = options
def connect(self):
db = sqlite3.connect(DB)
cursor = db.cursor()
with cursor:
cursor.execute(COMMANDS['create'])
return cursor
@classmethod
def get(self, name):
cursor = self.connect()
with cursor:
cursor.execute(COMMANDS['select'], (name))
path = cursor.fetchone()
if path is None:
print("Path not available. Have you inserted it before?")
sys.exit()
return path
@classmethod
def add(self,name,filename):
path = os.getcwd()
cursor = self.connect()
with cursor:
if cursor.execute(COMMANDS['select'],(name)) is not None:
print('The name you entered is already present in the db')
sys.exit()
cursor.execute(COMMANDS['insert'],(name, path, filename))
print('Command successfully inserted into the db')
@classmethod
def remove(self,name):
cursor = self.connect()
with cursor:
if cursor.execute(COMMANDS['select']).fetchone() is None:
print('That command is not present in the db')
sys.exit()
cursor.execute(COMMANDS['delete'],(name))
print('{0} is deleted from db'.format(name))
@classmethod
def delete_db(self):
os.remove(DB)
print('Db successfully deleted!')
@staticmethod
def execute(command):
print("Command is being executed...")
subprocess.call(command, shell=True)
def main():
args = sys.argv[1:]
if len(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment