Last active
August 29, 2015 14:02
-
-
Save yasoob/ee0f8e905092744bfbea 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
""" | |
[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