Mercurial > python-cmd2
comparison cmd2.py @ 23:37bff80ef816
refactoring, no failure mode yet
author | catherine@localhost |
---|---|
date | Fri, 16 May 2008 17:17:26 -0400 |
parents | 354a6b713d36 |
children | f9196cf5ef51 |
comparison
equal
deleted
inserted
replaced
22:354a6b713d36 | 23:37bff80ef816 |
---|---|
16 | 16 |
17 CHANGES: | 17 CHANGES: |
18 As of 0.3.0, options should be specified as `optparse` options. See README.txt. | 18 As of 0.3.0, options should be specified as `optparse` options. See README.txt. |
19 flagReader.py options are still supported for backward compatibility | 19 flagReader.py options are still supported for backward compatibility |
20 """ | 20 """ |
21 import cmd, re, os, sys, optparse, subprocess | 21 import cmd, re, os, sys, optparse, subprocess, tempfile |
22 from optparse import make_option | 22 from optparse import make_option |
23 | 23 |
24 class OptionParser(optparse.OptionParser): | 24 class OptionParser(optparse.OptionParser): |
25 def exit(self, status=0, msg=None): | 25 def exit(self, status=0, msg=None): |
26 if msg: | 26 if msg: |
73 if os.system('which %s' % (editor)): | 73 if os.system('which %s' % (editor)): |
74 break | 74 break |
75 | 75 |
76 settable = ['prompt', 'continuationPrompt', 'defaultFileName', 'editor', 'caseInsensitive'] | 76 settable = ['prompt', 'continuationPrompt', 'defaultFileName', 'editor', 'caseInsensitive'] |
77 terminators = ';\n' | 77 terminators = ';\n' |
78 _TO_PASTE_BUFFER = 1 | |
78 def do_cmdenvironment(self, args): | 79 def do_cmdenvironment(self, args): |
79 self.stdout.write(""" | 80 self.stdout.write(""" |
80 Commands are %(casesensitive)scase-sensitive. | 81 Commands are %(casesensitive)scase-sensitive. |
81 Commands may be terminated with: %(terminators)s | 82 Commands may be terminated with: %(terminators)s |
82 Settable parameters: %(settable)s | 83 Settable parameters: %(settable)s |
114 (newStatement, redirect) = (symbol.join(parts[:-1]), parts[-1].strip()) | 115 (newStatement, redirect) = (symbol.join(parts[:-1]), parts[-1].strip()) |
115 if redirect: | 116 if redirect: |
116 if not self.legalFileName.search(redirect): | 117 if not self.legalFileName.search(redirect): |
117 return statement, None | 118 return statement, None |
118 else: | 119 else: |
119 redirect = 1 | 120 redirect = self._TO_PASTE_BUFFER |
120 return newStatement, redirect | 121 return newStatement, redirect |
121 | 122 |
122 def extractCommand(self, statement): | 123 def extractCommand(self, statement): |
123 try: | 124 try: |
124 (command, args) = statement.split(None,1) | 125 (command, args) = statement.split(None,1) |
139 newStatement, redirect = self.parseRedirector(statement, '<', mustBeTerminated) | 140 newStatement, redirect = self.parseRedirector(statement, '<', mustBeTerminated) |
140 if redirect: | 141 if redirect: |
141 return newStatement, redirect, 'r' | 142 return newStatement, redirect, 'r' |
142 return statement, '', '' | 143 return statement, '', '' |
143 | 144 |
145 def pasteBufferProcess(self, mode='read'): | |
146 if mode == 'read': | |
147 command = 'xclip -o -sel clip' | |
148 else: | |
149 command = 'xclip -sel clip' | |
150 return subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) | |
151 | |
144 def onecmd(self, line): | 152 def onecmd(self, line): |
145 """Interpret the argument as though it had been typed in response | 153 """Interpret the argument as though it had been typed in response |
146 to the prompt. | 154 to the prompt. |
147 | 155 |
148 This may be overridden, but should not normally need to be; | 156 This may be overridden, but should not normally need to be; |
155 statement = ' '.join([command, args]) | 163 statement = ' '.join([command, args]) |
156 if command in self.multilineCommands: | 164 if command in self.multilineCommands: |
157 statement = self.finishStatement(statement) | 165 statement = self.finishStatement(statement) |
158 statekeeper = None | 166 statekeeper = None |
159 statement, redirect, mode = self.parseRedirectors(statement) | 167 statement, redirect, mode = self.parseRedirectors(statement) |
160 if redirect == 1: | 168 if redirect == self._TO_PASTE_BUFFER: |
161 if mode in ('a', 'r'): | 169 if mode in ('a', 'r'): |
162 clipcontents = subprocess.Popen('xclip -o -sel clip', shell=True, stdout=subprocess.PIPE).stdout.read() | 170 clipcontents = self.pasteBufferProcess('read').stdout.read() |
163 if mode in ('w', 'a'): | 171 if mode in ('w', 'a'): |
164 statekeeper = Statekeeper(self, ('stdout',)) | 172 statekeeper = Statekeeper(self, ('stdout',)) |
165 self.stdout = subprocess.Popen('xclip -sel clip', shell=True, stdin=subprocess.PIPE).stdin | 173 self.stdout = self.pasteBufferProcess('write').stdin |
166 if mode == 'a': | 174 if mode == 'a': |
167 self.stdout.write(clipcontents + '\n') | 175 self.stdout.write(clipcontents) |
168 else: | 176 else: |
169 statement = '%s %s' % (statement, clipcontents) | 177 statement = '%s %s' % (statement, clipcontents) |
170 elif redirect: | 178 elif redirect: |
171 if mode in ('w','a'): | 179 if mode in ('w','a'): |
172 statekeeper = Statekeeper(self, ('stdout',)) | 180 statekeeper = Statekeeper(self, ('stdout',)) |