6564549ae3f6415de305819ddabb7b2358c331f9
[hooke.git] / hooke / plugin / engine.py
1 # Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
2 #
3 # This file is part of Hooke.
4 #
5 # Hooke is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
9 #
10 # Hooke is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
13 # Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with Hooke.  If not, see
17 # <http://www.gnu.org/licenses/>.
18
19 """The `engine` module provides :class:`EnginePlugin` and several
20 associated :class:`hooke.command.Command`\s for basic
21 :class:`~hooke.hooke.Hooke` and :class:`~hooke.engine.CommandEngine`
22 interaction.
23 """
24
25 from ..command import CommandExit, Exit, Command, Argument
26 from ..interaction import BooleanRequest
27 from . import Builtin
28
29
30 class EnginePlugin (Builtin):
31     def __init__(self):
32         super(EnginePlugin, self).__init__(name='engine')
33         self._commands = [
34             ExitCommand(self), HelpCommand(self)]
35
36
37 class ExitCommand (Command):
38     """Exit Hooke cleanly.
39     """
40     def __init__(self, plugin):
41         super(ExitCommand, self).__init__(
42             name='exit',
43             arguments = [
44                 Argument(name='force', type='bool', default=False,
45                          help="""
46 Exit without prompting the user.  Use if you save often or don't make
47 typing mistakes ;).
48 """.strip()),
49                 ],
50              help=self.__doc__, plugin=plugin)
51
52     def _run(self, hooke, inqueue, outqueue, params):
53         """Exit Hooke, prompting if there are unsaved changes.
54         """
55         _exit = True
56         if params['force'] == False:
57             not_saved = [p.name for p in hooke.playlists
58                          if p.is_saved() == False]
59             msg = 'Exit?'
60             default = True
61             if len(not_saved) > 0:
62                 msg = 'Unsaved playlists (%s).  %s' \
63                     % (', '.join([str(p) for p in not_saved]), msg)
64                 default = False
65             outqueue.put(BooleanRequest(msg, default))
66             result = inqueue.get()
67             assert result.type == 'boolean'
68             _exit = result.value
69             if _exit == False:
70                 return
71             # TODO: check for unsaved config file
72         if _exit == True:
73             raise Exit()
74
75
76 class HelpCommand (Command):
77     """Called with an argument, prints that command's documentation.
78
79     With no argument, lists all available help topics as well as any
80     undocumented commands.
81     """
82     def __init__(self, plugin):
83         super(HelpCommand, self).__init__(
84             name='help', help=self.__doc__, plugin=plugin)
85         # We set .arguments now (vs. using th arguments option to __init__),
86         # to overwrite the default help argument.  We don't override
87         # :meth:`cmd.Cmd.do_help`, so `help --help` is not a valid command.
88         self.arguments = [
89             Argument(name='command', type='string', optional=True,
90                      help='The name of the command you want help with.')
91             ]
92
93     def _run(self, hooke, inqueue, outqueue, params):
94         if params['command'] == None:
95             outqueue.put(sorted([c.name for c in hooke.commands]))
96         else:
97             outqueue.put(hooke.command_by_name[params['command']].help())