1 # Copyright (C) 2010-2012 W. Trevor King <wking@drexel.edu>
3 # This file is part of Hooke.
5 # Hooke is free software: you can redistribute it and/or modify it under the
6 # terms of the GNU Lesser General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option) any
10 # Hooke is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with Hooke. If not, see <http://www.gnu.org/licenses/>.
18 """The `system` module provides :class:`SystemPlugin` and several
19 associated :class:`hooke.command.Command`\s for interacting with the
20 operating system and execution environment.
28 from ..command import Command, Argument
32 class SystemPlugin (Builtin):
34 super(SystemPlugin, self).__init__(name='system')
36 ListDirectoryCommand(self), GetWorkingDirectoryCommand(self),
37 ChangeDirectoryCommand(self), SystemCommand(self)]
40 class ListDirectoryCommand (Command):
41 """List the files in a directory.
43 def __init__(self, plugin):
44 super(ListDirectoryCommand, self).__init__(
45 name='ls', aliases=['dir'],
48 name='path', type='path', default='.',
50 Path to the directory whose contents get listed. Defaults to the
51 current working directory.
54 help=self.__doc__, plugin=plugin)
56 def _run(self, hooke, inqueue, outqueue, params):
57 outqueue.put('\n'.join(sorted(os.listdir(params['path']))))
59 class GetWorkingDirectoryCommand (Command):
60 """Get the current working directory.
62 def __init__(self, plugin):
63 super(GetWorkingDirectoryCommand, self).__init__(
64 name='cwd', aliases=['pwd'], help=self.__doc__, plugin=plugin)
66 def _run(self, hooke, inqueue, outqueue, params):
67 outqueue.put(os.getcwd())
69 class ChangeDirectoryCommand (Command):
70 """Change the current working directory.
72 def __init__(self, plugin):
73 super(ChangeDirectoryCommand, self).__init__(
77 name='path', type='path', default='~',
79 Path of the directory to change into. Default to the user's home
83 help=self.__doc__, plugin=plugin)
85 def _run(self, hooke, inqueue, outqueue, params):
86 os.chdir(os.path.expanduser(params['path']))
88 class SystemCommand (Command):
89 """Execute a system command and report the output.
91 def __init__(self, plugin):
92 super(SystemCommand, self).__init__(
96 name='command', type='string', optional=False, count=-1,
98 Command line to execute.
101 help=self.__doc__, plugin=plugin)
103 def _run(self, hooke, inqueue, outqueue, params):
104 p = subprocess.Popen(
105 params['command'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
106 stdout,stderr = p.communicate()
107 returncode = p.wait()