Mercurial > sqlpython
annotate cmd2.py @ 38:ed1f390c142d
all but flags
author | devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil |
---|---|
date | Thu, 03 Jan 2008 13:23:06 -0500 |
parents | 38aa4f49f095 |
children | 5d29e6a21c6f |
rev | line source |
---|---|
36
6315f75c45ae
untested redo of edit
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
35
diff
changeset
|
1 """Variant on standard library's cmd with extra features. |
16
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
2 |
36
6315f75c45ae
untested redo of edit
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
35
diff
changeset
|
3 To use, simply override cmd2.Cmd instead of cmd.Cmd; use precisely as though you |
6315f75c45ae
untested redo of edit
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
35
diff
changeset
|
4 were using the standard library's cmd, while enjoying the extra features. |
6315f75c45ae
untested redo of edit
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
35
diff
changeset
|
5 |
6315f75c45ae
untested redo of edit
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
35
diff
changeset
|
6 Searchable command history (commands: "hi", "li", "run") |
6315f75c45ae
untested redo of edit
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
35
diff
changeset
|
7 Load commands from file, save to file, edit commands in file |
20
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
8 Multi-line commands |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
9 Case-insensitive commands |
36
6315f75c45ae
untested redo of edit
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
35
diff
changeset
|
10 Special-character shortcut commands (beyond cmd's "@" and "!") |
27
ca6f34be3397
working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
26
diff
changeset
|
11 Settable environment parameters |
21
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
12 |
35
e2a5cdba3113
moved from precmd and postcmd into onecmd
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
34
diff
changeset
|
13 todo: |
36
6315f75c45ae
untested redo of edit
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
35
diff
changeset
|
14 edited commands end with "EOF". Hmm. |
35
e2a5cdba3113
moved from precmd and postcmd into onecmd
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
34
diff
changeset
|
15 flags |
27
ca6f34be3397
working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
26
diff
changeset
|
16 > |
20
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
17 """ |
31
5e2f6ec2e383
load so close!
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
30
diff
changeset
|
18 import cmd, re, os, sys |
16
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
19 |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
20 class Cmd(cmd.Cmd): |
17
25e908abf199
multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
16
diff
changeset
|
21 caseInsensitive = True |
25e908abf199
multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
16
diff
changeset
|
22 multilineCommands = [] |
25e908abf199
multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
16
diff
changeset
|
23 continuationPrompt = '> ' |
21
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
24 shortcuts = {'?': 'help', '!': 'shell', '@': 'load'} |
22
221095f3a4af
all load done but a bad chop
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
21
diff
changeset
|
25 excludeFromHistory = '''run r list l history hi ed li eof'''.split() |
21
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
26 defaultExtension = 'txt' |
27
ca6f34be3397
working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
26
diff
changeset
|
27 defaultFileName = 'command.txt' |
31
5e2f6ec2e383
load so close!
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
30
diff
changeset
|
28 editor = os.environ.get('EDITOR') |
5e2f6ec2e383
load so close!
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
30
diff
changeset
|
29 if not editor: |
5e2f6ec2e383
load so close!
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
30
diff
changeset
|
30 if sys.platform[:3] == 'win': |
5e2f6ec2e383
load so close!
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
30
diff
changeset
|
31 editor = 'notepad' |
30
2739250177ed
load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
29
diff
changeset
|
32 settable = ['prompt', 'continuationPrompt', 'defaultFileName', 'editor', 'caseInsensitive'] |
26
bb3fb24b6f5f
settables ironed out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
25
diff
changeset
|
33 terminators = ';\n' |
25
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
34 def do_cmdenvironment(self, args): |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
35 self.stdout.write(""" |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
36 Commands are %(casesensitive)scase-sensitive. |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
37 Commands may be terminated with: %(terminators)s |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
38 Settable parameters: %(settable)s |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
39 """ % |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
40 { 'casesensitive': 'not ' if self.caseInsensitive else '', |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
41 'terminators': ' '.join(self.terminators), |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
42 'settable': ' '.join(self.settable) |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
43 }) |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
44 |
17
25e908abf199
multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
16
diff
changeset
|
45 def __init__(self, *args, **kwargs): |
16
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
46 cmd.Cmd.__init__(self, *args, **kwargs) |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
47 self.history = History() |
17
25e908abf199
multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
16
diff
changeset
|
48 |
35
e2a5cdba3113
moved from precmd and postcmd into onecmd
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
34
diff
changeset
|
49 def onecmd(self, line): |
e2a5cdba3113
moved from precmd and postcmd into onecmd
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
34
diff
changeset
|
50 """Interpret the argument as though it had been typed in response |
e2a5cdba3113
moved from precmd and postcmd into onecmd
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
34
diff
changeset
|
51 to the prompt. |
e2a5cdba3113
moved from precmd and postcmd into onecmd
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
34
diff
changeset
|
52 |
e2a5cdba3113
moved from precmd and postcmd into onecmd
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
34
diff
changeset
|
53 This may be overridden, but should not normally need to be; |
e2a5cdba3113
moved from precmd and postcmd into onecmd
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
34
diff
changeset
|
54 see the precmd() and postcmd() methods for useful execution hooks. |
e2a5cdba3113
moved from precmd and postcmd into onecmd
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
34
diff
changeset
|
55 The return value is a flag indicating whether interpretation of |
e2a5cdba3113
moved from precmd and postcmd into onecmd
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
34
diff
changeset
|
56 commands by the interpreter should stop. |
e2a5cdba3113
moved from precmd and postcmd into onecmd
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
34
diff
changeset
|
57 |
e2a5cdba3113
moved from precmd and postcmd into onecmd
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
34
diff
changeset
|
58 """ |
17
25e908abf199
multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
16
diff
changeset
|
59 try: |
25e908abf199
multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
16
diff
changeset
|
60 (command, args) = line.split(None,1) |
25e908abf199
multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
16
diff
changeset
|
61 except ValueError: |
25e908abf199
multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
16
diff
changeset
|
62 (command, args) = line, '' |
25e908abf199
multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
16
diff
changeset
|
63 if self.caseInsensitive: |
25e908abf199
multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
16
diff
changeset
|
64 command = command.lower() |
25e908abf199
multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
16
diff
changeset
|
65 statement = ' '.join([command, args]) |
35
e2a5cdba3113
moved from precmd and postcmd into onecmd
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
34
diff
changeset
|
66 if command in self.multilineCommands: |
e2a5cdba3113
moved from precmd and postcmd into onecmd
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
34
diff
changeset
|
67 statement = self.finishStatement(statement) |
e2a5cdba3113
moved from precmd and postcmd into onecmd
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
34
diff
changeset
|
68 stop = cmd.Cmd.onecmd(self, statement) |
16
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
69 try: |
35
e2a5cdba3113
moved from precmd and postcmd into onecmd
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
34
diff
changeset
|
70 command = statement.split(None,1)[0].lower() |
16
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
71 if command not in self.excludeFromHistory: |
35
e2a5cdba3113
moved from precmd and postcmd into onecmd
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
34
diff
changeset
|
72 self.history.append(statement) |
16
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
73 finally: |
35
e2a5cdba3113
moved from precmd and postcmd into onecmd
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
34
diff
changeset
|
74 return stop |
16
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
75 |
17
25e908abf199
multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
16
diff
changeset
|
76 def finishStatement(self, firstline): |
25e908abf199
multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
16
diff
changeset
|
77 statement = firstline |
25e908abf199
multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
16
diff
changeset
|
78 while not self.statementHasEnded(statement): |
30
2739250177ed
load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
29
diff
changeset
|
79 inp = self.pseudo_raw_input(self.continuationPrompt) |
2739250177ed
load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
29
diff
changeset
|
80 statement = '%s\n%s' % (statement, inp) |
17
25e908abf199
multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
16
diff
changeset
|
81 return statement |
25e908abf199
multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
16
diff
changeset
|
82 # assembling a list of lines and joining them at the end would be faster, |
25e908abf199
multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
16
diff
changeset
|
83 # but statementHasEnded needs a string arg; anyway, we're getting |
25e908abf199
multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
16
diff
changeset
|
84 # user input and users are slow. |
25e908abf199
multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
16
diff
changeset
|
85 |
21
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
86 def pseudo_raw_input(self, prompt): |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
87 """copied from cmd's cmdloop; like raw_input, but accounts for changed stdin, stdout""" |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
88 |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
89 if self.use_rawinput: |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
90 try: |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
91 line = raw_input(prompt) |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
92 except EOFError: |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
93 line = 'EOF' |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
94 else: |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
95 self.stdout.write(prompt) |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
96 self.stdout.flush() |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
97 line = self.stdin.readline() |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
98 if not len(line): |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
99 line = 'EOF' |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
100 else: |
32
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
101 if line[-1] == '\n': # this was always true in Cmd |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
102 line = line[:-1] |
21
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
103 return line |
32
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
104 |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
105 def cmdloop(self, intro=None): |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
106 """Repeatedly issue a prompt, accept input, parse an initial prefix |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
107 off the received input, and dispatch to action methods, passing them |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
108 the remainder of the line as argument. |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
109 """ |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
110 |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
111 # An almost perfect copy from Cmd; however, the pseudo_raw_input portion |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
112 # has been split out so that it can be called separately |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
113 |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
114 self.preloop() |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
115 if self.use_rawinput and self.completekey: |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
116 try: |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
117 import readline |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
118 self.old_completer = readline.get_completer() |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
119 readline.set_completer(self.complete) |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
120 readline.parse_and_bind(self.completekey+": complete") |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
121 except ImportError: |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
122 pass |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
123 try: |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
124 if intro is not None: |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
125 self.intro = intro |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
126 if self.intro: |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
127 self.stdout.write(str(self.intro)+"\n") |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
128 stop = None |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
129 while not stop: |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
130 if self.cmdqueue: |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
131 line = self.cmdqueue.pop(0) |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
132 else: |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
133 line = self.pseudo_raw_input(self.prompt) |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
134 line = self.precmd(line) |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
135 stop = self.onecmd(line) |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
136 stop = self.postcmd(stop, line) |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
137 self.postloop() |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
138 finally: |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
139 if self.use_rawinput and self.completekey: |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
140 try: |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
141 import readline |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
142 readline.set_completer(self.old_completer) |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
143 except ImportError: |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
144 pass |
ebefe2d57e3b
multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
31
diff
changeset
|
145 |
22
221095f3a4af
all load done but a bad chop
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
21
diff
changeset
|
146 def do_EOF(self, arg): |
221095f3a4af
all load done but a bad chop
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
21
diff
changeset
|
147 return True |
221095f3a4af
all load done but a bad chop
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
21
diff
changeset
|
148 do_eof = do_EOF |
221095f3a4af
all load done but a bad chop
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
21
diff
changeset
|
149 |
33
061f40299ed5
ed now perfect except for eof
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
32
diff
changeset
|
150 statementEndPattern = re.compile(r'[%s]\s*$' % terminators) |
17
25e908abf199
multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
16
diff
changeset
|
151 def statementHasEnded(self, lines): |
33
061f40299ed5
ed now perfect except for eof
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
32
diff
changeset
|
152 return bool(self.statementEndPattern.search(lines)) \ |
061f40299ed5
ed now perfect except for eof
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
32
diff
changeset
|
153 or lines[-3:] == 'EOF' |
19
25f22e742a68
statement ends fixed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
18
diff
changeset
|
154 |
25
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
155 def clean(self, s): |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
156 """cleans up a string""" |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
157 if self.caseInsensitive: |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
158 return s.strip().lower() |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
159 return s.strip() |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
160 |
20
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
161 def parseline(self, line): |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
162 """Parse the line into a command name and a string containing |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
163 the arguments. Returns a tuple containing (command, args, line). |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
164 'command' and 'args' may be None if the line couldn't be parsed. |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
165 """ |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
166 line = line.strip() |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
167 if not line: |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
168 return None, None, line |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
169 shortcut = self.shortcuts.get(line[0]) |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
170 if shortcut and hasattr(self, 'do_%s' % shortcut): |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
171 line = '%s %s' % (shortcut, line[1:]) |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
172 i, n = 0, len(line) |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
173 while i < n and line[i] in self.identchars: i = i+1 |
26
bb3fb24b6f5f
settables ironed out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
25
diff
changeset
|
174 cmd, arg = line[:i], line[i:].strip().strip(self.terminators) |
20
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
175 return cmd, arg, line |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
176 |
25
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
177 def showParam(self, param): |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
178 param = self.clean(param) |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
179 if param in self.settable: |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
180 val = getattr(self, param) |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
181 self.stdout.write('%s: %s\n' % (param, str(getattr(self, param)))) |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
182 |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
183 def do_show(self, arg): |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
184 'Shows value of a parameter' |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
185 if arg.strip(): |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
186 self.showParam(arg) |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
187 else: |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
188 for param in self.settable: |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
189 self.showParam(param) |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
190 |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
191 def do_set(self, arg): |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
192 'Sets a parameter' |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
193 try: |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
194 paramName, val = arg.split(None, 1) |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
195 paramName = self.clean(paramName) |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
196 if paramName not in self.settable: |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
197 raise NotSettableError |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
198 currentVal = getattr(self, paramName) |
26
bb3fb24b6f5f
settables ironed out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
25
diff
changeset
|
199 val = cast(currentVal, val.strip(self.terminators)) |
25
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
200 setattr(self, paramName, val) |
26
bb3fb24b6f5f
settables ironed out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
25
diff
changeset
|
201 self.stdout.write('%s - was: %s\nnow: %s\n' % (paramName, currentVal, val)) |
30
2739250177ed
load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
29
diff
changeset
|
202 except (ValueError, AttributeError, NotSettableError), e: |
25
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
203 self.do_show(arg) |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
204 |
20
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
205 def do_shell(self, arg): |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
206 'execute a command as if at the OS prompt.' |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
207 os.system(arg) |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
208 |
16
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
209 def do_history(self, arg): |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
210 """history [arg]: lists past commands issued |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
211 |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
212 no arg -> list all |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
213 arg is integer -> list one history item, by index |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
214 arg is string -> string search |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
215 arg is /enclosed in forward-slashes/ -> regular expression search |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
216 """ |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
217 if arg: |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
218 history = self.history.get(arg) |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
219 else: |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
220 history = self.history |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
221 for hi in history: |
21
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
222 self.stdout.write(hi.pr()) |
16
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
223 def last_matching(self, arg): |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
224 try: |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
225 if arg: |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
226 return self.history.get(arg)[-1] |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
227 else: |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
228 return self.history[-1] |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
229 except: |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
230 return None |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
231 def do_list(self, arg): |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
232 """list [arg]: lists last command issued |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
233 |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
234 no arg -> list absolute last |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
235 arg is integer -> list one history item, by index |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
236 - arg, arg - (integer) -> list up to or after #arg |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
237 arg is string -> list last command matching string search |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
238 arg is /enclosed in forward-slashes/ -> regular expression search |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
239 """ |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
240 try: |
21
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
241 self.stdout.write(self.last_matching(arg).pr()) |
16
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
242 except: |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
243 pass |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
244 do_hi = do_history |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
245 do_l = do_list |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
246 do_li = do_list |
21
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
247 |
27
ca6f34be3397
working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
26
diff
changeset
|
248 def do_ed(self, arg): |
36
6315f75c45ae
untested redo of edit
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
35
diff
changeset
|
249 """ed: edit most recent command in text editor |
6315f75c45ae
untested redo of edit
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
35
diff
changeset
|
250 ed [N]: edit numbered command from history |
6315f75c45ae
untested redo of edit
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
35
diff
changeset
|
251 ed [filename]: edit specified file name |
6315f75c45ae
untested redo of edit
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
35
diff
changeset
|
252 |
6315f75c45ae
untested redo of edit
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
35
diff
changeset
|
253 commands are run after editor is closed.""" |
27
ca6f34be3397
working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
26
diff
changeset
|
254 if not self.editor: |
30
2739250177ed
load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
29
diff
changeset
|
255 print "please use 'set editor' to specify your text editing program of choice." |
27
ca6f34be3397
working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
26
diff
changeset
|
256 return |
36
6315f75c45ae
untested redo of edit
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
35
diff
changeset
|
257 filename = self.defaultFileName |
37
38aa4f49f095
untested redo of edit
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
36
diff
changeset
|
258 buffer = '' |
36
6315f75c45ae
untested redo of edit
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
35
diff
changeset
|
259 try: |
6315f75c45ae
untested redo of edit
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
35
diff
changeset
|
260 arg = int(arg) |
6315f75c45ae
untested redo of edit
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
35
diff
changeset
|
261 buffer = self.last_matching(arg) |
6315f75c45ae
untested redo of edit
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
35
diff
changeset
|
262 except: |
6315f75c45ae
untested redo of edit
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
35
diff
changeset
|
263 if arg: |
6315f75c45ae
untested redo of edit
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
35
diff
changeset
|
264 filename = arg |
6315f75c45ae
untested redo of edit
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
35
diff
changeset
|
265 else: |
6315f75c45ae
untested redo of edit
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
35
diff
changeset
|
266 buffer = self.last_matching(arg) |
6315f75c45ae
untested redo of edit
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
35
diff
changeset
|
267 |
37
38aa4f49f095
untested redo of edit
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
36
diff
changeset
|
268 if buffer: |
38aa4f49f095
untested redo of edit
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
36
diff
changeset
|
269 f = open(filename, 'w') |
38aa4f49f095
untested redo of edit
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
36
diff
changeset
|
270 f.write(buffer or '') |
38aa4f49f095
untested redo of edit
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
36
diff
changeset
|
271 f.close() |
38aa4f49f095
untested redo of edit
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
36
diff
changeset
|
272 |
36
6315f75c45ae
untested redo of edit
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
35
diff
changeset
|
273 os.system('%s %s' % (self.editor, filename)) |
38
ed1f390c142d
all but flags
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
37
diff
changeset
|
274 self.do_load(filename) |
27
ca6f34be3397
working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
26
diff
changeset
|
275 do_edit = do_ed |
ca6f34be3397
working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
26
diff
changeset
|
276 |
ca6f34be3397
working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
26
diff
changeset
|
277 def do_save(self, fname=None): |
ca6f34be3397
working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
26
diff
changeset
|
278 """Saves most recent command to a file.""" |
ca6f34be3397
working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
26
diff
changeset
|
279 |
ca6f34be3397
working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
26
diff
changeset
|
280 if fname is None: |
ca6f34be3397
working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
26
diff
changeset
|
281 fname = self.defaultFileName |
ca6f34be3397
working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
26
diff
changeset
|
282 try: |
ca6f34be3397
working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
26
diff
changeset
|
283 f = open(fname, 'w') |
ca6f34be3397
working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
26
diff
changeset
|
284 f.write(self.history[-1]) |
ca6f34be3397
working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
26
diff
changeset
|
285 f.close() |
ca6f34be3397
working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
26
diff
changeset
|
286 except Exception, e: |
ca6f34be3397
working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
26
diff
changeset
|
287 print 'Error saving %s: %s' % (fname, str(e)) |
ca6f34be3397
working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
26
diff
changeset
|
288 |
ca6f34be3397
working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
26
diff
changeset
|
289 def do_load(self, fname=None): |
21
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
290 """Runs command(s) from a file.""" |
27
ca6f34be3397
working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
26
diff
changeset
|
291 if fname is None: |
ca6f34be3397
working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
26
diff
changeset
|
292 fname = self.defaultFileName |
24
7a89805a47b1
load cleaned up
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
23
diff
changeset
|
293 keepstate = Statekeeper(self, ('stdin','use_rawinput','prompt','continuationPrompt')) |
21
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
294 try: |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
295 self.stdin = open(fname, 'r') |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
296 except IOError, e: |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
297 try: |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
298 self.stdin = open('%s.%s' % (fname, self.defaultExtension), 'r') |
29
a6f68b25ae16
load is perfect
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
27
diff
changeset
|
299 except IOError: |
21
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
300 print 'Problem opening file %s: \n%s' % (fname, e) |
29
a6f68b25ae16
load is perfect
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
27
diff
changeset
|
301 keepstate.restore() |
a6f68b25ae16
load is perfect
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
27
diff
changeset
|
302 return |
a6f68b25ae16
load is perfect
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
27
diff
changeset
|
303 self.use_rawinput = False |
a6f68b25ae16
load is perfect
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
27
diff
changeset
|
304 self.prompt = self.continuationPrompt = '' |
a6f68b25ae16
load is perfect
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
27
diff
changeset
|
305 self.cmdloop() |
a6f68b25ae16
load is perfect
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
27
diff
changeset
|
306 self.stdin.close() |
a6f68b25ae16
load is perfect
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
27
diff
changeset
|
307 keepstate.restore() |
a6f68b25ae16
load is perfect
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
27
diff
changeset
|
308 self.lastcmd = '' |
34 | 309 |
310 def do_run(self, arg): | |
311 """run [arg]: re-runs an earlier command | |
312 | |
313 no arg -> run most recent command | |
314 arg is integer -> run one history item, by index | |
315 arg is string -> run most recent command by string search | |
316 arg is /enclosed in forward-slashes/ -> run most recent by regex | |
317 """ | |
318 'run [N]: runs the SQL that was run N commands ago' | |
319 runme = self.last_matching(arg) | |
320 print runme | |
321 if runme: | |
322 runme = self.precmd(runme) | |
323 stop = self.onecmd(runme) | |
324 stop = self.postcmd(stop, runme) | |
325 do_r = do_run | |
24
7a89805a47b1
load cleaned up
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
23
diff
changeset
|
326 |
20
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
327 class HistoryItem(str): |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
328 def __init__(self, instr): |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
329 str.__init__(self, instr) |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
330 self.lowercase = self.lower() |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
331 self.idx = None |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
332 def pr(self): |
21
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
333 return '-------------------------[%d]\n%s\n' % (self.idx, str(self)) |
20
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
334 |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
335 class History(list): |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
336 rangeFrom = re.compile(r'^([\d])+\s*\-$') |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
337 def append(self, new): |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
338 new = HistoryItem(new) |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
339 list.append(self, new) |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
340 new.idx = len(self) |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
341 def extend(self, new): |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
342 for n in new: |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
343 self.append(n) |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
344 def get(self, getme): |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
345 try: |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
346 getme = int(getme) |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
347 if getme < 0: |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
348 return self[:(-1 * getme)] |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
349 else: |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
350 return [self[getme-1]] |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
351 except IndexError: |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
352 return [] |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
353 except (ValueError, TypeError): |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
354 getme = getme.strip() |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
355 mtch = self.rangeFrom.search(getme) |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
356 if mtch: |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
357 return self[(int(mtch.group(1))-1):] |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
358 if getme.startswith(r'/') and getme.endswith(r'/'): |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
359 finder = re.compile(getme[1:-1], re.DOTALL | re.MULTILINE | re.IGNORECASE) |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
360 def isin(hi): |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
361 return finder.search(hi) |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
362 else: |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
363 def isin(hi): |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
364 return (getme.lower() in hi.lowercase) |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
365 return [itm for itm in self if isin(itm)] |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
366 |
25
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
367 class NotSettableError(Exception): |
30
2739250177ed
load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
29
diff
changeset
|
368 pass |
16
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
369 |
26
bb3fb24b6f5f
settables ironed out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
25
diff
changeset
|
370 def cast(current, new): |
bb3fb24b6f5f
settables ironed out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
25
diff
changeset
|
371 """Tries to force a new value into the same type as the current.""" |
bb3fb24b6f5f
settables ironed out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
25
diff
changeset
|
372 typ = type(current) |
bb3fb24b6f5f
settables ironed out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
25
diff
changeset
|
373 if typ == bool: |
30
2739250177ed
load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
29
diff
changeset
|
374 try: |
2739250177ed
load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
29
diff
changeset
|
375 return bool(int(new)) |
2739250177ed
load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
29
diff
changeset
|
376 except ValueError, TypeError: |
2739250177ed
load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
29
diff
changeset
|
377 pass |
26
bb3fb24b6f5f
settables ironed out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
25
diff
changeset
|
378 try: |
30
2739250177ed
load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
29
diff
changeset
|
379 new = new.lower() |
2739250177ed
load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
29
diff
changeset
|
380 except: |
2739250177ed
load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
29
diff
changeset
|
381 pass |
2739250177ed
load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
29
diff
changeset
|
382 if (new=='on') or (new[0] in ('y','t')): |
2739250177ed
load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
29
diff
changeset
|
383 return True |
2739250177ed
load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
29
diff
changeset
|
384 if (new=='off') or (new[0] in ('n','f')): |
26
bb3fb24b6f5f
settables ironed out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
25
diff
changeset
|
385 return False |
30
2739250177ed
load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
29
diff
changeset
|
386 else: |
2739250177ed
load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
29
diff
changeset
|
387 try: |
2739250177ed
load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
29
diff
changeset
|
388 return typ(new) |
2739250177ed
load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
29
diff
changeset
|
389 except: |
26
bb3fb24b6f5f
settables ironed out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
25
diff
changeset
|
390 pass |
30
2739250177ed
load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
29
diff
changeset
|
391 print "Problem setting parameter (now %s) to %s; incorrect type?" % (current, new) |
2739250177ed
load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
29
diff
changeset
|
392 return current |
26
bb3fb24b6f5f
settables ironed out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
25
diff
changeset
|
393 |
25
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
394 class Statekeeper(object): |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
395 def __init__(self, obj, attribs): |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
396 self.obj = obj |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
397 self.attribs = attribs |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
398 self.save() |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
399 def save(self): |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
400 for attrib in self.attribs: |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
401 setattr(self, attrib, getattr(self.obj, attrib)) |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
402 def restore(self): |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
403 for attrib in self.attribs: |
c99853267a44
introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
24
diff
changeset
|
404 setattr(self.obj, attrib, getattr(self, attrib)) |