comparison cmd2.py @ 26:82dde2f60e46

catching no-xclip errors finally fixed
author catherine@cordelia
date Sat, 17 May 2008 07:23:15 -0400
parents c8efa4369189 6948d1f9e4b4
children a76798cde34c
comparison
equal deleted inserted replaced
20:c8efa4369189 26:82dde2f60e46
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, 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:
44 optionParser.add_option(opt) 44 optionParser.add_option(opt)
45 optionParser.set_usage("%s [options] arg" % func.__name__.strip('do_')) 45 optionParser.set_usage("%s [options] arg" % func.__name__.strip('do_'))
46 def newFunc(instance, arg): 46 def newFunc(instance, arg):
47 try: 47 try:
48 opts, arg = optionParser.parse_args(arg.split()) 48 opts, arg = optionParser.parse_args(arg.split())
49 arg = ' '.join(arg)
50 except (optparse.OptionValueError, optparse.BadOptionError, 49 except (optparse.OptionValueError, optparse.BadOptionError,
51 optparse.OptionError, optparse.AmbiguousOptionError, 50 optparse.OptionError, optparse.AmbiguousOptionError,
52 optparse.OptionConflictError), e: 51 optparse.OptionConflictError), e:
53 print e 52 print e
54 optionParser.print_help() 53 optionParser.print_help()
71 if not editor: 70 if not editor:
72 if sys.platform[:3] == 'win': 71 if sys.platform[:3] == 'win':
73 editor = 'notepad' 72 editor = 'notepad'
74 else: 73 else:
75 for editor in ['gedit', 'kate', 'vim', 'emacs', 'nano', 'pico']: 74 for editor in ['gedit', 'kate', 'vim', 'emacs', 'nano', 'pico']:
76 if not os.system('which %s' % (editor)): 75 if os.system('which %s' % (editor)):
77 break 76 break
78 77
79 settable = ['prompt', 'continuationPrompt', 'defaultFileName', 'editor', 'caseInsensitive'] 78 settable = ['prompt', 'continuationPrompt', 'defaultFileName', 'editor', 'caseInsensitive']
80 terminators = ';\n' 79 terminators = ';\n'
80 _TO_PASTE_BUFFER = 1
81 def do_cmdenvironment(self, args): 81 def do_cmdenvironment(self, args):
82 self.stdout.write(""" 82 self.stdout.write("""
83 Commands are %(casesensitive)scase-sensitive. 83 Commands are %(casesensitive)scase-sensitive.
84 Commands may be terminated with: %(terminators)s 84 Commands may be terminated with: %(terminators)s
85 Settable parameters: %(settable)s 85 Settable parameters: %(settable)s
113 if (len(parts) < 2): 113 if (len(parts) < 2):
114 return statement, None 114 return statement, None
115 if mustBeTerminated and (parts[-2].strip()[-1] not in self.terminators): 115 if mustBeTerminated and (parts[-2].strip()[-1] not in self.terminators):
116 return statement, None 116 return statement, None
117 (newStatement, redirect) = (symbol.join(parts[:-1]), parts[-1].strip()) 117 (newStatement, redirect) = (symbol.join(parts[:-1]), parts[-1].strip())
118 if not self.legalFileName.search(redirect): 118 if redirect:
119 return statement, None 119 if not self.legalFileName.search(redirect):
120 return statement, None
121 else:
122 redirect = self._TO_PASTE_BUFFER
120 return newStatement, redirect 123 return newStatement, redirect
121 124
122 def extractCommand(self, statement): 125 def extractCommand(self, statement):
123 try: 126 try:
124 (command, args) = statement.split(None,1) 127 (command, args) = statement.split(None,1)
129 return command, args 132 return command, args
130 133
131 def parseRedirectors(self, statement): 134 def parseRedirectors(self, statement):
132 mustBeTerminated = self.extractCommand(statement)[0] in self.multilineCommands 135 mustBeTerminated = self.extractCommand(statement)[0] in self.multilineCommands
133 newStatement, redirect = self.parseRedirector(statement, '>>', mustBeTerminated) 136 newStatement, redirect = self.parseRedirector(statement, '>>', mustBeTerminated)
134 if redirect: 137 if redirect:
135 return newStatement, redirect, 'a' 138 return newStatement, redirect, 'a'
136 newStatement, redirect = self.parseRedirector(statement, '>', mustBeTerminated) 139 newStatement, redirect = self.parseRedirector(statement, '>', mustBeTerminated)
137 if redirect: 140 if redirect:
138 return newStatement, redirect, 'w' 141 return newStatement, redirect, 'w'
139 newStatement, redirect = self.parseRedirector(statement, '<', mustBeTerminated) 142 newStatement, redirect = self.parseRedirector(statement, '<', mustBeTerminated)
140 if redirect: 143 if redirect:
141 return newStatement, redirect, 'r' 144 return newStatement, redirect, 'r'
142 return statement, '', '' 145 return statement, '', ''
143 146
147 def pasteBufferProcess(self, mode='read'):
148 if mode == 'read':
149 command = 'xc1ip -o -sel clip'
150 else:
151 command = 'xclip -sel clip'
152 result = subprocess.check_call(command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
153 return result
154
144 def onecmd(self, line): 155 def onecmd(self, line):
145 """Interpret the argument as though it had been typed in response 156 """Interpret the argument as though it had been typed in response
146 to the prompt. 157 to the prompt.
147 158
148 This may be overridden, but should not normally need to be; 159 This may be overridden, but should not normally need to be;
154 command, args = self.extractCommand(line) 165 command, args = self.extractCommand(line)
155 statement = ' '.join([command, args]) 166 statement = ' '.join([command, args])
156 if command in self.multilineCommands: 167 if command in self.multilineCommands:
157 statement = self.finishStatement(statement) 168 statement = self.finishStatement(statement)
158 statekeeper = None 169 statekeeper = None
170 stop = 0
159 statement, redirect, mode = self.parseRedirectors(statement) 171 statement, redirect, mode = self.parseRedirectors(statement)
160 if redirect: 172 if redirect == self._TO_PASTE_BUFFER:
173 try:
174 if mode in ('a', 'r'):
175 clipcontents = self.pasteBufferProcess('read').stdout.read()
176 if mode in ('w', 'a'):
177 statekeeper = Statekeeper(self, ('stdout',))
178 self.stdout = self.pasteBufferProcess('write').stdin
179 if mode == 'a':
180 self.stdout.write(clipcontents)
181 else:
182 statement = '%s %s' % (statement, clipcontents)
183 except subprocess.CalledProcessError:
184 print """Redirecting to or from paste buffer requires xclip
185 to be installed on operating system.
186 On Debian/Ubuntu, 'sudo apt-get install xclip' will install it."""
187 return 0
188 elif redirect:
161 if mode in ('w','a'): 189 if mode in ('w','a'):
162 statekeeper = Statekeeper(self, ('stdout',)) 190 statekeeper = Statekeeper(self, ('stdout',))
163 self.stdout = open(redirect, mode) 191 self.stdout = open(redirect, mode)
164 else: 192 else:
165 statement = '%s %s' % (statement, self.fileimport(statement=statement, source=redirect)) 193 statement = '%s %s' % (statement, self.fileimport(statement=statement, source=redirect))