More commits on functions.

Signed-off-by: Arun Prakash Jana <engineerarun@gmail.com>
This commit is contained in:
Arun Prakash Jana 2016-04-05 18:25:29 +05:30
parent ccd7c8b035
commit fb6ec10648

29
buku
View File

@ -653,8 +653,13 @@ def is_int(string):
# Open a URL in browser
def browser_open(url): def browser_open(url):
"""Duplicate stdin, stdout (to suppress showing errors
on the terminal) and open URL in default browser
Params: url to open
"""
url = url.replace("%22", "\"") url = url.replace("%22", "\"")
_stderr = os.dup(2) _stderr = os.dup(2)
@ -675,8 +680,12 @@ def browser_open(url):
# Get the SHA256 hash of a file
def get_filehash(filepath): def get_filehash(filepath):
"""Get the SHA256 hash of a file
Params: path to the file
"""
with open(filepath, 'rb') as f: with open(filepath, 'rb') as f:
hasher = hashlib.sha256() hasher = hashlib.sha256()
buf = f.read(BLOCKSIZE) buf = f.read(BLOCKSIZE)
@ -688,8 +697,9 @@ def get_filehash(filepath):
# Encrypt the bookmarks database file
def encrypt_file(): def encrypt_file():
"""Encrypt the bookmarks database file"""
dbpath = os.path.join(os.environ.get('HOME'), '.cache', 'buku', 'bookmarks.db') dbpath = os.path.join(os.environ.get('HOME'), '.cache', 'buku', 'bookmarks.db')
encpath = dbpath + '.enc' encpath = dbpath + '.enc'
if not os.path.exists(dbpath): if not os.path.exists(dbpath):
@ -748,8 +758,9 @@ def encrypt_file():
# Decrypt the bookmarks database file
def decrypt_file(): def decrypt_file():
"""Decrypt the bookmarks database file"""
dbpath = os.path.join(os.environ.get('HOME'), '.cache', 'buku', 'bookmarks.db') dbpath = os.path.join(os.environ.get('HOME'), '.cache', 'buku', 'bookmarks.db')
encpath = dbpath + '.enc' encpath = dbpath + '.enc'
if not os.path.exists(encpath): if not os.path.exists(encpath):
@ -805,8 +816,9 @@ def decrypt_file():
# SIGINT handler
def sigint_handler(signum, frame): def sigint_handler(signum, frame):
"""Custom SIGINT handler"""
print('\nInterrupted.', file=sys.stderr) print('\nInterrupted.', file=sys.stderr)
sys.exit(1) sys.exit(1)
@ -815,11 +827,18 @@ signal.signal(signal.SIGINT, sigint_handler)
def printmsg(msg, level=None): def printmsg(msg, level=None):
"""Print a message in 2 parts, with the level in bold
Params: msg, level
"""
if level is not None: if level is not None:
print("\x1b[1m%s:\x1b[21m %s" % (level, msg)) print("\x1b[1m%s:\x1b[21m %s" % (level, msg))
else: else:
print("%s" % msg) print("%s" % msg)
# Main starts here # Main starts here
# ---------------- # ----------------
pipeargs = [] pipeargs = []