1 """The `system` module provides :class:`SystemPlugin` and several
2 associated :class:`hooke.command.Command`\s for interacting with the
3 operating system and execution environment.
11 from ..command import Command, Argument
12 from ..plugin import Builtin
15 class SystemPlugin (Builtin):
17 super(SystemPlugin, self).__init__(name='system')
20 return [ListDirectoryCommand(), GetWorkingDirectoryCommand(),
21 ChangeDirectoryCommand(), SystemCommand()]
24 class ListDirectoryCommand (Command):
25 """List the files in a directory.
28 super(ListDirectoryCommand, self).__init__(
29 name='ls', aliases=['dir'],
32 name='path', type='path', default='.',
34 Path to the directory whose contents get listed. Defaults to the
35 current working directory.
40 def _run(self, hooke, inqueue, outqueue, params):
41 outqueue.put('\n'.join(sorted(os.listdir(params['path']))))
43 class GetWorkingDirectoryCommand (Command):
44 """Get the current working directory.
47 super(GetWorkingDirectoryCommand, self).__init__(
48 name='cwd', aliases=['pwd'], help=self.__doc__)
50 def _run(self, hooke, inqueue, outqueue, params):
51 outqueue.put(os.getcwd())
53 class ChangeDirectoryCommand (Command):
54 """Change the current working directory.
57 super(ChangeDirectoryCommand, self).__init__(
61 name='path', type='path', default='~',
63 Path of the directory to change into. Default to the user's home
69 def _run(self, hooke, inqueue, outqueue, params):
70 os.chdir(os.path.expanduser(params['path']))
72 class SystemCommand (Command):
73 """Execute a system command and report the output.
76 super(SystemCommand, self).__init__(
80 name='command', type='string', optional=False, count=-1,
82 Command line to execute.
87 def _run(self, hooke, inqueue, outqueue, params):
89 params['command'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
90 stdout,stderr = p.communicate()