5 Aur Shell is simple shell framework.
7 To use it, write some plugins that Aur Shell would use.
19 from showinfo
import Put
24 class AurShell(cmd
.Cmd
):
25 """Interactive shell framework.
26 To use it, write some modules, becouse by default it's very poor shell.
29 cmd
.Cmd
.__init
__(self
)
30 # use it instead of plain `print`
33 # prefix for method callable by shell
34 self
.cmdprefix
= "do_"
35 self
.prompt
= conf
.shell_prompt
37 self
.commands
= self
.load_plugins(conf
.modules_path
)
39 self
.intro
= "\n\tWelcome to aurShell v%(__version__)s\n" % globals()
40 # load history, create empty file if history doesn't exist
41 if not os
.path
.isfile(conf
.history_file
):
42 open(conf
.history_file
, "w").close()
43 readline
.read_history_file(conf
.history_file
)
44 readline
.set_history_length(conf
.history_length
)
45 # create build_dir if doesn't exist
46 if not os
.path
.isdir(conf
.build_dir
):
47 os
.mkdir(conf
.build_dir
)
49 def load_plugins(self
, modules_path
):
50 """Load all commands modules from modules_path direcotory."""
52 # adding plugins path to sys.path
53 if modules_path
not in sys
.path
:
54 sys
.path
.append(modules_path
)
55 # importing all modules from modules_path
56 for module_name
in os
.listdir(modules_path
):
57 module_path
= os
.path
.abspath(
58 string
.join(modules_path
, module_name
))
59 (module_name
, module_extension
) = os
.path
.splitext(module_name
)
60 # if it shouldn't be load
61 if os
.path
.islink(module_path
) or \
62 module_path
== __file__
or \
63 module_extension
!= ".py":
65 for key
in sys
.modules
.keys():
66 if key
== module_name
:
68 module
= __import__(module_name
)
69 command_modules
.append({"obj": module_name
, "name": module
,})
70 # search for class in each module
72 for module
in command_modules
:
73 for obj
in dir(module
["name"]):
75 # better class serch 2008-01-27 16:56:42
76 if not obj
.startswith("_"):
78 # check if it's method
79 x
= getattr(module
['name'], obj
)(self
.put
, conf
)
80 # create instance for each class
81 # as agrument give it Put() class instance
83 getattr(module
['name'], obj
)(self
.put
, conf
)
88 def precmd(self
, cmd
):
89 # check if alias exists and if so, replace command
90 for alias
in conf
.alias
.keys():
91 # command should start with alias, but:
92 # - next char should be white space, or
93 # - there's no next char
94 if cmd
.startswith(alias
) and \
95 (len(cmd
) <= len(alias
) or cmd
[len(alias
)] == " "):
96 cmd
= cmd
.replace(alias
, conf
.alias
[alias
], 1)
99 def do_shell(self
, cmd
):
100 """System shell command, for commads which starts with !"""
103 def default(self
, cmd
=None):
106 cmd = (<class>, [method], [arg1], [arg2], ...)
108 # cmd[0] should be class name
109 # cmd[1] should be method name (or arugmet if class is callable)
110 # cmd[1] can be empty
111 # cmd[2:] should be method argumments, can be empty
113 # operations for single command with no arguments
115 # if there's no such command (or plugin class)
116 if not cmd
[0] in self
.commands
.keys():
117 self
.put("%s : command not found." % cmd
[0])
118 # for only one argument, try to run __call__() method with
120 elif "__call__" in dir(self
.commands
[cmd
[0]]):
121 getattr(self
.commands
[cmd
[0]], "__call__")()
123 self
.put("%s : bad usege. Try to run help." % cmd
[0])
124 # if command was called with arguments
126 cmd
[1] = self
.cmdprefix
+ cmd
[1]
127 if not cmd
[0] in self
.commands
.keys():
128 self
.put("%s : command not found." % cmd
[0])
129 # if method named arg[1] exist in class arg[0], try to run it
130 elif cmd
[1] in dir(self
.commands
[cmd
[0]]):
131 #print dir(self.commands[cmd[0]])
133 getattr(self
.commands
[cmd
[0]], cmd
[1])(*cmd
[2:])
135 # show __doc__ if exist
136 doc
= getattr(self
.commands
[cmd
[0]], cmd
[1])
138 self
.put(doc
.__doc
__)
140 self
.put("%s : bad usage" % cmd
[1][3:])
141 # if there's no such method arg[1] in class arg[0],
142 # try to run class.__call__(args..)
145 self
.commands
[cmd
[0]](*cmd
[1:])
147 # object is not callable
148 self
.put("%s : bad usage" % cmd
[0])
150 def completenames(self
, text
, *ignored
):
151 """Complete commands"""
152 dotext
= self
.cmdprefix
+ text
155 [a
[3:] + " " for a
in self
.get_names() if a
.startswith(dotext
)]
156 # + all metrods from modules
158 [a
+ " " for a
in self
.commands
.keys() if a
.startswith(text
)]
161 [a
+ " " for a
in self
.conf
.alias
.keys() if a
.startswith(text
)]
162 return local_cmd_list
+ module_cmd_list
+ aliases_cmd_list
164 def completedefault(self
, text
, line
, begidx
, endidx
):
165 """Complete commands argument"""
166 dotext
= self
.cmdprefix
+ text
168 # if only commands was given
170 cmds
= [a
[3:] + " " for a
in dir(self
.commands
[line
[0]]) \
171 if a
.startswith(dotext
)]
173 cmds
= [a
[3:] + " " for a
in dir(self
.commands
[line
[0]]) \
174 if a
.startswith(dotext
)]
175 # else don't complete (or should I?)
180 def do_help(self
, arg
):
181 """Show help for commands"""
184 self
.put("Usage: help <command>")
186 # first - build-in methods
187 if self
.cmdprefix
+ arg
[0] in dir(self
):
188 doc
= getattr(self
, self
.cmdprefix
+ arg
[0]).__doc
__
194 # try to run help() method
195 self
.put(getattr(self
.commands
[arg
[0]], "help")())
196 except AttributeError:
197 # try to show __doc__
198 if self
.commands
[arg
[0]].__doc
__:
199 self
.put(self
.commands
[arg
[0]].__doc
__)
201 self
.put("No help found.")
203 self
.put("No help found.")
207 if arg
[0] in self
.commands
.keys():
208 arg
[1] = self
.cmdprefix
+ arg
[1]
209 doc
= getattr(self
.commands
[arg
[0]], arg
[1]).__doc
__
211 except AttributeError:
212 self
.put("%s : no help found" % arg
[1][3:])
214 self
.put("Try to do something else.\nThis option doesn't work well now.")
216 def do_clear(self
, *ignored
):
217 """Clear the screen"""
218 # TODO 2008-02-09 20:44:50
219 self
.put("Todo, sorry")
221 def do_history(self
, hnumb
=None, *ignored
):
222 """Show the history"""
223 # TODO better history listing
224 # 2008-01-27 18:51:22
225 # print whole history
227 for number
in range(1, conf
.history_length
):
228 cmd
= readline
.get_history_item(number
)
231 self
.put("%6d %s" % (number
, cmd
))
232 # for history range 12-22 or -22 or 22-
236 if hnumb
[-1] == "-" or hnumb
[0] == "-":
237 start
= int(hnumb
.replace("-", " "))
238 end
= conf
.history_length
240 start
, end
= hnumb
.split("-")
243 for number
in range(start
, end
):
244 cmd
= readline
.get_history_item(number
)
247 self
.put("%6d %s" % (number
, cmd
))
250 self
.put(readline
.get_history_item(hnumb
))
252 self
.put("""Bad value.
253 Usage: history <number or range>
254 history 11-20 -> from 11 to 20
255 history 22- -> from 22 fo the end of history file
256 history -22 -> same as 22-""")
258 def do_quit(self
, *ignored
):
259 """Quit from shell"""
260 if conf
.history_length
:
261 readline
.write_history_file(conf
.history_file
)
270 if __name__
== "__main__":
274 except KeyboardInterrupt:
275 # TODO 2008-01-27 16:56:31
276 sys
.exit(shell
.do_quit())