3 """The `system` module provides :class:`SystemPlugin` and several
4 associated :class:`hooke.command.Command`\s for interacting with the
5 operating system and execution environment.
13 from ..command import Command, Argument
14 from ..plugin import Builtin
17 class SystemPlugin (Builtin):
19 super(SystemPlugin, self).__init__(name='system')
22 return [ListDirectoryCommand(), GetWorkingDirectoryCommand(),
23 ChangeDirectoryCommand(), SystemCommand()]
26 class ListDirectoryCommand (Command):
27 """List the files in a directory.
30 super(ListDirectoryCommand, self).__init__(
31 name='ls', aliases=['dir'],
34 name='path', type='path', default='.',
36 Path to the directory whose contents get listed. Defaults to the
37 current working directory.
42 def _run(self, hooke, inqueue, outqueue, params):
43 outqueue.put('\n'.join(sorted(os.listdir(params['path']))))
45 class GetWorkingDirectoryCommand (Command):
46 """Get the current working directory.
49 super(GetWorkingDirectoryCommand, self).__init__(
50 name='cwd', aliases=['pwd'], help=self.__doc__)
52 def _run(self, hooke, inqueue, outqueue, params):
53 outqueue.put(os.getcwd())
55 class ChangeDirectoryCommand (Command):
56 """Change the current working directory.
59 super(ChangeDirectoryCommand, self).__init__(
63 name='path', type='path', default='~',
65 Path of the directory to change into. Default to the user's home
71 def _run(self, hooke, inqueue, outqueue, params):
72 os.chdir(os.path.expanduser(params['path']))
74 class SystemCommand (Command):
75 """Execute a system command and report the output.
78 super(SystemCommand, self).__init__(
82 name='command', type='string', optional=False, count=-1,
84 Command line to execute.
89 def _run(self, hooke, inqueue, outqueue, params):
91 params['command'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
92 stdout,stderr = p.communicate()