1 # Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
3 # This file is part of Hooke.
5 # Hooke is free software: you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation, either
8 # version 3 of the License, or (at your option) any later version.
10 # Hooke is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Lesser General Public License for more details.
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/>.
19 """The `system` module provides :class:`SystemPlugin` and several
20 associated :class:`hooke.command.Command`\s for interacting with the
21 operating system and execution environment.
29 from ..command import Command, Argument
30 from ..plugin import Builtin
33 class SystemPlugin (Builtin):
35 super(SystemPlugin, self).__init__(name='system')
38 return [ListDirectoryCommand(), GetWorkingDirectoryCommand(),
39 ChangeDirectoryCommand(), SystemCommand()]
42 class ListDirectoryCommand (Command):
43 """List the files in a directory.
46 super(ListDirectoryCommand, self).__init__(
47 name='ls', aliases=['dir'],
50 name='path', type='path', default='.',
52 Path to the directory whose contents get listed. Defaults to the
53 current working directory.
58 def _run(self, hooke, inqueue, outqueue, params):
59 outqueue.put('\n'.join(sorted(os.listdir(params['path']))))
61 class GetWorkingDirectoryCommand (Command):
62 """Get the current working directory.
65 super(GetWorkingDirectoryCommand, self).__init__(
66 name='cwd', aliases=['pwd'], help=self.__doc__)
68 def _run(self, hooke, inqueue, outqueue, params):
69 outqueue.put(os.getcwd())
71 class ChangeDirectoryCommand (Command):
72 """Change the current working directory.
75 super(ChangeDirectoryCommand, self).__init__(
79 name='path', type='path', default='~',
81 Path of the directory to change into. Default to the user's home
87 def _run(self, hooke, inqueue, outqueue, params):
88 os.chdir(os.path.expanduser(params['path']))
90 class SystemCommand (Command):
91 """Execute a system command and report the output.
94 super(SystemCommand, self).__init__(
98 name='command', type='string', optional=False, count=-1,
100 Command line to execute.
105 def _run(self, hooke, inqueue, outqueue, params):
106 p = subprocess.Popen(
107 params['command'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
108 stdout,stderr = p.communicate()
109 returncode = p.wait()