From cb2db624963631e4516abc78a0328f9806ee5616 Mon Sep 17 00:00:00 2001 From: Arun Prakash Jana Date: Sat, 29 Oct 2016 16:06:29 +0530 Subject: [PATCH] Coding style changes. API rename. Use 'macro' DELIM. --- buku | 127 ++++++++++++++++++++++++++++------------------------------- 1 file changed, 60 insertions(+), 67 deletions(-) diff --git a/buku b/buku index 1c21af9..77e639c 100755 --- a/buku +++ b/buku @@ -46,7 +46,7 @@ description = None # Description of the bookmark tagsearch = False # Search bookmarks by tag title_data = None # Title fetched from a webpage interrupted = False # Received SIGINT -DELIMITER = ',' # Delimiter used to store tags in DB +DELIM = ',' # Delimiter used to store tags in DB _VERSION_ = '2.5' # Program version # Crypto globals @@ -199,8 +199,7 @@ class BukuCrypt: elif len(chunk) % 16 != 0: chunk = '%s%s' % (chunk, ' ' * (16 - len(chunk) % 16)) - outfp.write(encryptor.update(chunk) + - encryptor.finalize()) + outfp.write(encryptor.update(chunk) + encryptor.finalize()) os.remove(dbpath) print('File encrypted') @@ -246,8 +245,7 @@ class BukuCrypt: sys.exit(1) with open(encpath, 'rb') as infp: - origsize = struct.unpack('= 0: - newtags = DELIMITER + newtags = DELIM tags = row[1].replace(orig, newtags) tags = parse_tags([tags]) @@ -1080,9 +1075,8 @@ class BukuDb: return False - def export_bookmark(self, filepath, markdown=False, taglist=None): - '''Export bookmarks to a Firefox - bookmarks formatted html file. + def exportdb(self, filepath, markdown=False, taglist=None): + '''Export bookmarks to a Firefox bookmarks formatted html file. :param filepath: path to file to export to :param markdown: use markdown syntax @@ -1101,18 +1095,18 @@ class BukuDb: if taglist is not None: tagstr = parse_tags(taglist) - if len(tagstr) == 0 or tagstr == DELIMITER: + if len(tagstr) == 0 or tagstr == DELIM: logger.error('Invalid tag') return False if len(tagstr) > 0: - tags = tagstr.split(DELIMITER) + tags = tagstr.split(DELIM) query = '%s WHERE' % query for tag in tags: if tag != '': is_tag_valid = True query += " tags LIKE '%' || ? || '%' OR" - tag = '%s%s%s' % (DELIMITER, tag, DELIMITER) + tag = '%s%s%s' % (DELIM, tag, DELIM) arguments += (tag,) if is_tag_valid: @@ -1152,9 +1146,9 @@ class BukuDb: ''' % (timestamp, timestamp)) for row in resultset: - out = '
= (3, 4, 4): # Python 3.4.4 and above - indb_conn = sqlite3.connect('file:%s?mode=ro' % filepath, uri=True) + indb_conn = sqlite3.connect('file:%s?mode=ro' % path, uri=True) else: - indb_conn = sqlite3.connect(filepath) + indb_conn = sqlite3.connect(path) indb_cur = indb_conn.cursor() indb_cur.execute('SELECT * FROM bookmarks') @@ -1468,35 +1462,35 @@ def parse_tags(keywords=None): if keywords is None: return None - tags = DELIMITER + tags = DELIM orig_tags = [] unique_tags = [] # Cleanse and get the tags tagstr = ' '.join(keywords) - marker = tagstr.find(DELIMITER) + marker = tagstr.find(DELIM) while marker >= 0: token = tagstr[0:marker] tagstr = tagstr[marker + 1:] - marker = tagstr.find(DELIMITER) + marker = tagstr.find(DELIM) token = token.strip() if token == '': continue - tags = '%s%s%s' % (tags, token, DELIMITER) + tags = '%s%s%s' % (tags, token, DELIM) tagstr = tagstr.strip() if tagstr != '': - tags = '%s%s%s' % (tags, tagstr, DELIMITER) + tags = '%s%s%s' % (tags, tagstr, DELIM) logger.debug('keywords: %s', keywords) logger.debug('parsed tags: [%s]', tags) - if tags == DELIMITER: + if tags == DELIM: return tags - orig_tags += tags.strip(DELIMITER).split(DELIMITER) + orig_tags += tags.strip(DELIM).split(DELIM) for tag in orig_tags: if tag not in unique_tags: unique_tags += (tag, ) # Select unique tags @@ -1505,7 +1499,7 @@ def parse_tags(keywords=None): sorted_tags = sorted(unique_tags, key=str.lower) # Wrap with delimiter - return '%s%s%s' % (DELIMITER, DELIMITER.join(sorted_tags), DELIMITER) + return '%s%s%s' % (DELIM, DELIM.join(sorted_tags), DELIM) def prompt(results, noninteractive=False): @@ -1595,8 +1589,8 @@ def print_record(row, idx=0): if row[4] != '': pr = '%s \x1B[91m+\x1B[0m %s\n' % (pr, row[4]) - # Append tags IF not default (DELIMITER) - if row[3] != DELIMITER: + # Append tags IF not default (delimiter) + if row[3] != DELIM: pr = '%s \x1B[91m#\x1B[0m %s\n' % (pr, row[3][1:-1]) print(pr) @@ -1746,7 +1740,7 @@ class CustomTagAction(argparse.Action): def __call__(self, parser, args, values, option_string=None): global tags_in - tags_in = [DELIMITER, ] + tags_in = [DELIM, ] setattr(args, self.dest, values) @@ -2021,7 +2015,7 @@ if __name__ == '__main__': # Add a record if args.addurl is not None: # Parse tags into a comma-separated string - tags = DELIMITER + tags = DELIM keywords = args.addurl if tags_in is not None: if tags_in[0] == '+' and len(tags_in) == 1: @@ -2029,10 +2023,10 @@ if __name__ == '__main__': elif tags_in[0] == '+': tags_in = tags_in[1:] # In case of add, args.addurl may have URL followed by tags - # Add DELIMITER as url+tags may not end with comma - keywords = args.addurl + [DELIMITER] + tags_in + # Add delimiter as url+tags may not end with one + keywords = args.addurl + [DELIM] + tags_in else: - keywords = args.addurl + [DELIMITER] + tags_in + keywords = args.addurl + [DELIM] + tags_in if len(keywords) > 1: tags = parse_tags(keywords[1:]) @@ -2114,8 +2108,7 @@ if __name__ == '__main__': elif tagsearch: search_opted = True if len(args.stag) > 0: - tag = '%s%s%s' % (DELIMITER, ' '.join(args.stag).strip(DELIMITER), - DELIMITER) + tag = '%s%s%s' % (DELIM, ' '.join(args.stag).strip(DELIM), DELIM) search_results = bdb.search_by_tag(tag) else: bdb.list_tags() @@ -2198,15 +2191,15 @@ if __name__ == '__main__': # Export bookmarks if args.export is not None: if args.tag is None: - bdb.export_bookmark(args.export[0], args.markdown) + bdb.exportdb(args.export[0], args.markdown) elif len(args.tag) == 0: logger.error('Missing tag') else: - bdb.export_bookmark(args.export[0], args.markdown, args.tag) + bdb.exportdb(args.export[0], args.markdown, args.tag) # Import bookmarks if args.imports is not None: - bdb.import_bookmark(args.imports[0], args.markdown) + bdb.importdb(args.imports[0], args.markdown) # Merge a database file and exit if args.merge is not None: