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

View File

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