Remove length check for empty lists

Fix a bug in get_all_tags() where tuple was not returned.
Fix Travis error.
Convert some prints to tacit
This commit is contained in:
Arun Prakash Jana 2017-01-29 22:51:55 +05:30
parent bb8e683f93
commit ce3a7aa377
2 changed files with 76 additions and 72 deletions

141
buku.py
View File

@ -458,11 +458,11 @@ class BukuDb:
''' '''
self.cur.execute('SELECT * FROM bookmarks WHERE id = ?', (index,)) self.cur.execute('SELECT * FROM bookmarks WHERE id = ?', (index,))
results = self.cur.fetchall() resultset = self.cur.fetchall()
if len(results) == 0: if resultset:
return None return resultset[0]
else:
return results[0] return None
def get_rec_id(self, url): def get_rec_id(self, url):
'''Check if URL already exists in DB '''Check if URL already exists in DB
@ -473,10 +473,10 @@ class BukuDb:
self.cur.execute('SELECT id FROM bookmarks WHERE URL = ?', (url,)) self.cur.execute('SELECT id FROM bookmarks WHERE URL = ?', (url,))
resultset = self.cur.fetchall() resultset = self.cur.fetchall()
if len(resultset) == 0: if resultset:
return -1 return resultset[0][0]
return resultset[0][0] return -1
def add_rec(self, url, title_in=None, tags_in=None, desc=None, immutable=0, def add_rec(self, url, title_in=None, tags_in=None, desc=None, immutable=0,
delay_commit=False): delay_commit=False):
@ -815,7 +815,7 @@ class BukuDb:
while True: while True:
cond.acquire() cond.acquire()
if len(resultset) > 0: if resultset:
row = resultset.pop() row = resultset.pop()
else: else:
cond.release() cond.release()
@ -972,7 +972,7 @@ class BukuDb:
self.cur.execute('SELECT MAX(id) from bookmarks') self.cur.execute('SELECT MAX(id) from bookmarks')
results = self.cur.fetchall() results = self.cur.fetchall()
# Return if the last index left in DB was just deleted # Return if the last index left in DB was just deleted
if len(results) == 1 and results[0][0] is None: if results[0][0] is None:
return return
query1 = 'SELECT id, URL, metadata, tags, \ query1 = 'SELECT id, URL, metadata, tags, \
@ -991,7 +991,8 @@ class BukuDb:
(index, row[1], row[2], row[3], row[4],)) (index, row[1], row[2], row[3], row[4],))
if not delay_commit: if not delay_commit:
self.conn.commit() self.conn.commit()
print('Index %d moved to %d' % (row[0], index)) if self.chatty:
print('Index %d moved to %d' % (row[0], index))
def delete_rec(self, index, low=0, high=0, is_range=False, def delete_rec(self, index, low=0, high=0, is_range=False,
delay_commit=False): delay_commit=False):
@ -1104,7 +1105,7 @@ class BukuDb:
query = 'SELECT * FROM bookmarks WHERE id = ?' query = 'SELECT * FROM bookmarks WHERE id = ?'
self.cur.execute(query, (index,)) self.cur.execute(query, (index,))
results = self.cur.fetchall() results = self.cur.fetchall()
if len(results) == 0: if not results:
logerr('No matching index %d', index) logerr('No matching index %d', index)
return return
except IndexError: except IndexError:
@ -1146,8 +1147,8 @@ class BukuDb:
def get_all_tags(self): def get_all_tags(self):
'''Get list of tags in DB '''Get list of tags in DB
:return: list of unique tags sorted alphabetically :return: tuple (list of unique tags sorted alphabetically,
:return: a dictionary of {tag:usage_count} a dictionary of {tag:usage_count})
''' '''
tags = [] tags = []
@ -1163,8 +1164,8 @@ class BukuDb:
else: else:
dic[tag] += row[1] dic[tag] += row[1]
if len(tags) == 0: if not tags:
return tags return tags, dic
if tags[0] == '': if tags[0] == '':
unique_tags = sorted(tags[1:]) unique_tags = sorted(tags[1:])
@ -1198,15 +1199,14 @@ class BukuDb:
query = 'SELECT id, tags FROM bookmarks WHERE tags LIKE ?' query = 'SELECT id, tags FROM bookmarks WHERE tags LIKE ?'
self.cur.execute(query, ('%' + orig + '%',)) self.cur.execute(query, ('%' + orig + '%',))
results = self.cur.fetchall() results = self.cur.fetchall()
if results:
query = 'UPDATE bookmarks SET tags = ? WHERE id = ?'
for row in results:
tags = row[1].replace(orig, newtags)
tags = parse_tags([tags])
self.cur.execute(query, (tags, row[0],))
print('Index %d updated' % row[0])
query = 'UPDATE bookmarks SET tags = ? WHERE id = ?'
for row in results:
tags = row[1].replace(orig, newtags)
tags = parse_tags([tags])
self.cur.execute(query, (tags, row[0],))
print('Index %d updated' % row[0])
if len(results):
self.conn.commit() self.conn.commit()
return True return True
@ -1263,30 +1263,28 @@ class BukuDb:
if taglist is not None: if taglist is not None:
tagstr = parse_tags(taglist) tagstr = parse_tags(taglist)
if len(tagstr) == 0 or tagstr == DELIM: if not tagstr or tagstr == DELIM:
logerr('Invalid tag') logerr('Invalid tag')
return False return False
if len(tagstr) > 0: tags = tagstr.split(DELIM)
tags = tagstr.split(DELIM) query = '%s WHERE' % query
query = '%s WHERE' % query for tag in tags:
for tag in tags: if tag != '':
if tag != '': is_tag_valid = True
is_tag_valid = True query += " tags LIKE '%' || ? || '%' OR"
query += " tags LIKE '%' || ? || '%' OR" tag = '%s%s%s' % (DELIM, tag, DELIM)
tag = '%s%s%s' % (DELIM, tag, DELIM) arguments += (tag,)
arguments += (tag,)
if is_tag_valid: if is_tag_valid:
query = query[:-3] query = query[:-3]
else: else:
query = query[:-6] query = query[:-6]
logdbg('(%s), %s', query, arguments) logdbg('(%s), %s', query, arguments)
self.cur.execute(query, arguments) self.cur.execute(query, arguments)
resultset = self.cur.fetchall() resultset = self.cur.fetchall()
if not resultset:
if len(resultset) == 0:
print('No bookmarks exported') print('No bookmarks exported')
return False return False
@ -1424,10 +1422,10 @@ Buku bookmarks</H3>
return False return False
resultset = indb_cur.fetchall() resultset = indb_cur.fetchall()
for row in resultset: if resultset:
self.add_rec(row[1], row[2], row[3], row[4], row[5], True) for row in resultset:
self.add_rec(row[1], row[2], row[3], row[4], row[5], True)
if len(resultset):
self.conn.commit() self.conn.commit()
try: try:
@ -1455,11 +1453,11 @@ Buku bookmarks</H3>
self.cur.execute('SELECT url FROM bookmarks WHERE id = ?', self.cur.execute('SELECT url FROM bookmarks WHERE id = ?',
(index,)) (index,))
results = self.cur.fetchall() results = self.cur.fetchall()
if len(results): if not results:
url = results[0][0]
else:
return None return None
url = results[0][0]
proxies = { proxies = {
'https': os.environ.get('https_proxy'), 'https': os.environ.get('https_proxy'),
} }
@ -1749,7 +1747,7 @@ def network_handler(url):
return (page_title.strip().replace('\n', ''), 0, 0) return (page_title.strip().replace('\n', ''), 0, 0)
def parse_tags(keywords=None): def parse_tags(keywords=[]):
'''Format and get tag string from tokens '''Format and get tag string from tokens
:param keywords: list of tags :param keywords: list of tags
@ -1761,6 +1759,9 @@ def parse_tags(keywords=None):
if keywords is None: if keywords is None:
return None return None
if not keywords:
return DELIM
tags = DELIM tags = DELIM
orig_tags = [] orig_tags = []
unique_tags = [] unique_tags = []
@ -1816,7 +1817,7 @@ def taglist_subprompt(obj, msg, noninteractive=False):
while True: while True:
if new_results: if new_results:
if len(unique_tags) == 0: if not unique_tags:
count = 0 count = 0
print('0 tags') print('0 tags')
else: else:
@ -2197,7 +2198,7 @@ def main():
pass pass
# If piped input, set argument vector # If piped input, set argument vector
if len(pipeargs) > 0: if pipeargs:
sys.argv = pipeargs sys.argv = pipeargs
# Setup custom argument parser # Setup custom argument parser
@ -2365,7 +2366,7 @@ POSITIONAL ARGUMENTS:
addarg('--fixtags', action='store_true', help=HIDE) addarg('--fixtags', action='store_true', help=HIDE)
# Show help and exit if no arguments # Show help and exit if no arguments
if len(sys.argv) < 2: if len(sys.argv) == 1:
argparser.print_help(sys.stdout) argparser.print_help(sys.stdout)
sys.exit(1) sys.exit(1)
@ -2404,21 +2405,21 @@ POSITIONAL ARGUMENTS:
# Set up tags # Set up tags
if args.tag is not None: if args.tag is not None:
if len(args.tag): if args.tag:
tags_in = args.tag tags_in = args.tag
else: else:
tags_in = [DELIM, ] tags_in = [DELIM, ]
# Set up title # Set up title
if args.title is not None: if args.title is not None:
if len(args.title): if args.title:
title_in = ' '.join(args.title) title_in = ' '.join(args.title)
else: else:
title_in = '' title_in = ''
# Set up comment # Set up comment
if args.comment is not None: if args.comment is not None:
if len(args.comment) > 0: if args.comment:
desc_in = ' '.join(args.comment) desc_in = ' '.join(args.comment)
else: else:
desc_in = '' desc_in = ''
@ -2433,14 +2434,13 @@ POSITIONAL ARGUMENTS:
tags = DELIM tags = DELIM
keywords = args.add keywords = args.add
if tags_in is not None: if tags_in is not None:
if tags_in[0] == '+' and len(tags_in) == 1: if tags_in[0] == '+':
pass if len(tags_in) > 1:
elif tags_in[0] == '+': # The case: buku -a url tag1, tag2 --tag + tag3, tag4
# The case: buku -a url tag1, tag2 --tag + tag3, tag4 tags_in = tags_in[1:]
tags_in = tags_in[1:] # In case of add, args.add may have URL followed by tags
# In case of add, args.add may have URL followed by tags # Add delimiter as url+tags may not end with one
# Add delimiter as url+tags may not end with one keywords = args.add + [DELIM] + tags_in
keywords = args.add + [DELIM] + tags_in
else: else:
keywords = args.add + [DELIM] + tags_in keywords = args.add + [DELIM] + tags_in
@ -2482,10 +2482,10 @@ POSITIONAL ARGUMENTS:
# In case of search and delete/update, # In case of search and delete/update,
# prompt should be non-interactive # prompt should be non-interactive
# delete gets priority over update # delete gets priority over update
if args.delete is not None and len(args.delete) == 0: if args.delete is not None and not args.delete:
oneshot = True oneshot = True
to_delete = True to_delete = True
elif args.update is not None and len(args.update) == 0: elif args.update is not None and not args.update:
oneshot = True oneshot = True
update_search_results = True update_search_results = True
@ -2507,7 +2507,7 @@ POSITIONAL ARGUMENTS:
url_in = '' url_in = ''
# Parse tags into a comma-separated string # Parse tags into a comma-separated string
if tags_in and len(tags_in): if tags_in:
if tags_in[0] == '+': if tags_in[0] == '+':
tags = '+%s' % parse_tags(tags_in[1:]) tags = '+%s' % parse_tags(tags_in[1:])
elif tags_in[0] == '-': elif tags_in[0] == '-':
@ -2517,13 +2517,14 @@ POSITIONAL ARGUMENTS:
else: else:
tags = None tags = None
if len(args.update) == 0: if not args.update:
# Update all records only if search was not opted # Update all records only if search was not opted
if not search_opted: if not search_opted:
bdb.update_rec(0, url_in, title_in, tags, desc_in, bdb.update_rec(0, url_in, title_in, tags, desc_in,
args.immutable, args.threads) args.immutable, args.threads)
elif update_search_results and search_results is not None: elif update_search_results and search_results is not None:
print("Updated results:\n") if not args.tacit:
print("Updated results:\n")
pos = len(search_results) - 1 pos = len(search_results) - 1
while pos >= 0: while pos >= 0:
@ -2565,7 +2566,7 @@ POSITIONAL ARGUMENTS:
# Delete record # Delete record
if args.delete is not None: if args.delete is not None:
if len(args.delete) == 0: if not args.delete:
# Attempt delete-all only if search was not opted # Attempt delete-all only if search was not opted
if not search_opted: if not search_opted:
bdb.cleardb() bdb.cleardb()
@ -2598,7 +2599,7 @@ POSITIONAL ARGUMENTS:
# Print record # Print record
if args.print is not None: if args.print is not None:
if len(args.print) == 0: if not args.print:
bdb.print_rec(0) bdb.print_rec(0)
else: else:
for idx in args.print: for idx in args.print:
@ -2627,7 +2628,7 @@ POSITIONAL ARGUMENTS:
if args.export is not None: if args.export is not None:
if args.tag is None: if args.tag is None:
bdb.exportdb(args.export[0], args.markdown) bdb.exportdb(args.export[0], args.markdown)
elif len(args.tag) == 0: elif not args.tag:
logerr('Missing tag') logerr('Missing tag')
else: else:
bdb.exportdb(args.export[0], args.markdown, args.tag) bdb.exportdb(args.export[0], args.markdown, args.tag)
@ -2642,7 +2643,7 @@ POSITIONAL ARGUMENTS:
# Open URL in browser # Open URL in browser
if args.open is not None: if args.open is not None:
if len(args.open) == 0: if not args.open:
bdb.browse_by_index(0) bdb.browse_by_index(0)
else: else:
for idx in args.open: for idx in args.open:

View File

@ -11,9 +11,12 @@ class TestHelpers(unittest.TestCase):
# @unittest.skip('skipping') # @unittest.skip('skipping')
def test_parse_tags(self): def test_parse_tags(self):
# call without arguments # call with None
parsed = parse_tags() parsed = parse_tags(None)
self.assertIsNone(parsed) self.assertIsNone(parsed)
# call with empty list
parsed = parse_tags([])
self.assertEqual(parsed, ",")
# empty tags # empty tags
parsed = parse_tags([",,,,,"]) parsed = parse_tags([",,,,,"])
self.assertEqual(parsed, ",") self.assertEqual(parsed, ",")