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')
37 ListDirectoryCommand(self), GetWorkingDirectoryCommand(self),
38 ChangeDirectoryCommand(self), SystemCommand(self)]
41 class ListDirectoryCommand (Command):
42 """List the files in a directory.
44 def __init__(self, plugin):
45 super(ListDirectoryCommand, self).__init__(
46 name='ls', aliases=['dir'],
49 name='path', type='path', default='.',
51 Path to the directory whose contents get listed. Defaults to the
52 current working directory.
55 help=self.__doc__, plugin=plugin)
57 def _run(self, hooke, inqueue, outqueue, params):
58 outqueue.put('\n'.join(sorted(os.listdir(params['path']))))
60 class GetWorkingDirectoryCommand (Command):
61 """Get the current working directory.
63 def __init__(self, plugin):
64 super(GetWorkingDirectoryCommand, self).__init__(
65 name='cwd', aliases=['pwd'], help=self.__doc__, plugin=plugin)
67 def _run(self, hooke, inqueue, outqueue, params):
68 outqueue.put(os.getcwd())
70 class ChangeDirectoryCommand (Command):
71 """Change the current working directory.
73 def __init__(self, plugin):
74 super(ChangeDirectoryCommand, self).__init__(
78 name='path', type='path', default='~',
80 Path of the directory to change into. Default to the user's home
84 help=self.__doc__, plugin=plugin)
86 def _run(self, hooke, inqueue, outqueue, params):
87 os.chdir(os.path.expanduser(params['path']))
89 class SystemCommand (Command):
90 """Execute a system command and report the output.
92 def __init__(self, plugin):
93 super(SystemCommand, self).__init__(
97 name='command', type='string', optional=False, count=-1,
99 Command line to execute.
102 help=self.__doc__, plugin=plugin)
104 def _run(self, hooke, inqueue, outqueue, params):
105 p = subprocess.Popen(
106 params['command'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
107 stdout,stderr = p.communicate()
108 returncode = p.wait()