Mercurial > python-cmd2
comparison cmd2.py @ 22:354a6b713d36
paste working! (linux)
author | catherine@localhost |
---|---|
date | Fri, 16 May 2008 16:28:25 -0400 |
parents | 24fbec27d66f |
children | 37bff80ef816 |
comparison
equal
deleted
inserted
replaced
21:24fbec27d66f | 22:354a6b713d36 |
---|---|
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 | 21 import cmd, re, os, sys, optparse, subprocess |
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: |
110 if (len(parts) < 2): | 110 if (len(parts) < 2): |
111 return statement, None | 111 return statement, None |
112 if mustBeTerminated and (parts[-2].strip()[-1] not in self.terminators): | 112 if mustBeTerminated and (parts[-2].strip()[-1] not in self.terminators): |
113 return statement, None | 113 return statement, None |
114 (newStatement, redirect) = (symbol.join(parts[:-1]), parts[-1].strip()) | 114 (newStatement, redirect) = (symbol.join(parts[:-1]), parts[-1].strip()) |
115 if not self.legalFileName.search(redirect): | 115 if redirect: |
116 return statement, None | 116 if not self.legalFileName.search(redirect): |
117 return statement, None | |
118 else: | |
119 redirect = 1 | |
117 return newStatement, redirect | 120 return newStatement, redirect |
118 | 121 |
119 def extractCommand(self, statement): | 122 def extractCommand(self, statement): |
120 try: | 123 try: |
121 (command, args) = statement.split(None,1) | 124 (command, args) = statement.split(None,1) |
126 return command, args | 129 return command, args |
127 | 130 |
128 def parseRedirectors(self, statement): | 131 def parseRedirectors(self, statement): |
129 mustBeTerminated = self.extractCommand(statement)[0] in self.multilineCommands | 132 mustBeTerminated = self.extractCommand(statement)[0] in self.multilineCommands |
130 newStatement, redirect = self.parseRedirector(statement, '>>', mustBeTerminated) | 133 newStatement, redirect = self.parseRedirector(statement, '>>', mustBeTerminated) |
131 if redirect: | 134 if redirect: |
132 return newStatement, redirect, 'a' | 135 return newStatement, redirect, 'a' |
133 newStatement, redirect = self.parseRedirector(statement, '>', mustBeTerminated) | 136 newStatement, redirect = self.parseRedirector(statement, '>', mustBeTerminated) |
134 if redirect: | 137 if redirect: |
135 return newStatement, redirect, 'w' | 138 return newStatement, redirect, 'w' |
136 newStatement, redirect = self.parseRedirector(statement, '<', mustBeTerminated) | 139 newStatement, redirect = self.parseRedirector(statement, '<', mustBeTerminated) |
152 statement = ' '.join([command, args]) | 155 statement = ' '.join([command, args]) |
153 if command in self.multilineCommands: | 156 if command in self.multilineCommands: |
154 statement = self.finishStatement(statement) | 157 statement = self.finishStatement(statement) |
155 statekeeper = None | 158 statekeeper = None |
156 statement, redirect, mode = self.parseRedirectors(statement) | 159 statement, redirect, mode = self.parseRedirectors(statement) |
157 if redirect: | 160 if redirect == 1: |
161 if mode in ('a', 'r'): | |
162 clipcontents = subprocess.Popen('xclip -o -sel clip', shell=True, stdout=subprocess.PIPE).stdout.read() | |
163 if mode in ('w', 'a'): | |
164 statekeeper = Statekeeper(self, ('stdout',)) | |
165 self.stdout = subprocess.Popen('xclip -sel clip', shell=True, stdin=subprocess.PIPE).stdin | |
166 if mode == 'a': | |
167 self.stdout.write(clipcontents + '\n') | |
168 else: | |
169 statement = '%s %s' % (statement, clipcontents) | |
170 elif redirect: | |
158 if mode in ('w','a'): | 171 if mode in ('w','a'): |
159 statekeeper = Statekeeper(self, ('stdout',)) | 172 statekeeper = Statekeeper(self, ('stdout',)) |
160 self.stdout = open(redirect, mode) | 173 self.stdout = open(redirect, mode) |
161 else: | 174 else: |
162 statement = '%s %s' % (statement, self.fileimport(statement=statement, source=redirect)) | 175 statement = '%s %s' % (statement, self.fileimport(statement=statement, source=redirect)) |