89f560aead2b688a8c6dc633f6695f3140d684b6
[hooke.git] / hooke / plugin / engine.py
1 # Copyright
2
3 """The `engine` module provides :class:`EnginePlugin` and several
4 associated :class:`hooke.command.Command`\s for basic
5 :class:`~hooke.hooke.Hooke` and :class:`~hooke.engine.CommandEngine`
6 interaction.
7 """
8
9 from ..command import CommandExit, Exit, Command, Argument
10 from ..interaction import BooleanRequest
11 from . import Builtin
12
13
14 class EnginePlugin (Builtin):
15     def __init__(self):
16         super(EnginePlugin, self).__init__(name='engine')
17         self._commands = [
18             ExitCommand(self), HelpCommand(self)]
19
20
21 class ExitCommand (Command):
22     """Exit Hooke cleanly.
23     """
24     def __init__(self, plugin):
25         super(ExitCommand, self).__init__(
26             name='exit',
27             arguments = [
28                 Argument(name='force', type='bool', default=False,
29                          help="""
30 Exit without prompting the user.  Use if you save often or don't make
31 typing mistakes ;).
32 """.strip()),
33                 ],
34              help=self.__doc__, plugin=plugin)
35
36     def _run(self, hooke, inqueue, outqueue, params):
37         """Exit Hooke, prompting if there are unsaved changes.
38         """
39         _exit = True
40         if params['force'] == False:
41             not_saved = [p.name for p in hooke.playlists
42                          if p.is_saved() == False]
43             msg = 'Exit?'
44             default = True
45             if len(not_saved) > 0:
46                 msg = 'Unsaved playlists (%s).  %s' \
47                     % (', '.join([str(p) for p in not_saved]), msg)
48                 default = False
49             outqueue.put(BooleanRequest(msg, default))
50             result = inqueue.get()
51             assert result.type == 'boolean'
52             _exit = result.value
53             if _exit == False:
54                 return
55             # TODO: check for unsaved config file
56         if _exit == True:
57             raise Exit()
58
59
60 class HelpCommand (Command):
61     """Called with an argument, prints that command's documentation.
62
63     With no argument, lists all available help topics as well as any
64     undocumented commands.
65     """
66     def __init__(self, plugin):
67         super(HelpCommand, self).__init__(
68             name='help', help=self.__doc__, plugin=plugin)
69         # We set .arguments now (vs. using th arguments option to __init__),
70         # to overwrite the default help argument.  We don't override
71         # :meth:`cmd.Cmd.do_help`, so `help --help` is not a valid command.
72         self.arguments = [
73             Argument(name='command', type='string', optional=True,
74                      help='The name of the command you want help with.')
75             ]
76
77     def _run(self, hooke, inqueue, outqueue, params):
78         if params['command'] == None:
79             outqueue.put(sorted([c.name for c in hooke.commands]))
80         else:
81             outqueue.put(hooke.command_by_name[params['command']].help())