00001 import os
00002 import fnmatch
00003 import inspect
00004 import traceback
00005
00006 from cterm.cterm_const import *
00007
00008 import clnch_misc
00009 import clnch_threadutil
00010 import clnch_native
00011
00012
00013 def executeCommand( commandline, func, args, mod, history_item, quit ):
00014
00015
00016 argspec = inspect.getargspec(func)
00017 if inspect.ismethod(func):
00018 num_args = len(argspec[0])-1
00019 else:
00020 num_args = len(argspec[0])
00021
00022 try:
00023 if num_args==0:
00024 result = func()
00025 elif num_args==1:
00026 result = func(args)
00027 elif num_args==2:
00028 result = func(args,mod)
00029 else:
00030 raise TypeError("arg spec is not acceptable.")
00031 except Exception, e:
00032 print e
00033 return
00034
00035 if isinstance(result,unicode):
00036 commandline.setText(result)
00037 commandline.updateWindowWidth(result)
00038 commandline.setSelection( [ 0, len(result) ] )
00039 commandline.paint()
00040 return
00041
00042 if history_item!=None:
00043 commandline.appendHistory( history_item )
00044
00045 if quit:
00046 commandline.closeList()
00047 commandline.setText(u"")
00048 commandline.selectAll()
00049 commandline.quit()
00050
00051
00052 class commandline_Launcher:
00053
00054 def __init__( self, main_window ):
00055 self.main_window = main_window
00056 self.command_list = []
00057
00058 def onCandidate( self, update_info ):
00059
00060 basedir = "."
00061
00062 left = update_info.text[ : update_info.selection[0] ]
00063 left_lower = left.lower()
00064 pos_arg = left.rfind(";")+1
00065 arg = left[ pos_arg : ]
00066 pos_dir = max( arg.rfind("/")+1, arg.rfind("\\")+1 )
00067 directory = arg[:pos_dir]
00068 directory_lower = directory.lower()
00069 name_prefix = arg[pos_dir:].lower()
00070
00071 dirname_list = []
00072 filename_list = []
00073 candidate_list = []
00074
00075 if len(directory)>0:
00076
00077 try:
00078 path = clnch_misc.joinPath( basedir, directory )
00079 unc = os.path.splitunc(path)
00080 if unc[0]:
00081 clnch_misc.checkNetConnection(path)
00082 if unc[0] and not unc[1]:
00083 servername = unc[0].replace('/','\\')
00084 infolist = clnch_native.enumShare(servername)
00085 for info in infolist:
00086 if info[1]==0:
00087 if info[0].lower().startswith(name_prefix):
00088 if clnch_misc.use_slash:
00089 dirname_list.append( info[0] + u"/" )
00090 else:
00091 dirname_list.append( info[0] + u"\\" )
00092 else:
00093 infolist = clnch_native.findFile( clnch_misc.joinPath(path,'*'), use_cache=True )
00094 for info in infolist:
00095 if info[0].lower().startswith(name_prefix):
00096 if info[3] & clnch_misc.FILE_ATTRIBUTE_DIRECTORY:
00097 if clnch_misc.use_slash:
00098 dirname_list.append( info[0] + u"/" )
00099 else:
00100 dirname_list.append( info[0] + u"\\" )
00101 else:
00102 filename_list.append( info[0] )
00103 except:
00104 pass
00105
00106 for item in self.command_list:
00107 item_lower = item[0].lower()
00108 if item_lower.startswith(left_lower) and len(item_lower)!=len(left_lower):
00109 candidate_list.append( item[0] )
00110
00111 return dirname_list + filename_list + candidate_list
00112
00113 def onEnter( self, commandline, text, mod ):
00114
00115 args = text.split(';')
00116
00117 command_name = args[0].lower()
00118
00119 for command in self.command_list:
00120 if command[0].lower() == command_name:
00121 executeCommand( commandline, command[1], args[1:], mod, text, quit=True )
00122 return True
00123
00124 return False
00125
00126 def onStatusString( self, text ):
00127 return None
00128
00129
00130 class commandline_ExecuteFile:
00131
00132 def __init__( self, main_window ):
00133 self.main_window = main_window
00134
00135 def onCandidate( self, update_info ):
00136 return []
00137
00138 def onEnter( self, commandline, text, mod ):
00139
00140 args = text.split(';')
00141
00142 file = args[0]
00143
00144 if not os.path.exists(file):
00145 return False
00146
00147 for association in self.main_window.association_list:
00148 for pattern in association[0].split():
00149 if fnmatch.fnmatch( file, pattern ):
00150 executeCommand( commandline, association[1], args, mod, text, quit=True )
00151 return
00152
00153 joint_args = clnch_misc.joinArgs(args[1:])
00154
00155 directory, tmp = clnch_misc.splitPath(file)
00156
00157
00158
00159
00160 def jobShellExecute( job_item ):
00161 clnch_misc.shellExecute( None, None, file, joint_args, directory )
00162
00163 def jobShellExecuteFinished( job_item ):
00164 pass
00165
00166 job_item = clnch_threadutil.JobItem( jobShellExecute, jobShellExecuteFinished )
00167 clnch_threadutil.job_queue.enqueue(job_item)
00168
00169 commandline.closeList()
00170 commandline.setText(u"")
00171 commandline.selectAll()
00172
00173 commandline.appendHistory( text )
00174
00175 commandline.quit()
00176
00177 return True
00178
00179 def onStatusString( self, text ):
00180 return None
00181
00182
00183 class commandline_ExecuteURL:
00184
00185 def __init__( self, main_window ):
00186 self.main_window = main_window
00187
00188 def onCandidate( self, update_info ):
00189 return []
00190
00191 def onEnter( self, commandline, text, mod ):
00192
00193 if not (text.lower().startswith("http:") or text.lower().startswith("https:")):
00194 return False
00195
00196
00197
00198 def jobShellExecute( job_item ):
00199 clnch_misc.shellExecute( None, None, text, u"", u"" )
00200
00201 def jobShellExecuteFinished( job_item ):
00202 pass
00203
00204 job_item = clnch_threadutil.JobItem( jobShellExecute, jobShellExecuteFinished )
00205 clnch_threadutil.job_queue.enqueue(job_item)
00206
00207 commandline.closeList()
00208 commandline.setText(u"")
00209 commandline.selectAll()
00210
00211 commandline.appendHistory( text )
00212
00213 commandline.quit()
00214
00215 return True
00216
00217 def onStatusString( self, text ):
00218 return None
00219
00220
00221 class commandline_Calculator:
00222
00223 def __init__( self, main_window ):
00224 self.main_window = main_window
00225
00226 def onCandidate( self, update_info ):
00227 return []
00228
00229 def onEnter( self, commandline, text, mod ):
00230
00231 from math import sin, cos, tan, acos, asin, atan
00232 from math import e, fabs, log, log10, pi, pow, sqrt
00233
00234 try:
00235 result = eval(text)
00236 except:
00237 return False
00238
00239 if isinstance(result,int):
00240
00241 if mod & MODKEY_CTRL:
00242 result_string = u"0x%x" % result
00243 else :
00244 result_string = u"%d" % result
00245
00246 elif isinstance(result,float):
00247 result_string = u"%f" % result
00248
00249 else:
00250 return False
00251
00252 if result_string!=text:
00253
00254 print "%s => %s" % ( text, result_string )
00255 commandline.closeList()
00256 commandline.setText( result_string )
00257 commandline.selectAll()
00258
00259 commandline.appendHistory( text )
00260
00261 return True
00262
00263 def onStatusString( self, text ):
00264 return None