Add regex search support.

This commit is contained in:
Arun Prakash Jana 2016-10-21 17:15:10 +05:30
parent a0aadec0a8
commit 3ed68f0197
3 changed files with 17 additions and 3 deletions

View File

@ -14,7 +14,7 @@
`buku` is a powerful bookmark management utility written in Python3 and SQLite3. When I started writing it, I couldn't find a flexible cmdline solution with a private, portable, merge-able database along with browser integration. Hence, `buku` (after my son's nickname). `buku` is a powerful bookmark management utility written in Python3 and SQLite3. When I started writing it, I couldn't find a flexible cmdline solution with a private, portable, merge-able database along with browser integration. Hence, `buku` (after my son's nickname).
With tagging and multiple options to search bookmarks, including a deep scan mode (particularly for URLs), finding a bookmark is very easy. Multiple search results can be opened in the browser at once. With tagging and multiple options to search bookmarks, including regex and a deep scan mode (particularly for URLs), finding a bookmark is very easy. Multiple search results can be opened in the browser at once.
Though a terminal utility, it's possible to add bookmarks to `buku` without touching the terminal! Refer to the section on [GUI integration](#gui-integration). If you prefer the terminal, thanks to the shell completion scripts, you don't need to memorize any of the options. There's an Easter egg to revisit random forgotten bookmarks too. Though a terminal utility, it's possible to add bookmarks to `buku` without touching the terminal! Refer to the section on [GUI integration](#gui-integration). If you prefer the terminal, thanks to the shell completion scripts, you don't need to memorize any of the options. There's an Easter egg to revisit random forgotten bookmarks too.
@ -160,6 +160,7 @@ Shell completion scripts for Bash, Fish and Zsh can be found in respective subdi
special keyword - special keyword -
"blank": list entries with empty title/tag "blank": list entries with empty title/tag
--deep match substrings ('pen' matches 'opened') --deep match substrings ('pen' matches 'opened')
--sreg expr run a regex search
--st, --stag [...] search bookmarks by tag --st, --stag [...] search bookmarks by tag
list tags alphabetically, if no arguments list tags alphabetically, if no arguments
@ -224,6 +225,7 @@ Shell completion scripts for Bash, Fish and Zsh can be found in respective subdi
- -s : match any of the keywords in URL, title or tags. - -s : match any of the keywords in URL, title or tags.
- -S : match all the keywords in URL, title or tags. - -S : match all the keywords in URL, title or tags.
- --deep : match **substrings** (`match` matches `rematched`) in URL, title and tags. - --deep : match **substrings** (`match` matches `rematched`) in URL, title and tags.
- --sreg : match a regular expression (ignores --deep).
- --st : search bookmarks by tag, or show all tags alphabetically. - --st : search bookmarks by tag, or show all tags alphabetically.
- Search results are indexed serially. This index is different from actual database index of a bookmark record which is shown in bold within `[]` after the URL. - Search results are indexed serially. This index is different from actual database index of a bookmark record which is shown in bold within `[]` after the URL.
- **Encryption** is optional and manual. AES256 algorithm is used. To use encryption, the database file should be unlocked (-k) before using buku and locked (-l) afterwards. Between these 2 operations, the database file lies unencrypted on the disk, and NOT in memory. Also, note that the database file is *unencrypted on creation*. - **Encryption** is optional and manual. AES256 algorithm is used. To use encryption, the database file should be unlocked (-k) before using buku and locked (-l) afterwards. Between these 2 operations, the database file lies unencrypted on the disk, and NOT in memory. Also, note that the database file is *unencrypted on creation*.

12
buku
View File

@ -668,7 +668,8 @@ class BukuDb:
self.conn.commit() self.conn.commit()
def searchdb(self, keywords, all_keywords=False, delete=False, deep=False): def searchdb(self, keywords, all_keywords=False, delete=False, deep=False,
regex=False):
'''Search the database for an entries with tags or URL '''Search the database for an entries with tags or URL
or title info matching keywords and list those. or title info matching keywords and list those.
@ -699,7 +700,8 @@ class BukuDb:
else: # Match any keyword in URL or Title else: # Match any keyword in URL or Title
for token in keywords: for token in keywords:
if not deep: if not deep:
token = '\\b' + token + '\\b' if not regex:
token = '\\b' + token + '\\b'
query = '%s tags REGEXP ? OR URL REGEXP ? OR metadata \ query = '%s tags REGEXP ? OR URL REGEXP ? OR metadata \
REGEXP ? OR desc REGEXP ? OR' % (query) REGEXP ? OR desc REGEXP ? OR' % (query)
else: else:
@ -1726,6 +1728,7 @@ if __name__ == '__main__':
special keyword - special keyword -
"blank": list entries with empty title/tag "blank": list entries with empty title/tag
--deep match substrings ('pen' matches 'opened') --deep match substrings ('pen' matches 'opened')
--sreg expr run a regex search
--st, --stag [...] search bookmarks by tag --st, --stag [...] search bookmarks by tag
list tags alphabetically, if no arguments''') list tags alphabetically, if no arguments''')
addarg = search_grp.add_argument addarg = search_grp.add_argument
@ -1733,6 +1736,7 @@ if __name__ == '__main__':
help=argparse.SUPPRESS) help=argparse.SUPPRESS)
addarg('-S', '--sall', nargs='+', metavar='keyword', addarg('-S', '--sall', nargs='+', metavar='keyword',
help=argparse.SUPPRESS) help=argparse.SUPPRESS)
addarg('--sreg', nargs=1, metavar='keyword', help=argparse.SUPPRESS)
addarg('--deep', dest='deep', action='store_true', help=argparse.SUPPRESS) addarg('--deep', dest='deep', action='store_true', help=argparse.SUPPRESS)
addarg('--st', '--stag', nargs='*', dest='stag', addarg('--st', '--stag', nargs='*', dest='stag',
action=CustomTagSearchAction, metavar='keyword', action=CustomTagSearchAction, metavar='keyword',
@ -1907,6 +1911,10 @@ if __name__ == '__main__':
else: else:
bdb.searchdb(args.sall, True, (args.delete is not None), args.deep) bdb.searchdb(args.sall, True, (args.delete is not None), args.deep)
# Run a regular expression search
elif args.sreg is not None:
bdb.searchdb(args.sreg, False, (args.delete is not None), regex=True)
# Search bookmarks by tag and delete if wanted # Search bookmarks by tag and delete if wanted
elif tagsearch: elif tagsearch:
if len(args.stag) > 0: if len(args.stag) > 0:

4
buku.1
View File

@ -51,6 +51,7 @@ URLs are unique in DB. The same URL cannot be added twice.
- -s : match any of the keywords in URL, title or tags. - -s : match any of the keywords in URL, title or tags.
- -S : match all the keywords in URL, title or tags. - -S : match all the keywords in URL, title or tags.
- --deep : match \fBsubstrings\fR (`match` matches `rematched`) in URL, title and tags. - --deep : match \fBsubstrings\fR (`match` matches `rematched`) in URL, title and tags.
- --sreg : match a regular expression (ignores --deep).
- --st : search bookmarks by tag, or show all tags alphabetically. - --st : search bookmarks by tag, or show all tags alphabetically.
- Search results are indexed serially. This index is different from actual database index of a bookmark record which is shown in bold within '[]' after the URL. - Search results are indexed serially. This index is different from actual database index of a bookmark record which is shown in bold within '[]' after the URL.
.PP .PP
@ -95,6 +96,9 @@ Special keyword:
.br .br
"blank": list entries with empty title/tag "blank": list entries with empty title/tag
.TP .TP
.BI \--sreg " expr"
Scan for a regular expression match.
.TP
.BI \--deep .BI \--deep
Search modifier to match substrings. Works with -s, -S. Search modifier to match substrings. Works with -s, -S.
.TP .TP