Support print function.

This commit is contained in:
Arun Prakash Jana 2016-04-25 03:35:46 +05:30
parent 58b8805b70
commit 0fb89072d3
No known key found for this signature in database
GPG Key ID: C0A712ED95043DCB

45
buku
View File

@ -51,7 +51,6 @@ except ImportError:
# Globals # Globals
addindex = None # DB index to insert URL into addindex = None # DB index to insert URL into
showindex = None # Index of bookmark to show
showOpt = 0 # Modify show. 1: show only URL, 2: show URL and tag showOpt = 0 # Modify show. 1: show only URL, 2: show URL and tag
entry = None # DB index to update or delete entry = None # DB index to update or delete
update = False # Update a bookmark in DB update = False # Update a bookmark in DB
@ -408,7 +407,7 @@ def AddUpdateEntry(conn, cur, keywords, index):
cur.execute('INSERT INTO bookmarks(id, URL, metadata, tags) VALUES (?, ?, ?, ?)', (int(addindex), url, meta, tags,)) cur.execute('INSERT INTO bookmarks(id, URL, metadata, tags) VALUES (?, ?, ?, ?)', (int(addindex), url, meta, tags,))
conn.commit() conn.commit()
print("Added at index %d\n" % cur.lastrowid) print("Added at index %d\n" % cur.lastrowid)
printdb(cur, str(cur.lastrowid)) printdb(cur, cur.lastrowid)
except sqlite3.IntegrityError: except sqlite3.IntegrityError:
for row in cur.execute("SELECT id from bookmarks where URL LIKE ?", (url,)): for row in cur.execute("SELECT id from bookmarks where URL LIKE ?", (url,)):
print("URL already exists at index %s" % row[0]) print("URL already exists at index %s" % row[0])
@ -596,7 +595,7 @@ def cleardb(conn, cur, index):
def printdb(cur, index, empty=False): def printdb(cur, index, empty=False):
"""Print bookmark details at index or all bookmarks if index is None """Print bookmark details at index or all bookmarks if index is None
Print only bookmarks with blank title or tags if empty is True Print only bookmarks with blank title or tag if empty is True
Note: URL is printed on top because title may be blank Note: URL is printed on top because title may be blank
Params: cursor, index to print, flag to show only bookmarks with no title or tags Params: cursor, index to print, flag to show only bookmarks with no title or tags
@ -606,7 +605,7 @@ def printdb(cur, index, empty=False):
global jsonOutput global jsonOutput
resultset = None resultset = None
if int(index) == 0: # Show all entries if index == 0: # Show all entries
if empty == False: if empty == False:
cur.execute('SELECT * FROM bookmarks') cur.execute('SELECT * FROM bookmarks')
resultset = cur.fetchall() resultset = cur.fetchall()
@ -630,7 +629,7 @@ def printdb(cur, index, empty=False):
print(formatJson(resultset)) print(formatJson(resultset))
else: # Show record at index else: # Show record at index
try: try:
resultset = cur.execute("SELECT * FROM bookmarks WHERE id = ?", (int(index),)) resultset = cur.execute("SELECT * FROM bookmarks WHERE id = ?", (index,))
except IndexError: except IndexError:
print("Index out of bound") print("Index out of bound")
return return
@ -894,7 +893,7 @@ def decrypt_file():
password = '' password = ''
password = getpass.getpass() password = getpass.getpass()
if password == '': if password == '':
print("Decryption failed"); printmsg("Decryption failed", "ERROR");
sys.exit(1) sys.exit(1)
with open(encpath, 'rb') as infile: with open(encpath, 'rb') as infile:
@ -926,7 +925,7 @@ def decrypt_file():
dbhash = get_filehash(dbpath) dbhash = get_filehash(dbpath)
if dbhash != enchash: if dbhash != enchash:
os.remove(dbpath) os.remove(dbpath)
print("Decryption failed"); printmsg("Decryption failed", "ERROR");
sys.exit(1) sys.exit(1)
else: else:
os.remove(encpath) os.remove(encpath)
@ -1044,7 +1043,7 @@ addarg('-e', '--empty', dest='empty', action='store_true',
help='show bookmarks with empty titles or no tags') help='show bookmarks with empty titles or no tags')
addarg('-i', '--insert', dest='insert', nargs='+', metavar=('N', 'URL tags'), addarg('-i', '--insert', dest='insert', nargs='+', metavar=('N', 'URL tags'),
help='insert new bookmark with URL and tags at free DB index N') help='insert new bookmark with URL and tags at free DB index N')
addarg('-j', '--json', dest='jsonOutput', action='store_true', addarg('-j', '--json', dest='jsonOutput', action='store_true', # DONE
help='show results in Json format') help='show results in Json format')
addarg('-k', '--decrypt', dest='decrypt', action='store_true', # DONE addarg('-k', '--decrypt', dest='decrypt', action='store_true', # DONE
help='decrypt (unlock) database file') help='decrypt (unlock) database file')
@ -1052,13 +1051,13 @@ addarg('-l', '--encrypt', dest='encrypt', action='store_true',
help='encrypt (lock) database file') help='encrypt (lock) database file')
addarg('-o', '--open', dest='openurl', type=int, metavar='N', # DONE addarg('-o', '--open', dest='openurl', type=int, metavar='N', # DONE
help='open URL at DB index N in browser') help='open URL at DB index N in browser')
addarg('-p', '--print', dest='showindex', type=int, metavar='N', addarg('-p', '--print', nargs='?', dest='printindex', type=int, const=0, metavar='N', # DONE
help='show details of bookmark record at DB index N, N=0 shows all') help='show details of bookmark record at DB index N, N=0 shows all')
addarg('-r', '--replace', nargs=2, dest='replace', metavar=('oldtag', 'newtag'), # DONE addarg('-r', '--replace', nargs=2, dest='replace', metavar=('oldtag', 'newtag'), # DONE
help='replace oldtag with newtag, delete oldtag if newtag=none') help='replace oldtag with newtag, delete oldtag if newtag=none')
addarg('-t', '--iterate', dest='iterations', type=int, metavar='N', # DONE addarg('-t', '--iterate', dest='iterations', type=int, metavar='N', # DONE
help='use N (> 0) hash iterations to generate key, for -k, -l') help='use N (> 0) hash iterations to generate key, for -k, -l')
addarg('-x', '--format', dest='showOpt', metavar='N', addarg('-x', '--format', dest='showOpt', type=int, choices=[1, 2], metavar='N', # DONE
help='modify -p behaviour, N=1: show only URL, N=2: show URL and tag') help='modify -p behaviour, N=1: show only URL, N=2: show URL and tag')
addarg('-z', '--debug', dest='debug', action='store_true', # DONE addarg('-z', '--debug', dest='debug', action='store_true', # DONE
help='show debug information') help='show debug information')
@ -1188,6 +1187,9 @@ except GetoptError as e:
args = argparser.parse_args() args = argparser.parse_args()
# Assign the values to globals
if args.showOpt is not None:
showOpt = args.showOpt
titleManual = args.titleManual titleManual = args.titleManual
if args.iterations is not None: if args.iterations is not None:
iterations = args.iterations iterations = args.iterations
@ -1207,12 +1209,18 @@ if args.encrypt == True:
if no_crypto: if no_crypto:
printmsg("PyCrypto missing", "ERROR") printmsg("PyCrypto missing", "ERROR")
sys.exit(1) sys.exit(1)
if iterations < 1:
printmsg("Iterations must be >= 1", "ERROR")
sys.exit(1)
encrypt_file() encrypt_file()
if args.decrypt == True: if args.decrypt == True:
if no_crypto: if no_crypto:
printmsg("PyCrypto missing", "ERROR") printmsg("PyCrypto missing", "ERROR")
sys.exit(1) sys.exit(1)
if iterations < 1:
printmsg("Decryption failed", "ERROR");
sys.exit(1)
decrypt_file() decrypt_file()
# Initialize the database and get handles # Initialize the database and get handles
@ -1224,6 +1232,10 @@ if args.addurl is not None:
# Remove a single record or all records # Remove a single record or all records
if args.delete is not None: if args.delete is not None:
if args.delete < 0:
printmsg("Index must be >= 0", "ERROR")
conn.close()
sys.exit(1)
cleardb(conn, cur, args.delete) cleardb(conn, cur, args.delete)
# Show all unique tags # Show all unique tags
@ -1252,14 +1264,23 @@ if update == True:
AddUpdateEntry(conn, cur, keywords, entry) AddUpdateEntry(conn, cur, keywords, entry)
# Print all records # Print all records
if showindex is not None: if args.printindex is not None:
printdb(cur, showindex) if args.printindex < 0:
printmsg("Index must be >= 0", "ERROR")
conn.close()
sys.exit(1)
printdb(cur, args.printindex)
# Print records with blank title or tag
if args.empty == True: if args.empty == True:
printdb(cur, 0, True) printdb(cur, 0, True)
# Open URL in browser # Open URL in browser
if args.openurl is not None: if args.openurl is not None:
if args.openurl < 1:
printmsg("Index must be >= 1", "ERROR")
conn.close()
sys.exit(1)
fetchopen(args.openurl) fetchopen(args.openurl)
# Close the connection before exiting # Close the connection before exiting