More options added to argparse.

This commit is contained in:
Arun Prakash Jana 2016-04-25 02:48:56 +05:30
parent 340f048d46
commit 58b8805b70
No known key found for this signature in database
GPG Key ID: C0A712ED95043DCB

23
buku
View File

@ -51,16 +51,15 @@ except ImportError:
# Globals # Globals
addindex = None # DB index to insert URL into addindex = None # DB index to insert URL into
openurl = None # Open URL in browser
showindex = None # Index of bookmark to show showindex = None # Index of bookmark to show
showOpt = 0 # Modify show. 1: show only URL, 2: show URL and tag showOpt = 0 # Modify show. 1: show only URL, 2: show URL and tag
entry = None # DB index to update or delete entry = None # DB index to update or delete
update = False # Update a bookmark in DB update = False # Update a bookmark in DB
debug = False # Enable debug logs
titleData = None # Title fetched from a page titleData = None # Title fetched from a page
titleManual = None # Manually add a title offline titleManual = None # Manually add a title offline
iterations = 8 # Number of hash iterations to generate key iterations = 8 # Number of hash iterations to generate key
jsonOutput = False # Output json formatted result jsonOutput = False # Output json formatted result
debug = False # Enable debug logs
pipeargs = [] # Holds arguments piped to the program pipeargs = [] # Holds arguments piped to the program
_VERSION_ = 1.9 # Program version _VERSION_ = 1.9 # Program version
@ -597,7 +596,8 @@ def cleardb(conn, cur, index):
def printdb(cur, index, empty=False): def printdb(cur, index, empty=False):
"""Print bookmark details at index or all bookmarks if index is None """Print bookmark details at index or all bookmarks if index is None
Print only bookmarks with empty title or tags if empty is True Print only bookmarks with blank title or tags if empty is True
Note: URL is printed on top because title may be blank
Params: cursor, index to print, flag to show only bookmarks with no title or tags Params: cursor, index to print, flag to show only bookmarks with no title or tags
""" """
@ -748,7 +748,7 @@ def fetchopen(index):
""" """
try: try:
for row in cur.execute("SELECT URL FROM bookmarks WHERE id = ?", (int(index),)): for row in cur.execute("SELECT URL FROM bookmarks WHERE id = ?", (index,)):
url = unquote(row[0]) url = unquote(row[0])
browser_open(url) browser_open(url)
return return
@ -927,6 +927,7 @@ def decrypt_file():
if dbhash != enchash: if dbhash != enchash:
os.remove(dbpath) os.remove(dbpath)
print("Decryption failed"); print("Decryption failed");
sys.exit(1)
else: else:
os.remove(encpath) os.remove(encpath)
print("File decrypted") print("File decrypted")
@ -1049,13 +1050,13 @@ addarg('-k', '--decrypt', dest='decrypt', action='store_true',
help='decrypt (unlock) database file') help='decrypt (unlock) database file')
addarg('-l', '--encrypt', dest='encrypt', action='store_true', # DONE addarg('-l', '--encrypt', dest='encrypt', action='store_true', # DONE
help='encrypt (lock) database file') help='encrypt (lock) database file')
addarg('-o', '--open', dest='openurl', type=int, metavar='N', addarg('-o', '--open', dest='openurl', type=int, metavar='N', # DONE
help='open URL at DB index N in browser') help='open URL at DB index N in browser')
addarg('-p', '--print', dest='showindex', type=int, metavar='N', addarg('-p', '--print', dest='showindex', type=int, metavar='N',
help='show details of bookmark record at DB index N, N=0 shows all') help='show details of bookmark record at DB index N, N=0 shows all')
addarg('-r', '--replace', nargs=2, dest='replace', metavar=('oldtag', 'newtag'), addarg('-r', '--replace', nargs=2, dest='replace', metavar=('oldtag', 'newtag'), # DONE
help='replace oldtag with newtag, delete oldtag if newtag=none') help='replace oldtag with newtag, delete oldtag if newtag=none')
addarg('-t', '--iterate', dest='iterations', type=int, metavar='N', addarg('-t', '--iterate', dest='iterations', type=int, metavar='N', # DONE
help='use N (> 0) hash iterations to generate key, for -k, -l') help='use N (> 0) hash iterations to generate key, for -k, -l')
addarg('-x', '--format', dest='showOpt', metavar='N', addarg('-x', '--format', dest='showOpt', metavar='N',
help='modify -p behaviour, N=1: show only URL, N=2: show URL and tag') help='modify -p behaviour, N=1: show only URL, N=2: show URL and tag')
@ -1188,6 +1189,8 @@ except GetoptError as e:
args = argparser.parse_args() args = argparser.parse_args()
titleManual = args.titleManual titleManual = args.titleManual
if args.iterations is not None:
iterations = args.iterations
jsonOutput = args.jsonOutput jsonOutput = args.jsonOutput
debug = args.debug debug = args.debug
keywords = [] keywords = []
@ -1212,7 +1215,7 @@ if args.decrypt == True:
sys.exit(1) sys.exit(1)
decrypt_file() decrypt_file()
# Initilize the database and get handles # Initialize the database and get handles
conn, cur = initdb() conn, cur = initdb()
# Add a record # Add a record
@ -1256,8 +1259,8 @@ if args.empty == True:
printdb(cur, 0, True) printdb(cur, 0, True)
# Open URL in browser # Open URL in browser
if openurl != None: if args.openurl is not None:
fetchopen(openurl) fetchopen(args.openurl)
# Close the connection before exiting # Close the connection before exiting
conn.close() conn.close()