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