diff --git a/buku.py b/buku.py index 308448e..03fe82b 100755 --- a/buku.py +++ b/buku.py @@ -56,8 +56,7 @@ 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:51.0) ' - 'Gecko/20100101 Firefox/51.0') +USER_AGENT = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:54.0) Gecko/20100101 Firefox/54.0' myheaders = None # Default dictionary of headers myproxy = None # Default proxy @@ -144,8 +143,7 @@ class BukuCrypt: try: from cryptography.hazmat.backends import default_backend - from cryptography.hazmat.primitives.ciphers import (Cipher, modes, - algorithms) + from cryptography.hazmat.primitives.ciphers import (Cipher, modes, algorithms) from getpass import getpass from hashlib import sha256 import struct @@ -192,8 +190,7 @@ class BukuCrypt: # Generate random 256-bit salt and key salt = os.urandom(BukuCrypt.SALT_SIZE) - key = ('%s%s' % (password, - salt.decode('utf-8', 'replace'))).encode('utf-8') + key = ('%s%s' % (password, salt.decode('utf-8', 'replace'))).encode('utf-8') for _ in range(iterations): key = sha256(key).digest() @@ -241,8 +238,7 @@ class BukuCrypt: try: from cryptography.hazmat.backends import default_backend - from cryptography.hazmat.primitives.ciphers import (Cipher, modes, - algorithms) + from cryptography.hazmat.primitives.ciphers import (Cipher, modes, algorithms) from getpass import getpass from hashlib import sha256 import struct @@ -286,8 +282,7 @@ class BukuCrypt: # Read 256-bit salt and generate key salt = infp.read(32) - key = ('%s%s' % (password, - salt.decode('utf-8', 'replace'))).encode('utf-8') + key = ('%s%s' % (password, salt.decode('utf-8', 'replace'))).encode('utf-8') for _ in range(iterations): key = sha256(key).digest() @@ -307,8 +302,7 @@ class BukuCrypt: if len(chunk) == 0: break - outfp.write( - decryptor.update(chunk) + decryptor.finalize()) + outfp.write(decryptor.update(chunk) + decryptor.finalize()) outfp.truncate(size) @@ -393,8 +387,7 @@ def import_html(html_soup, add_parent_folder_as_tag, newtag): # get list of tags within that folder tag_list = tag.parent.parent.find_parent('dl') - if ((possible_folder) and - possible_folder.parent in list(tag_list.parents)): + if ((possible_folder) and possible_folder.parent in list(tag_list.parents)): # then it's the folder of this bookmark if tag.has_attr('tags'): tag['tags'] += (DELIM + possible_folder.text) @@ -417,8 +410,7 @@ def import_html(html_soup, add_parent_folder_as_tag, newtag): class BukuDb: '''Abstracts all database operations''' - def __init__(self, json=False, field_filter=0, chatty=False, dbfile=None, - colorize=True): + def __init__(self, json=False, field_filter=0, chatty=False, dbfile=None, colorize=True): '''Database initialization API :param json: print results in json format @@ -455,8 +447,7 @@ class BukuDb: else: return os.path.abspath('.') else: - data_home = os.path.join(os.environ.get('HOME'), - '.local', 'share') + data_home = os.path.join(os.environ.get('HOME'), '.local', 'share') return os.path.join(data_home, 'buku') @@ -498,8 +489,7 @@ class BukuDb: sys.exit(1) else: # not db_exists and not enc_exists - print('DB file is being created at %s.\nYou should encrypt it.' - % dbfile) + print('DB file is being created at %s.\nYou should encrypt it.' % dbfile) try: # Create a connection @@ -540,8 +530,7 @@ class BukuDb: :return: bookmark data as a tuple, or None, if index is not found ''' - self.cur.execute('SELECT * FROM bookmarks WHERE id = ? LIMIT 1', - (index,)) + self.cur.execute('SELECT * FROM bookmarks WHERE id = ? LIMIT 1', (index,)) resultset = self.cur.fetchall() return resultset[0] if resultset else None @@ -552,8 +541,7 @@ class BukuDb: :return: DB index if URL found, else -1 ''' - self.cur.execute('SELECT id FROM bookmarks WHERE URL = ? LIMIT 1', - (url,)) + self.cur.execute('SELECT id FROM bookmarks WHERE URL = ? LIMIT 1', (url,)) resultset = self.cur.fetchall() return resultset[0][0] if resultset else -1 @@ -567,8 +555,7 @@ class BukuDb: resultset = self.cur.fetchall() return -1 if resultset[0][0] is None else resultset[0][0] - def add_rec(self, url, title_in=None, tags_in=None, desc=None, immutable=0, - delay_commit=False): + def add_rec(self, url, title_in=None, tags_in=None, desc=None, immutable=0, delay_commit=False): '''Add a new bookmark :param url: URL to bookmark @@ -623,8 +610,7 @@ class BukuDb: if immutable == 1: flagset |= immutable - qry = ('INSERT INTO bookmarks(URL, metadata, tags, desc, flags) ' - 'VALUES (?, ?, ?, ?, ?)') + qry = 'INSERT INTO bookmarks(URL, metadata, tags, desc, flags) VALUES (?, ?, ?, ?, ?)' self.cur.execute(qry, (url, meta, tags_in, desc, flagset)) if not delay_commit: self.conn.commit() @@ -651,8 +637,7 @@ class BukuDb: self.cur.execute('SELECT id, tags FROM bookmarks ORDER BY id ASC') else: - self.cur.execute('SELECT id, tags FROM bookmarks WHERE id = ? ' - 'LIMIT 1', (index,)) + self.cur.execute('SELECT id, tags FROM bookmarks WHERE id = ? LIMIT 1', (index,)) resultset = self.cur.fetchall() if resultset: @@ -691,8 +676,7 @@ class BukuDb: match = "'%' || ? || '%'" for tag in tags_to_delete: tag = delim_wrap(tag) - q = ("UPDATE bookmarks SET tags = replace(tags, '%s', '%s') " - 'WHERE tags LIKE %s' % (tag, DELIM, match)) + q = ("UPDATE bookmarks SET tags = replace(tags, '%s', '%s') WHERE tags LIKE %s" % (tag, DELIM, match)) self.cur.execute(q, (tag,)) count += self.cur.rowcount @@ -727,8 +711,7 @@ class BukuDb: return True - def update_rec(self, index, url=None, title_in=None, tags_in=None, - desc=None, immutable=-1, threads=4): + def update_rec(self, index, url=None, title_in=None, tags_in=None, desc=None, immutable=-1, threads=4): '''Update an existing record at index Update all records if index is 0 and url is not specified. URL is an exception because URLs are unique in DB. @@ -886,11 +869,9 @@ class BukuDb: ''' if index == 0: - self.cur.execute('SELECT id, url, flags FROM bookmarks ' - 'ORDER BY id ASC') + self.cur.execute('SELECT id, url, flags FROM bookmarks ORDER BY id ASC') else: - self.cur.execute('SELECT id, url, flags FROM bookmarks WHERE ' - 'id = ? LIMIT 1', (index,)) + self.cur.execute('SELECT id, url, flags FROM bookmarks WHERE id = ? LIMIT 1', (index,)) resultset = self.cur.fetchall() recs = len(resultset) @@ -1106,8 +1087,7 @@ class BukuDb: ''' tag = delim_wrap(tag.strip(DELIM)) - query = ('SELECT id, url, metadata, tags, desc FROM bookmarks ' - "WHERE tags LIKE '%' || ? || '%' ORDER BY id ASC") + query = "SELECT id, url, metadata, tags, desc FROM bookmarks WHERE tags LIKE '%' || ? || '%' ORDER BY id ASC" logdbg('query: "%s", args: %s', query, tag) self.cur.execute(query, (tag,)) @@ -1126,26 +1106,22 @@ class BukuDb: if max_id == -1: return - query1 = ('SELECT id, URL, metadata, tags, desc FROM bookmarks ' - 'WHERE id = ? LIMIT 1') + query1 = 'SELECT id, URL, metadata, tags, desc FROM bookmarks WHERE id = ? LIMIT 1' query2 = 'DELETE FROM bookmarks WHERE id = ?' - query3 = ('INSERT INTO bookmarks(id, URL, metadata, tags, desc) ' - 'VALUES (?, ?, ?, ?, ?)') + query3 = 'INSERT INTO bookmarks(id, URL, metadata, tags, desc) VALUES (?, ?, ?, ?, ?)' if max_id > index: self.cur.execute(query1, (max_id,)) results = self.cur.fetchall() for row in results: self.cur.execute(query2, (row[0],)) - self.cur.execute(query3, - (index, row[1], row[2], row[3], row[4],)) + self.cur.execute(query3, (index, row[1], row[2], row[3], row[4],)) if not delay_commit: self.conn.commit() if self.chatty: print('Index %d moved to %d' % (row[0], index)) - def delete_rec(self, index, low=0, high=0, is_range=False, - delay_commit=False): + def delete_rec(self, index, low=0, high=0, is_range=False, delay_commit=False): '''Delete a single record or remove the table if index is None :param index: DB index of deleted entry @@ -1172,8 +1148,7 @@ class BukuDb: try: query = 'DELETE from bookmarks where id BETWEEN ? AND ?' self.cur.execute(query, (low, high)) - print('Index %d-%d: %d deleted' - % (low, high, self.cur.rowcount)) + print('Index %d-%d: %d deleted' % (low, high, self.cur.rowcount)) if not self.cur.rowcount: return False @@ -1331,8 +1306,7 @@ class BukuDb: elif self.field_filter == 3: print('%s\t%s' % (row[0], row[2])) elif self.field_filter == 4: - print('%s\t%s\t%s\t%s' % ( - row[0], row[1], row[2], row[3][1:-1])) + print('%s\t%s\t%s\t%s' % (row[0], row[1], row[2], row[3][1:-1])) else: print(format_json(results, True, self.field_filter)) @@ -1360,8 +1334,7 @@ class BukuDb: print('%s\t%s' % (row[0], row[2])) elif self.field_filter == 4: for row in resultset: - print('%s\t%s\t%s\t%s' % ( - row[0], row[1], row[2], row[3][1:-1])) + print('%s\t%s\t%s\t%s' % (row[0], row[1], row[2], row[3][1:-1])) else: print(format_json(resultset, field_filter=self.field_filter)) @@ -1711,20 +1684,16 @@ class BukuDb: count += 1 else: outfp.write('\n\n' - '\n' + '\n' '
\n' - '
\n' % (timestamp, timestamp)) for row in resultset: - out = ('