Merge remote-tracking branch 'origin/json-output' into json-output

This commit is contained in:
lmessier 2016-03-23 13:06:52 +01:00
commit d60d9c4249

128
buku
View File

@ -276,6 +276,8 @@ def AddUpdateEntry(conn, cur, keywords, index):
# Search the database for a tag or matching URL or Title info
def searchdb(cur, keywords):
global jsonOutput
searchtag = ''
for token in keywords:
searchtag += token + " "
@ -310,35 +312,43 @@ def searchdb(cur, keywords):
count = 0
results = []
for row in cur.execute(query, arguments):
results.append(row[1])
count += 1
print("\x1B[1m\x1B[93m%d. \x1B[0m\x1B[92m%s\x1B[0m (%d)\n\t%s\n\t\x1B[91m[TAGS]\x1B[0m %s" % (count, row[1], row[0], row[2], row[3][1:-1]))
resultset = cur.execute(query, arguments)
if count == 0:
return
if jsonOutput == True:
results = cur.fetchall();
if len(results) > 0:
print(formatJson(results))
print("")
else:
for row in resultset:
results.append(row[1])
count += 1
print("\x1B[1m\x1B[93m%d. \x1B[0m\x1B[92m%s\x1B[0m (%d)\n\t%s\n\t\x1B[91m[TAGS]\x1B[0m %s" % (count, row[1], row[0], row[2], row[3][1:-1]))
while True:
try:
nav = input("Result number to open: ")
except EOFError:
if count == 0:
return
if is_int(nav):
index = int(nav) - 1
if index < 0:
print("Index out of bound")
continue
print("")
while True:
try:
openurl = unquote(results[int(nav) - 1])
browser_open(openurl)
except IndexError:
print("Index out of bound")
else:
break
nav = input("Result number to open: ")
except EOFError:
return
if is_int(nav):
index = int(nav) - 1
if index < 0:
print("Index out of bound")
continue
try:
openurl = unquote(results[int(nav) - 1])
browser_open(openurl)
except IndexError:
print("Index out of bound")
else:
break
@ -366,47 +376,59 @@ def printdb(cur, index):
global jsonOutput
if index == None: # Show all entries
resultSet = cur.execute('SELECT * FROM bookmarks')
for row in resultSet:
if jsonOutput == True:
data = formatJson(resultSet)
else:
data = formatRaw(resultSet)
resultset = cur.execute('SELECT * FROM bookmarks')
if jsonOutput == False:
for row in resultset:
if showOpt == 1:
print("%s %s" % (row[0], row[1]))
elif showOpt == 2:
print("%s %s %s" % (row[0], row[1], row[3][1:-1]))
else:
print("\x1B[1m\x1B[93m%s. \x1B[0m\x1B[92m%s\x1B[0m\n\t%s\n\t\x1B[91m[TAGS]\x1B[0m %s" % (row[0], row[1], row[2], row[3][1:-1]))
else:
print(formatJson(resultset))
else: # Show record at index
try:
for row in cur.execute("SELECT * FROM bookmarks WHERE id = ?", (int(index),)):
print("\x1B[1m\x1B[93m%s. \x1B[0m\x1B[92m%s\x1B[0m\n\t%s\n\t\x1B[91m[TAGS]\x1B[0m %s" % (row[0], row[1], row[2], row[3][1:-1]))
return
print("No matching index")
resultset = cur.execute("SELECT * FROM bookmarks WHERE id = ?", (int(index),))
except IndexError:
print("Index out of bound")
print(data)
def formatRaw(resultSet):
marks = []
for row in resultSet:
if showOpt == 1:
marks.append(str(row[0]) + " " + row[1]);
elif showOpt == 2:
marks.append(str(row[0]) + " " + row[1] + " " + row[3][1:-1]);
if jsonOutput == False:
for row in resultset:
print("\x1B[1m\x1B[93m%s. \x1B[0m\x1B[92m%s\x1B[0m\n\t%s\n\t\x1B[91m[TAGS]\x1B[0m %s" % (row[0], row[1], row[2], row[3][1:-1]))
return
print("No matching index")
else:
marks.append(str(row[0]) + "." + row[1] + "\n\t" + row[2] + "\n\t" + row[3][1:-1]);
print(formatJson(resultset, True))
return "\n".join(marks)
def formatJson(resultset, single=False):
global showOpt
def formatJson(resultSet):
marks = []
for row in resultSet:
if showOpt == 1:
record = { 'url': row[1] }
elif showOpt == 2:
record = { 'url': row[1], 'tags': row[3][1:-1] }
else:
record = { 'url': row[1], 'title': row[2], 'tags': row[3][1:-1]}
if single == False:
marks = []
for row in resultset:
if showOpt == 1:
record = { 'url': row[1] }
elif showOpt == 2:
record = { 'url': row[1], 'tags': row[3][1:-1] }
else:
record = { 'url': row[1], 'title': row[2], 'tags': row[3][1:-1]}
marks.append(record)
else:
marks = {}
for row in resultset:
if showOpt == 1:
marks['url'] = row[1]
elif showOpt == 2:
marks['title'] = row[2]
marks['tags'] = row[3][1:-1]
else:
marks['url'] = row[1]
marks['title'] = row[2]
marks['tags'] = row[3][1:-1]
marks.append(record)
return json.dumps(marks, sort_keys=True, indent=4)