Merge pull request #10 from AMDmi3/datapath

Fix database path
This commit is contained in:
Arun Prakash Jana 2016-04-08 19:03:34 +05:30
commit 7560400921
3 changed files with 54 additions and 8 deletions

View File

@ -6,10 +6,12 @@
You can add bookmarks to `buku` with title and tags, optionally fetch page title from web, search by keywords for matching tags or title or URL, update and remove bookmarks, title or tags. You can open the URLs from search results directly in the browser. You can encrypt or decrypt the database file manually, optionally with custom number of hash passes for key generation.
`buku` can also handle piped input, which lets you combine it with `xsel` (on Linux) and use a shortcut to add selected or copied text as bookmark without touching the terminal.
`buku` can also handle piped input, which lets you combine it with `xsel` (on Linux) and use a shortcut to add selected or copied text as bookmark without touching the terminal.
Ref: [buku & xsel: add selected or copied URL as bookmark](http://tuxdiary.com/2016/03/26/buku-xsel/)
The SQLite3 database file is stored in `$HOME/.cache/buku/bookmarks.db` for each user.
The SQLite3 database file is stored in `$HOME/.local/share/buku/bookmarks.db` (or `$XDG_DATA_HOME/buku/bookmarks.db`, if XDG_DATA_HOME is defined) for each user.
Before version 1.9, buku stored database in `$HOME/.cache/buku/bookmarks.db`. If that location exists, buku automatically moves the database to new location.
`buku` is **GPLv3** licensed. Copyright (C) 2015 [Arun Prakash Jana](mailto:engineerarun@gmail.com).

48
buku
View File

@ -27,6 +27,7 @@ import html.parser as HTMLParser
from http.client import HTTPConnection, HTTPSConnection
from urllib.parse import urljoin, unquote
import signal
import shutil
# Import libraries needed for encryption
try:
@ -69,7 +70,7 @@ encrypt = False # Lock database file
decrypt = False # Unlock database file
iterations = 8 # Number of hash iteratons to generate key
pipeargs = [] # Holds arguments piped to the program
_VERSION_ = 1.8 # Program version
_VERSION_ = 1.9 # Program version
class BMHTMLParser(HTMLParser.HTMLParser):
@ -102,6 +103,42 @@ class BMHTMLParser(HTMLParser.HTMLParser):
def getDataPath():
data_home = os.environ.get('XDG_DATA_HOME')
if data_home is None:
if os.environ.get('HOME') is None:
data_home = '.'
else:
data_home = os.path.join(os.environ.get('HOME'), '.local', 'share')
return os.path.join(data_home, 'buku')
def moveOldDatabase():
olddbpath = os.path.join(os.environ.get('HOME'), '.cache', 'buku')
olddbfile = os.path.join(olddbpath, 'bookmarks.db')
if not os.path.exists(olddbfile):
return
newdbpath = getDataPath()
newdbfile = os.path.join(newdbpath, 'bookmarks.db')
if os.path.exists(newdbfile):
print("Both old (%s) and new (%s) databases exist, need manual action" % (olddbfile, newdbfile))
sys.exit(1)
if not os.path.exists(newdbpath):
os.makedirs(newdbpath)
shutil.move(olddbfile, newdbfile)
print("Database was moved from old (%s) to new (%s) location" % (olddbfile, newdbfile))
os.rmdir(olddbpath)
def initdb():
"""Initialize the database connection. Create DB file and/or bookmarks table
if they don't exist. Alert on encryption options on first execution.
@ -109,7 +146,7 @@ def initdb():
Returns: connection, cursor
"""
dbpath = os.path.join(os.environ.get('HOME'), '.cache', 'buku')
dbpath = getDataPath()
if not os.path.exists(dbpath):
os.makedirs(dbpath)
@ -663,7 +700,7 @@ def get_filehash(filepath):
def encrypt_file():
"""Encrypt the bookmarks database file"""
dbpath = os.path.join(os.environ.get('HOME'), '.cache', 'buku', 'bookmarks.db')
dbpath = os.path.join(getDataPath(), 'bookmarks.db')
encpath = dbpath + '.enc'
if not os.path.exists(dbpath):
print("%s missing. Already encrypted?" % dbpath)
@ -724,7 +761,7 @@ def encrypt_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(getDataPath(), 'bookmarks.db')
encpath = dbpath + '.enc'
if not os.path.exists(encpath):
printmsg((encpath + " missing"), "ERROR")
@ -1005,6 +1042,9 @@ if online == True and titleManual != None:
print("You can either fetch title from web or add/update title manually.\n")
usage()
# Move database to new location, if needed
moveOldDatabase()
# Handle encrypt/decrypt options at top priority
if encrypt == True:
encrypt_file()

8
buku.1
View File

@ -1,4 +1,4 @@
.TH "BUKU" "1" "Mar 2016" "Version 1.8" "User Commands"
.TH "BUKU" "8" "Mar 2016" "Version 1.9" "User Commands"
.SH NAME
buku \- A private cmdline bookmark manager. Your mini web!
.SH SYNOPSIS
@ -11,7 +11,11 @@ buku \- A private cmdline bookmark manager. Your mini web!
.B buku
is a command-line tool to save, tag and search bookmarks.
.PP
The SQLite3 database file is stored in \fB$HOME/.cache/bookmarks.db\fR for each user.
The SQLite3 database file is stored in \fB$HOME/.local/share/buku/bookmarks.db\fR (or \fB$XDG_DATA_HOME/buku/bookmarks.db\fR, if XDG_DATA_HOME is defined) for each user.
.PP
Before version 1.9,
.B buku
stored database in \fB$HOME/.cache/buku/bookmarks.db\fR. If that location exists, buku automatically moves the database to new location.
.SH OPERATIONAL NOTES
URL must precede the tags. Manual title with multiple keywords must be within quotes.
.PP