b06bf73ded
Exception handling is value is not unique Signed-off-by: Arun Prakash Jana <engineerarun@gmail.com>
115 lines
3.1 KiB
Python
Executable File
115 lines
3.1 KiB
Python
Executable File
#!/usr/bin/python3
|
|
#
|
|
# Bookmark management utility
|
|
#
|
|
# Copyright (C) 2015 Arun Prakash Jana <engineerarun@gmail.com>
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with markit. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
import sys
|
|
import sqlite3
|
|
from getopt import getopt, GetoptError
|
|
|
|
# Globals
|
|
addurl = False
|
|
deletedb = False
|
|
printdb = False
|
|
|
|
def usage():
|
|
print("Usage: markit [OPTIONS] KEYWORDS...")
|
|
print("Bookmark manager.\n")
|
|
print("Options")
|
|
print(" -a URL tag entry1, tag entry2, ... print all bookmarks")
|
|
print(" -p print all bookmarks")
|
|
print(" -u N update index number")
|
|
print(" any other input exits markit\n")
|
|
print("Version 0.1")
|
|
print("Copyright (C) 2015 Arun Prakash Jana <engineerarun@gmail.com>")
|
|
print("Webpage: https://github.com/jarun/markit")
|
|
sys.exit(1)
|
|
|
|
def initdb():
|
|
# Create a connection
|
|
conn = sqlite3.connect('bookmarks.db')
|
|
cur = conn.cursor()
|
|
|
|
# Create table if it doesn't exist
|
|
cur.execute('''CREATE TABLE if not exists bookmarks
|
|
(id integer PRIMARY KEY AUTOINCREMENT, URL text NOT NULL UNIQUE, tags text, metadata text)''')
|
|
conn.commit()
|
|
return (conn, cur)
|
|
|
|
def addentry(conn, cur, keywords):
|
|
tags = ','
|
|
url = keywords[0]
|
|
for tag in keywords[1:]:
|
|
if tags[-1] == ",":
|
|
tags += tag
|
|
else:
|
|
tags += " " + tag
|
|
|
|
if tags[-1] != ",":
|
|
tags += ","
|
|
|
|
try:
|
|
cur.execute('INSERT INTO bookmarks(URL, tags, metadata) VALUES (?, ?, ?)', (url, tags, ''))
|
|
conn.commit()
|
|
except sqlite3.IntegrityError:
|
|
print("URL already exists")
|
|
|
|
def cleardb(conn, cur):
|
|
cur.execute('DROP TABLE if exists bookmarks')
|
|
conn.commit()
|
|
|
|
def printtable(cur):
|
|
for row in cur.execute('SELECT * FROM bookmarks'):
|
|
print("%s. %s\t%s\t%s" % (row[0], row[1], row[2][1:-1], row[3]))
|
|
|
|
# Main starts here
|
|
# ----------------
|
|
optlist = None
|
|
keywords = None
|
|
|
|
if len(sys.argv) < 2:
|
|
usage()
|
|
|
|
try:
|
|
optlist, keywords = getopt(sys.argv[1:], "s:n:c:l:t:NCjdap")
|
|
for opt in optlist:
|
|
if opt[0] == "-a":
|
|
addurl = True
|
|
elif opt[0] == "-d":
|
|
deletedb = True
|
|
elif opt[0] == "-p":
|
|
printdb = True
|
|
except GetoptError as e:
|
|
print("markit:", e)
|
|
sys.exit(1)
|
|
|
|
conn, cur = initdb()
|
|
|
|
if addurl == True:
|
|
if len(keywords) < 1:
|
|
usage()
|
|
|
|
addentry(conn, cur, keywords)
|
|
|
|
if deletedb == True:
|
|
cleardb(conn, cur)
|
|
|
|
if printdb == True:
|
|
printtable(cur)
|
|
|
|
conn.close()
|