Rename APIs

New APIs: get_rec_all(), delete_rec_all()
Rename API open_in_browser() to browse()
Rename help APIs.
Update User Agent to Firefox 51.
Other minor changes
This commit is contained in:
Arun Prakash Jana 2017-02-19 08:18:38 +05:30
parent e5395b5528
commit 64799aff05
2 changed files with 27 additions and 26 deletions

37
buku.py
View File

@ -56,8 +56,8 @@ DESC_str = '%s \x1b[91m+\x1b[0m %s\n'
TAG_str = '%s \x1b[91m#\x1b[0m %s\n'
# Disguise as Firefox on Ubuntu
USER_AGENT = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:50.0) Gecko/20100101 \
Firefox/50.0'
USER_AGENT = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:51.0) Gecko/20100101 \
Firefox/51.0'
myheaders = None # Default dictionary of headers
myproxy = None # Default proxy
@ -451,10 +451,10 @@ class BukuDb:
return (conn, cur)
def get_all_rec(self):
def get_rec_all(self):
''' Get all the bookmarks in the database
:return: a list of tuples each of which is a bookmark along with its parameters
:return: a list of tuples as bookmark records
'''
self.cur.execute('SELECT * FROM bookmarks')
@ -1100,12 +1100,13 @@ class BukuDb:
return True
def delete_all_rec(self, delay_commit=False):
def delete_rec_all(self, delay_commit=False):
'''Removes all records in the Bookmarks table
:param delay_commit: do not commit to DB, caller responsibility
:return: True on success, False on failure
'''
try:
self.cur.execute('DELETE FROM bookmarks')
if not delay_commit:
@ -1182,7 +1183,7 @@ class BukuDb:
else:
print(format_json(resultset, field_filter=self.field_filter))
def get_all_tags(self):
def get_tag_all(self):
'''Get list of tags in DB
:return: tuple (list of unique tags sorted alphabetically,
@ -1272,7 +1273,7 @@ class BukuDb:
query = 'SELECT URL FROM bookmarks WHERE id = ? LIMIT 1'
try:
for row in self.cur.execute(query, (index,)):
open_in_browser(row[0])
browse(row[0])
return True
logerr('No matching index %d', index)
except IndexError:
@ -1574,7 +1575,7 @@ class ExtendedArgumentParser(argparse.ArgumentParser):
# Print program info
@staticmethod
def print_program_info(file=sys.stdout):
def program_info(file=sys.stdout):
if sys.platform == 'win32' and file == sys.stdout:
file = sys.stderr
@ -1592,7 +1593,7 @@ Webpage: https://github.com/jarun/Buku
# Print prompt help
@staticmethod
def print_prompt_help(file=sys.stdout):
def prompt_help(file=sys.stdout):
file.write('''
keys:
1-N browse search result indices and/or ranges
@ -1611,7 +1612,7 @@ keys:
# Help
def print_help(self, file=sys.stdout):
super(ExtendedArgumentParser, self).print_help(file)
self.print_program_info(file)
self.program_info(file)
# ----------------
@ -1849,7 +1850,7 @@ def taglist_subprompt(obj, msg, noninteractive=False):
:return: new command string
'''
unique_tags, dic = obj.get_all_tags()
unique_tags, dic = obj.get_tag_all()
new_results = True
while True:
@ -1994,7 +1995,7 @@ def prompt(obj, results, noninteractive=False, deep=False, subprompt=False):
# Show help with '?'
if nav == '?':
ExtendedArgumentParser.print_prompt_help(sys.stdout)
ExtendedArgumentParser.prompt_help(sys.stdout)
new_results = False
continue
@ -2008,7 +2009,7 @@ def prompt(obj, results, noninteractive=False, deep=False, subprompt=False):
# open all results and re-prompt with 'a'
if nav == 'a':
for index in range(0, count):
open_in_browser(results[index][1])
browse(results[index][1])
continue
# iterate over white-space separated indices
@ -2018,7 +2019,7 @@ def prompt(obj, results, noninteractive=False, deep=False, subprompt=False):
if index < 0 or index >= count:
print('No matching index %s' % nav)
continue
open_in_browser(results[index][1])
browse(results[index][1])
elif '-' in nav and is_int(nav.split('-')[0]) \
and is_int(nav.split('-')[1]):
lower = int(nav.split('-')[0])
@ -2027,7 +2028,7 @@ def prompt(obj, results, noninteractive=False, deep=False, subprompt=False):
lower, upper = upper, lower
for index in range(lower-1, upper):
if 0 <= index < count:
open_in_browser(results[index][1])
browse(results[index][1])
else:
print('No matching index %d' % (index + 1))
else:
@ -2124,7 +2125,7 @@ def is_int(string):
return False
def open_in_browser(url):
def browse(url):
'''Duplicate stdin, stdout (to suppress showing errors
on the terminal) and open URL in default browser
@ -2149,7 +2150,7 @@ def open_in_browser(url):
try:
webbrowser.open(url)
except Exception as e:
logerr('open_in_browser(): %s', e)
logerr('browse(): %s', e)
finally:
os.close(fd)
os.dup2(_stderr, 2)
@ -2724,7 +2725,7 @@ POSITIONAL ARGUMENTS:
# URLs are opened first and updated/deleted later.
if args.oa:
for row in search_results:
open_in_browser(row[1])
browse(row[1])
# In case of search and delete/update,
# prompt should be non-interactive

View File

@ -271,7 +271,7 @@ class TestBukuDb(unittest.TestCase):
# simulate user input, select range of indices 1-3
index_range = '1-%s' % len(self.bookmarks)
with mock.patch('builtins.input', side_effect=[index_range]):
with mock.patch('buku.open_in_browser') as mock_open_in_browser:
with mock.patch('buku.browse') as mock_browse:
try:
# search the db with keywords from each bookmark
# searching using the first tag from bookmarks
@ -282,11 +282,11 @@ class TestBukuDb(unittest.TestCase):
# catch exception thrown by reaching the end of the side effect iterable
pass
# collect arguments passed to open_in_browser
arg_list = [args[0] for args, _ in mock_open_in_browser.call_args_list]
# collect arguments passed to browse
arg_list = [args[0] for args, _ in mock_browse.call_args_list]
# expect a list of one-tuples that are bookmark URLs
expected = [x[0] for x in self.bookmarks]
# checking if open_in_browser called with expected arguments
# checking if browse called with expected arguments
self.assertEqual(arg_list, expected)
# @unittest.skip('skipping')
@ -297,7 +297,7 @@ class TestBukuDb(unittest.TestCase):
# simulate user input, select 'a' to open all bookmarks in results
with mock.patch('builtins.input', side_effect=['a']):
with mock.patch('buku.open_in_browser') as mock_open_in_browser:
with mock.patch('buku.browse') as mock_browse:
try:
# search the db with keywords from each bookmark
# searching using the first tag from bookmarks
@ -308,11 +308,11 @@ class TestBukuDb(unittest.TestCase):
# catch exception thrown by reaching the end of the side effect iterable
pass
# collect arguments passed to open_in_browser
arg_list = [args[0] for args, _ in mock_open_in_browser.call_args_list]
# collect arguments passed to browse
arg_list = [args[0] for args, _ in mock_browse.call_args_list]
# expect a list of one-tuples that are bookmark URLs
expected = [x[0] for x in self.bookmarks][:2]
# checking if open_in_browser called with expected arguments
# checking if browse called with expected arguments
self.assertEqual(arg_list, expected)
# @unittest.skip('skipping')