Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add commands for managing queries in namedqueries mode #1533

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions alot/commands/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from ..completion.tags import TagsCompleter
from ..widgets.utils import DialogBox
from ..db.errors import DatabaseLockedError
from ..db.errors import DatabaseROError
from ..db.envelope import Envelope
from ..settings.const import settings
from ..settings.errors import ConfigError, NoMatchingAccount
Expand Down Expand Up @@ -1122,7 +1123,7 @@ def __init__(self, alias, query=None, flush=True, **kwargs):
self.flush = flush
Command.__init__(self, **kwargs)

def apply(self, ui):
async def apply(self, ui):
msg = 'saved alias "%s" for query string "%s"' % (self.alias,
self.query)

Expand All @@ -1136,7 +1137,7 @@ def apply(self, ui):

# flush index
if self.flush:
ui.apply_command(commands.globals.FlushCommand())
return await ui.apply_command(commands.globals.FlushCommand())


@registerCommand(
Expand All @@ -1153,7 +1154,7 @@ class RemoveQueryCommand(Command):
"""remove named query string for given alias"""
repeatable = False

def __init__(self, alias, flush=True, **kwargs):
def __init__(self, alias, afterwards=None, flush=True, **kwargs):
"""
:param alias: name to use for query string
:type alias: str
Expand All @@ -1162,13 +1163,14 @@ def __init__(self, alias, flush=True, **kwargs):
"""
self.alias = alias
self.flush = flush
self.afterwards = afterwards
Command.__init__(self, **kwargs)

def apply(self, ui):
async def apply(self, ui):
msg = 'removed alias "%s"' % (self.alias)

try:
ui.dbman.remove_named_query(self.alias)
ui.dbman.remove_named_query(self.alias, afterwards=self.afterwards)
logging.debug(msg)
ui.notify(msg)
except DatabaseROError:
Expand All @@ -1177,7 +1179,7 @@ def apply(self, ui):

# flush index
if self.flush:
ui.apply_command(commands.globals.FlushCommand())
await ui.apply_command(commands.globals.FlushCommand())


@registerCommand(
Expand Down
91 changes: 91 additions & 0 deletions alot/commands/namedqueries.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
import argparse

from . import Command, registerCommand
from .globals import ConfirmCommand
from .globals import FlushCommand
from .globals import PromptCommand
from .globals import RemoveQueryCommand
from .globals import SearchCommand
from ..db.errors import DatabaseROError

MODE = 'namedqueries'

Expand All @@ -28,3 +33,89 @@ async def apply(self, ui):

cmd = SearchCommand(query=query)
await ui.apply_command(cmd)


@registerCommand(MODE, 'query-rename', arguments=[
(['newalias'], {'help': 'new name for the query'}),
])
class NamedqueriesRenameCommand(Command):

"""rename a query"""
def __init__(self, newalias, **kwargs):
self.newalias = newalias
self.complete_count = 0

def afterwards(self):
self.complete_count += 1
if self.complete_count == 2:
self.ui.current_buffer.rebuild()

async def apply(self, ui):
oldalias = ui.current_buffer.get_selected_query()
querydict = ui.dbman.get_named_queries()
query_string = querydict[oldalias]
self.ui = ui # save for callback
try:
ui.dbman.save_named_query(self.newalias, query_string,
afterwards=self.afterwards)
except DatabaseROError:
ui.notify('index in read-only mode', priority='error')
return
ui.dbman.remove_named_query(oldalias, afterwards=self.afterwards)
return await ui.apply_command(FlushCommand())


@registerCommand(MODE, 'query-duplicate', arguments=[
(['newalias'], {'help': 'name for duplicated query', 'nargs': '?'}),
])
class NamedqueriesDuplicateCommand(Command):

"""create a copy of the query"""
def __init__(self, newalias, **kwargs):
self.newalias = newalias

def afterwards(self):
self.ui.current_buffer.rebuild()

async def apply(self, ui):
oldalias = ui.current_buffer.get_selected_query()
self.newalias = self.newalias or (oldalias + '_copy')
querydict = ui.dbman.get_named_queries()
query_string = querydict[oldalias]
self.ui = ui # save for callback
try:
ui.dbman.save_named_query(self.newalias, query_string,
afterwards=self.afterwards)
except DatabaseROError:
ui.notify('index in read-only mode', priority='error')
return
return await ui.apply_command(FlushCommand())


@registerCommand(MODE, 'query-refine')
class NamedqueriesRefineCommand(Command):

"""refine a query string"""
async def apply(self, ui):
alias = ui.current_buffer.get_selected_query()
query_string = ui.dbman.get_named_queries()[alias]
await ui.apply_command(PromptCommand('savequery {} {}'.format(
alias, query_string)))
ui.current_buffer.rebuild()


@registerCommand(MODE, 'query-remove')
class NamedqueriesRemoveCommand(Command):

def afterwards(self):
self.ui.current_buffer.rebuild()

"""remove the selected namedquery"""
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy found an issue: String statement has no effect

async def apply(self, ui):
self.ui = ui
alias = ui.current_buffer.get_selected_query()
await ui.apply_command(ConfirmCommand(
msg=['Remove named query {}'.format(alias)]))
# SequenceCanceled raised if not confirmed
await ui.apply_command(
RemoveQueryCommand(alias, afterwards=self.afterwards))