5a56deda381c7aa56b37b2efff97f30196df70ea
[hooke.git] / hooke / plugin / system.py
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.
4 """
5
6 import os
7 import os.path
8 import subprocess
9 import sys
10
11 from ..command import Command, Argument
12 from ..plugin import Builtin
13
14
15 class SystemPlugin (Builtin):
16     def __init__(self):
17         super(SystemPlugin, self).__init__(name='system')
18
19     def commands(self):
20         return [ListDirectoryCommand(), GetWorkingDirectoryCommand(),
21                 ChangeDirectoryCommand(), SystemCommand()]
22
23
24 class ListDirectoryCommand (Command):
25     """List the files in a directory.
26     """
27     def __init__(self):
28         super(ListDirectoryCommand, self).__init__(
29             name='ls', aliases=['dir'],
30             arguments=[
31                 Argument(
32     name='path', type='path', default='.',
33     help="""
34 Path to the directory whose contents get listed.  Defaults to the
35 current working directory.
36 """.strip()),
37 ],
38             help=self.__doc__)
39
40     def _run(self, hooke, inqueue, outqueue, params):
41         outqueue.put('\n'.join(sorted(os.listdir(params['path']))))
42
43 class GetWorkingDirectoryCommand (Command):
44     """Get the current working directory.
45     """
46     def __init__(self):
47         super(GetWorkingDirectoryCommand, self).__init__(
48             name='cwd', aliases=['pwd'], help=self.__doc__)
49
50     def _run(self, hooke, inqueue, outqueue, params):
51         outqueue.put(os.getcwd())
52
53 class ChangeDirectoryCommand (Command):
54     """Change the current working directory.
55     """
56     def __init__(self):
57         super(ChangeDirectoryCommand, self).__init__(
58             name='cd',
59             arguments=[
60                 Argument(
61     name='path', type='path', default='~',
62     help="""
63 Path of the directory to change into.  Default to the user's home
64 directory.
65 """.strip()),
66 ],
67             help=self.__doc__)
68
69     def _run(self, hooke, inqueue, outqueue, params):
70         os.chdir(os.path.expanduser(params['path']))
71
72 class SystemCommand (Command):
73     """Execute a system command and report the output.
74     """
75     def __init__(self):
76         super(SystemCommand, self).__init__(
77             name='system',
78             arguments=[
79                 Argument(
80     name='command', type='string', optional=False, count=-1,
81     help="""
82 Command line to execute.
83 """.strip()),
84 ],
85             help=self.__doc__)
86
87     def _run(self, hooke, inqueue, outqueue, params):
88         p = subprocess.Popen(
89             params['command'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
90         stdout,stderr = p.communicate()
91         returncode = p.wait()
92         outqueue.put(stdout)
93         outqueue.put(stderr)