def __init__(self, **args): debug.trace('ReformatFromRecentWX_NonBlocking.__init__', '** invoked') self.deep_construct(ReformatFromRecentWX_NonBlocking, {}, args ) debug.trace('ReformatFromRecentWX_NonBlocking.__init__', '** upon exit, self.view()=%s, self.view_layer=%s' % (self.view(), self.view_layer))
def __init__(self, **args): debug.trace('ReformatFromRecentWX_NonBlocking.__init__', '** invoked') self.deep_construct(ReformatFromRecentWX_NonBlocking, {}, args) debug.trace( 'ReformatFromRecentWX_NonBlocking.__init__', '** upon exit, self.view()=%s, self.view_layer=%s' % (self.view(), self.view_layer))
def close_window(self): """close the window corresponding to this frame **NOTE:** The owner must call the frame's cleanup method before calling this method to close the actual GUI frame **INPUTS** *none* **OUTPUTS** *none* """ if not self.closing: # this should never happen, but if it does, report the error and make # the best of it msg = 'frame received close_window without prior cleanup\n' debug.critical_warning(msg) # this is diagnostic information associated with the critical warning, # not a trace, so it should always be printed # debug.print_call_stack() self.cleanup() debug.trace('WaxFrameBase.close_window', 'calling self.Close') self.Close()
def get_text(self, start = None, end = None): """retrieves a portion of the buffer from the cache. **INPUTS** *INT start* is the start of the region returned. Defaults to start of buffer. *INT end* is the offset into the buffer of the character following the region to be returned (this matches Python's slice convention). Defaults to end of buffer. **OUTPUTS** *STR* -- contents of specified range of the buffer """ trace('SourceBuffCached.get_text', 'start=%s, end=%s' % (start, end)) text = self._get_cache_element('get_text', self._get_text_from_app) # # Note: cannot invoke self.make_valid_range() because it causes # infinite recursion, ie: # -> get_text() -> make_valid_range() -> len() -> get_text() - # | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ | # if start == None: start = 0 if end == None: end = len(text) if end < start: tmp = end end = start start = tmp return text[start:end]
def _add_new_instance(self, app): """private method called internally to do the work of new_instance, except for notifying the recog_mgr. **INPUTS** *AppState* app -- AppState interface corresponding to the new instance **OUTPUTS** *STR* -- name of the application instance. Necessary if you want to add windows to the application in the future. """ app_name = app.app_name if app_name not in self.app_names(): self.instance_names[app_name] = [] self.past_instances[app_name] = 0 if app_name not in self.title_prefixes.keys(): self.title_prefixes[app_name] = self.unknown_app_prefixes.pop() n = self.past_instances[app_name] title_prefix = self.title_prefixes[app_name] new_name = app_name + "(%d)" % (n) self.past_instances[app_name] = n + 1 debug.trace('AppMgr._add_new_instance', 'adding instance name: %s' % new_name) self.instances[new_name] = app app.set_manager(self) app.set_name(new_name) self.instance_names[app_name].append(new_name) self.instance_data[new_name] = InstanceInfo(new_name, app_name) app.set_instance_string("(%s %d)" % (title_prefix, n)) # print 'new instance ' + new_name + ' app = ' # print repr(app) return new_name
def _get_text_from_app(self, start = None, end = None): """retrieves a portion of the buffer **INPUTS** *INT start* is the start of the region returned. Defaults to start of buffer. *INT end* is the offset into the buffer of the character following the region to be returned (this matches Python's slice convention). Defaults to end of buffer. **OUTPUTS** *STR* -- contents of specified range of the buffer """ trace('SourceBuffMessaging._get_text_from_app', 'start=%s, end=%s' % (start, end)) args = {'start': start, 'end': end, 'buff_name': self.name()} self.app.talk_msgr.send_mess('get_text', args) response = self.app.talk_msgr.get_mess(expect=['get_text_resp']) return response[1]['value']
def _get_text_from_app(self, start=None, end=None): """retrieves a portion of the buffer **INPUTS** *INT start* is the start of the region returned. Defaults to start of buffer. *INT end* is the offset into the buffer of the character following the region to be returned (this matches Python's slice convention). Defaults to end of buffer. **OUTPUTS** *STR* -- contents of specified range of the buffer """ trace('SourceBuffMessaging._get_text_from_app', 'start=%s, end=%s' % (start, end)) args = {'start': start, 'end': end, 'buff_name': self.name()} self.app.talk_msgr.send_mess('get_text', args) response = self.app.talk_msgr.get_mess(expect=['get_text_resp']) return response[1]['value']
def updates_from_app(self, what=None, exclude=1): """Gets a list of updates from the external app. Does this through a messaging protocol. Note: the list of updates must ALWAYS include the name of the external app's active buffer. **INPUTS** [STR] *what* -- List of items to be included/excluded in the updates. BOOL *exclude* -- Indicates if *what* is a list of items to be included or excluded from updates. **OUTPUTS** [ [AS_Update] ] *updates* -- List of updates retrieved from the external app. ..[AS_Update] file:///./AppState.AS_Update.html""" debug.trace('AppStateMessaging.updates_from_app', 'invoked') if what == None: what = [] self.talk_msgr.send_mess('updates') response = self.talk_msgr.get_mess(expect=['updates']) # # Parse response as a list of udate messages # return response[1]['value']
def delete_window(self, window): """clean up and destroy all grammars for a window which has been deleted. **INPUTS** *INT* window -- number identifying the current window displaying the buffer. In Microsoft Windows, this will be the window handle **OUTPUTS** *none* """ debug.trace('WinGramMgr.delete_window', 'window=%s' % window) if self.sel_grammars.has_key(window): self._deactivate_all_window(window) del self.sel_grammars[window] if self.text_mode_toggling: del self.text_mode_toggling_grammars[window] if self.correction: del self.correction_grammars[window] del self.reformatting_grammars[window] if self.dict_grammars.has_key(window): for a_buffer in self.dict_grammars[window].keys(): self.dict_grammars[window][a_buffer].cleanup() self.dict_cmd_grammars[window][a_buffer].cleanup() del self.dict_grammars[window][a_buffer] del self.dict_cmd_grammars[window][a_buffer] del self.dict_grammars[window] del self.dict_cmd_grammars[window]
def new_global_manager(self, editor, instance_name, recog_mgr, exclusive = 1): """creates a new GramMgr using global grammars (regardless of the value of self.global_grammars) **INPUTS** *AppState* editor -- AppState object for which to manage grammars *STR instance_name* -- the name of this AppState instance *RecogStartMgr recog_mgr* -- the RecogStartMgr which owns this grammar manager **OUTPUTS** *none* """ debug.trace('WinGramMgrFactory.new_global_manager', 'new global manager') return WinGramMgr(app = editor, instance_name = instance_name, recog_mgr = recog_mgr, factory = self.gram_factory, global_grammars = 1, exclusive = exclusive, correction = self.correction, text_mode_toggling = self.text_mode_toggling, text_mode_on_spoken_as=self.text_mode_on_spoken_as, text_mode_off_spoken_as=self.text_mode_off_spoken_as, text_mode_off_sets_nat_text_to=self.text_mode_off_sets_nat_text_to)
def connect(user_name, mic_state=None, mic_change_callback = None): """Connects to the SR system. **INPUTS** *STR* name of the Natspeak user to open *STR* mic_state -- *'on'* or *'off'*. State to put the mic in after connection. If *None*, then leave mic alone. *FCT* mic_change_callback -- mic_change_callback(*STR* mic_state) (optional) function to be called when the microphone state changes. **OUTPUTS** *none* -- """ global sr_is_connected, vc_user_name, vc_base_model, vc_base_topic global sr_mic_change_callback if not sr_is_connected: trace('sr_interface.connect', 'mic_state=%s, sr_is_connected=%s' % (mic_state, sr_is_connected)) # trace_call_stack('sr_interface.connect') # 1 means use threads -- needed for GUI apps natlink.natConnect(1) sr_is_connected = 1 openUser(user_name) if mic_state: natlink.setMicState(mic_state) if mic_change_callback: set_change_callback(mic_change_callback)
def spoken_written_form(vocabulary_entry, clean_written = 1, clean_spoken = 1): """Returns the written and spoken forms of a NatSpeak vocabulary entry **INPUTS** *STR* vocabulary_entry -- the vocabulary entry in either written or written\\spoken form. **OUTPUTS** *STR* (spoken, written) -- written and spoken forms of the vocabulary entry. """ a_match = re.match('^([\s\S]*)\\\\([^\\\\]*)$', vocabulary_entry) if a_match: trace('sr_interface.spoken_written_form', 'entry \'%s\' is spoken/written form' % vocabulary_entry) # # Note: need to check for things like {Enter} in written_form # ignore for now # written = a_match.group(1) # the try block handles the special case of selection grammar utterances which # select words with different written and spoken form, which, in # natspeak 7, get reported as written\spoken\t (That's backslash and t, # not a tab character) - DCF # But careful... if the word spoken was "backslash", then the written form is # "\" (at least in NatSpeak 8 and 9). - AD spoken = a_match.group(2) trace('sr_interface.spoken_written_form', 'initial spoken, written = "%s", "%s"' % (spoken, written)) extra = string.find(written, '\\') if extra >= 0 and written != "\\": was_written = written written = was_written[:extra] spoken = was_written[extra+1:] trace('sr_interface.spoken_written_form', 'but extra = %d' % extra) trace('sr_interface.spoken_written_form', 'final spoken, written = "%s", "%s"' % (spoken, written)) else: trace('sr_interface.spoken_written_form', 'entry \'%s\' is just spoken ' % vocabulary_entry ) written = vocabulary_entry spoken = vocabulary_entry # # Substitute special characters in written form (e.g. ' ', '\n') to the # form that the SR expects (e.g. {Spacebar}, {Enter}) # if clean_written: written = clean_written_form(written, clean_for='vc') # # Clean spoken form # if clean_spoken: spoken = clean_spoken_form(spoken) # trace('sr_interface.spoken_written_form', 'spoken=\'%s\', written=\'%s\'' % (spoken, written)) return (spoken, written)
def get_mess(self, expect=None): """Gets a message from the external editor. **NOTE:** In this version, get_mess won't block, but will return None if no message is available. **INPUTS** [STR] *expect* -- If not *None*, then make sure the message's name is listed in *expect*. If not, send an error message. **OUTPUTS** (STR, {STR: STR}) name_argvals_mess -- The message retrieved from external editor in *(mess_name, {arg:val})* format, or None if no message is available.""" trace('get_mess', 'self=%s, expecting %s' % (self, repr(expect))) try: name_argvals_mess = self.receiver.get(block=0) except Queue.Empty: return None if expect != None and (not (name_argvals_mess[0] in expect)): self.wrong_message(name_argvals_mess, expect) if tracing('get_mess.%s' % name_argvals_mess[0]): trace('get_mess.%s' % name_argvals_mess[0], 'got one of %s! It was: %s' \ % (repr(expect), repr(name_argvals_mess))) return name_argvals_mess
def get_packed_mess(self, transporter): """Receive a message packed as a sequence of fixed length chunks. **INPUTS** [MessTransporter] *transporter* -- Transport channel to be used **OUTPUTS** STR *pkd_mess* -- The packed message .. [MessTransporter] file:///./messaging.MessTransporter.html""" trace('messaging.MessPackager_FixedLenSeq.get_packed_mess:', 'invoked') # # Read the fixed length messages until we get one that starts with 1 # pkd_message = '' last_chunk = 0 while not (last_chunk == '1'): a_chunk = transporter.receive_string(self.chunk_len) trace('messaging.MessPackager_FixedLenSeq.get_packed_mess:', 'read a_chunk="%s"' % a_chunk) pkd_message = pkd_message + a_chunk last_chunk = a_chunk[0] return pkd_message
def messarg_is_None(messarg): """Indicates whether or not a message argument looks like the value None. Returns true if the message argument is one of the following: *''* (the string) *'None'* (the string) *'[]'* (the list, not the string... this corresponds to the EmacsLisp *nil* value) **INPUTS** STR *messarg* -- The message argument to be converted. **OUTPUTS** BOOL *answer* -- True iif the message argument can be interpreted as the value None """ if messarg == 'None' or messarg == '' or messarg == []: # # Note: MessEncoder_LenPrefArgs encodes None value as string 'None', # while MessEncoderWDDX encodes it as the empty string, and # EmacsLisp's nil value gets encoded as an empty list. # if tracing('messarg_is_None'): trace('messarg_is_None', 'messarg=%s, returning 1' % messarg) return 1 else: if tracing('messarg_is_None'): trace('messarg_is_None', 'messarg=%s, returning 0' % messarg) return 0
def send_mess(self, mess_name, mess_argvals=None): """Sends a message to the external editor. **INPUTS** STR *mess_name* -- Identifier indicating what kind of message this is. {STR: STR} *mess_argvals* -- Dictionary of arguments and values for the message to be sent to the editor. **OUTPUTS** *none* response -- """ trace_id = 'send_mess.%s' % mess_name if tracing(trace_id): trace(trace_id, 'self=%s, mess_name=\'%s\'' % (self, mess_name)) if mess_argvals == None: tmp_args = {} else: tmp_args = copy.copy(mess_argvals) if tracing(trace_id): trace(trace_id, 'mess_argvals=\'%s\'' % tmp_args) unpkd_mess = self.encoder.encode(mess_name, tmp_args) pkd_mess = self.packager.pack_mess(unpkd_mess) self.packager.send_packed_mess(pkd_mess, self.transporter)
def decode(self, str_mess): """Decodes a message to {arg:val} format. **INPUTS** *STR* str_mess -- The message in raw string format **OUTPUTS** *(STR, {STR: STR}) name_argvals_mess* -- First element is the message name, second element is message arguments in *(name, {arg:val})* format. """ if tracing('messaging.MessEncoderWDDX.decode'): trace('messaging.MessEncoderWDDX.decode', 'decoding str_mess="%s"' % str_mess) mess_argvals = self.unmarshaller.loads(str_mess) # # Name of message is one of the entries in the unmarshalled dictionnary. # Remove it from there. # mess_name = mess_argvals['message_name'] del mess_argvals['message_name'] return (mess_name, mess_argvals)
def _applies(self, app, preceding_symbol=0): if preceding_symbol: last_cont = None last_action = actions_gen.ActionInsert('%dummy%') else: entry = app.get_history(1) trace('ContLastActionWas.applies', 'entry=%s' % repr(entry)) if entry: (last_cont, last_action) = entry else: return 0 if self.connector == 'and': answer = 1 for a_class in self.types: if not isinstance(last_action, a_class): answer = 0 break else: answer = 0 for a_class in self.types: if isinstance(last_action, a_class): answer = 1 break trace( 'ContLastActionWas.applies', 'last_cont=%s, last_action=%s, self.types=%s, answer=%s' % (last_cont, last_action, self.types, answer)) return answer
def _applies(self, app, preceding_symbol=0): buff = app.curr_buffer() if buff == None: trace("ContLanguage.applies", "buff == None") return False return buff.language_name() in self.language
def kbd_event_sim_factory(self, app): """Returns a [KbdEventSim] that can be used to simulate keyboard events a given client editor. **INPUTS** [AppState] *app* -- the client editor on which we want to simulate keyboard events **OUTPUT** [KbdEventSim] -- an object through which we can simulate keyboard events for that editor. ..[KbdEventSim] file:///./regression.KbdEventSim.html ..[AppState] file:///./AppState.AppState.html""" # For now, this factory method always returns a MS-Windows style # simulator for all editors. # if isinstance(app, AppStateEmacs.AppStateEmacs): # Temporarily disable creation of Emacs style kbd simulator. if 0: debug.trace('PersistentConfigNewMediator.kbd_event_sim_factory', 'Emacs style') return KbdEventSimEmacs() else: debug.trace('PersistentConfigNewMediator.kbd_event_sim_factory', 'Windows style') return KbdEventSimWindowsStyle()
def _deactivate_all_window(self, window): """de-activate all buffer-specific grammars which would be active in window **INPUTS** *INT* window -- identifier of current window. Only grammars associated with that window will be explicitly de-activated. **OUTPUTS** *none* """ debug.trace('GramMgr._deactivate_all_window', 'invoked') if self.dict_grammars.has_key(window): self.sel_grammars[window].deactivate() if self.correction: self.correction_grammars[window].deactivate() self.reformatting_grammars[window].deactivate() if self.text_mode_toggling: self.text_mode_toggling_grammars[window].deactivate() buffers = self.dict_grammars[window].keys() buffers.sort() for a_buffer in buffers: self.dict_grammars[window][a_buffer].deactivate() self.dict_cmd_grammars[window][a_buffer].deactivate()
def change_caps(self, caps = None, one_word = 1): """manually adjust capitalization of the following word or words in the symbol **INPUTS** *STR caps* -- the new capitalization state: 'no-caps', 'normal', 'cap', or 'all-caps' *BOOL one_word* -- if true, modify capitalization for the next word. If false, modify for all following words until a subsequent change_caps with one_word = 0. (A subsequent call to change_caps with one_word = 1 will take precedence temporarily) **OUTPUTS** *none* """ debug.trace('ManualCaps.change_caps', 'caps, one_word = %s, %d' % (caps, one_word)) self.current_caps = caps if one_word: self.one_word = 1 else: self.ongoing_caps = caps self.one_word = 0
def _applies(self, app, preceding_symbol = 0): buff = app.curr_buffer() if buff == None: trace("ContLanguage.applies", "buff == None") return False return buff.language_name() in self.language
def _applies(self, app, preceding_symbol = 0): if preceding_symbol: last_cont = None last_action = actions_gen.ActionInsert('%dummy%') else: entry = app.get_history(1) trace('ContLastActionWas.applies', 'entry=%s' % repr(entry)) if entry: (last_cont, last_action) = entry else: return 0 if self.connector == 'and': answer = 1 for a_class in self.types: if not isinstance(last_action, a_class): answer = 0 break else: answer = 0 for a_class in self.types: if isinstance(last_action, a_class): answer = 1 break trace('ContLastActionWas.applies', 'last_cont=%s, last_action=%s, self.types=%s, answer=%s' % (last_cont, last_action, self.types, answer)) return answer
def start_web_controller(model_filename): """Start up the CherryPy controller for categorization via MODEL_FILENAME""" # TODO: return status code debug.trace(5, "start_web_controller()") # Load in CherryPy configuration # TODO: use external configuration file conf = { '/': { 'tools.sessions.on': True, 'tools.staticdir.root': os.path.abspath(os.getcwd()), ## take 2: on avoiding cross-origin type errrors 'tools.response_headers.on': True, 'tools.response_headers.headers': [ ## OLD: ('Content-Type', 'text/javascript'), ('Access-Control-Allow-Origin', '*'), ] }, 'global': { 'server.socket_host': "0.0.0.0", 'server.socket_port': SERVER_PORT, 'server.thread_pool': 10, } } # Start the server # TODO: trace out all configuration settings debug.trace_values(4, cherrypy.response.headers, "default response headers") cherrypy.quickstart(web_controller(model_filename), "", conf) ## TODO: debug.trace_value(4, cherrypy.response.headers, "response headers") cherrypy.engine.start() return
def boto_method_handler(self, call=None, **kwargs): data = {} trace({'timestamp': time.time(), 'call': call}) try: data = call(**kwargs) except ConnectTimeoutError as error: debug(level=1, service=self.name, error=error) except EndpointConnectionError as error: debug(level=1, service=self.name, error=error) except ClientError as error: try: errcode = error.response['Error']['Code'] data['ErrorData'] = self.ErrHandler.get(errcode, errx)(error) except: debug(level=1, service=self.name, region=self.region) raise except (ValueError, TypeError, ParamValidationError) as error: debug(level=1, service=self.name, region=self.region) raise try: del data['ResponseMetadata'] except: pass return data
def get_mess(self, expect=None): """Gets a message from the external editor. **NOTE:** In this version, get_mess won't block, but will return None if no message is available. **INPUTS** [STR] *expect* -- If not *None*, then make sure the message's name is listed in *expect*. If not, send an error message. **OUTPUTS** (STR, {STR: STR}) name_argvals_mess -- The message retrieved from external editor in *(mess_name, {arg:val})* format, or None if no message is available.""" trace('get_mess', 'expecting %s' % repr(expect)) try: name_argvals_mess = self.receiver.get(block=0) except Queue.Empty: return None if expect != None and (not (name_argvals_mess[0] in expect)): self.wrong_message(name_argvals_mess, expect) if tracing('get_mess.%s' % name_argvals_mess[0]): trace('get_mess.%s' % name_argvals_mess[0], 'got one of %s! It was: %s' \ % (repr(expect), repr(name_argvals_mess))) return name_argvals_mess
def add_letter(self, letter, spoken): """appends a single letter to the symbol **INPUTS** *STR letter* -- the new letter *STR spoken* -- spoken form of the letter **OUTPUTS** *none* """ SymBuilder.add_letter(self, letter, spoken) debug.trace('BuildUnder.add_letter', 'letter = %s, symbol = %s' % (letter, repr(self.symbol))) if self.single: self.abbreviation_state() state = self.separator_state() letter = self.capitalize(letter, self.capitalization_state()) self.symbol = self.symbol + letter debug.trace('BuildUnder.add_letter', 'letter = %s, symbol = %s' % (letter, repr(self.symbol))) else: self.add_word(letter) self.single = 1
def add_phrase(self, phrase, value): """adds a phrase to the trie and associates it with a value Note: if the phrase is already present in the trie, its value will be replaced. Note: the WordTrie uses the value of None to indicate a non-terminal node, so you cannot use None as the value corresponding to a phrase **INPUTS** *[STR] phrase* -- a list of words *ANY value* -- the value which will be retrieved by later queries with this phrase **OUTPUTS** *ANY* -- the old value of that phrase, or None if it didn't exist """ debug.trace('WordTrie.add_phrase', 'phrase=%s, value=%s' % (phrase, value)) if not phrase: old = self.value self.value = value return old word = phrase[0] rest = phrase[1:] if not self.branches.has_key(word): self.branches[word] = WordTrie() # create a new branch self.branches[word].add_phrase(rest, value)
def getconf(self, conf): try: self.conf.update(conf['services'][self.name]) except: errx("No KMS configuration found") trace(self.conf)
def add_word(self, word, original=None): """appends a new word to the symbol **INPUTS** *STR word* -- the new word *STR original* -- original, unabbreviated version (if word is an abbreviation/substitution) or None if word is not abbreviated **OUTPUTS** *none* """ SymBuilder.add_word(self, word, original) debug.trace('BuildUnder.add_word', 'word = %s' % word) debug.trace('BuildUnder.add_word', 'original = %s' % original) self.single = 0 if original and not self.abbreviation_state(): word = original state = self.separator_state() word = self.capitalize(word, self.capitalization_state()) debug.trace('BuildUnder.add_word', 'now, word = %s' % word) debug.trace('BuildUnder.add_word', 'symbol = %s' % repr(self.symbol)) if not word: return if state and self.symbol: last_char = self.symbol[-1] first_char = word[0] if not (last_char.isdigit() or first_char.isdigit() \ or last_char == '_' or first_char == '_'): self.symbol = self.symbol + '_' self.symbol = self.symbol + word debug.trace('BuildUnder.add_word', 'symbol = %s' % repr(self.symbol))
def add_word(self, word, original = None): """appends a new word to the symbol **INPUTS** *STR word* -- the new word *STR original* -- original, unabbreviated version (if word is an abbreviation/substitution) or None if word is not abbreviated **OUTPUTS** *none* """ debug.trace('BuildInterCaps.add_word', 'default_caps = %s' % repr(self.default_caps)) SymBuilder.add_word(self, word, original) if original and not self.abbreviation_state(): word = original self.separator_state() # no separators, but still need to check this once per add_word debug.trace('BuildInterCaps.add_word', 'word before capitalize = %s' % repr(word)) debug.trace('BuildInterCaps.add_word', 'default_caps = %s' % repr(self.default_caps)) word = self.capitalize(word, self.capitalization_state()) debug.trace('BuildInterCaps.add_word', 'word after capitalize = %s' % repr(word)) debug.trace('BuildInterCaps.add_word', 'default_caps = %s' % repr(self.default_caps)) self.symbol = self.symbol + word
def add_letter(self, letter, spoken): """appends a single letter to the symbol **INPUTS** *STR letter* -- the new letter *STR spoken* -- spoken form of the letter **OUTPUTS** *none* """ SymBuilder.add_letter(self, letter, spoken) debug.trace('BuildUnder.add_letter', 'letter = %s, symbol = %s' % (letter, repr(self.symbol))) if self.single: self.abbreviation_state() state = self.separator_state() letter = self.capitalize(letter, self.capitalization_state()) self.symbol = self.symbol + letter debug.trace( 'BuildUnder.add_letter', 'letter = %s, symbol = %s' % (letter, repr(self.symbol))) else: self.add_word(letter) self.single = 1
def run(self): """Start listening for data. **INPUTS** *none* -- **OUTPUTS** *none* -- """ debug.trace('ListenAndQueueMsgsThread.run', 'thread %s starting' % threading.currentThread().getName()) while 1: try: debug.trace('ListenAndQueueMsgsThread.run', '** getting a message') data = self.get_mess() except messaging.SocketError, err: if self.connection_ending.isSet(): # sys.stderr.write('SocketError, but connection_ending was set\n') break # connection broken unexpectedly (unless we just didn't get the # connection_ending event in time) # sys.stderr.write('unexpected SocketError\n') self.completed_msgs.put(self.conn_broken_msg) self.notify_main() break except messaging.WokenUp: break
def restore_state(self, cookie): """ restores the buffer to its state at the time when the cookie was returned by store_current_state. Both the contents and the selection will be restored. However, other data, such as the search history, may not. The restore operation can fail, which will be indicated by a return value of 0, so the caller should always check the return value. **INPUTS** *SourceBuffState cookie* -- see above. **OUTPUTS** *BOOL* -- true if restore was successful """ debug.trace('SB_ServiceFullState.restore_state', 'cookie=%s' % cookie) debug.trace('SB_ServiceFullState.restore_state', 'buffer %s' % self.buff.name()) if not self.valid_cookie(cookie): return 0 if debug.trace_is_active('SB_ServiceFullState.restore_state'): contents = cookie.contents() first_lines = re.match(r'.*\n.*\n', contents).group() last_lines = re.search(r'\n.*\n.*\n?$', contents).group() self.buff.set_text(cookie.contents()) self.buff.set_selection(cookie.get_selection(), cursor_at=cookie.cursor_at()) self.buff.print_buff_if_necessary() self.buff.last_search = cookie.last_search() return 1
def updates_from_app(self, what = None, exclude=1): """Gets a list of updates from the external app. Does this through a messaging protocol. Note: the list of updates must ALWAYS include the name of the external app's active buffer. **INPUTS** [STR] *what* -- List of items to be included/excluded in the updates. BOOL *exclude* -- Indicates if *what* is a list of items to be included or excluded from updates. **OUTPUTS** [ [AS_Update] ] *updates* -- List of updates retrieved from the external app. ..[AS_Update] file:///./AppState.AS_Update.html""" debug.trace('AppStateMessaging.updates_from_app', 'invoked') if what == None: what = [] self.talk_msgr.send_mess('updates') response = self.talk_msgr.get_mess(expect=['updates']) # # Parse response as a list of udate messages # return response[1]['value']
def change_caps(self, caps=None, one_word=1): """manually adjust capitalization of the following word or words in the symbol **INPUTS** *STR caps* -- the new capitalization state: 'no-caps', 'normal', 'cap', or 'all-caps' *BOOL one_word* -- if true, modify capitalization for the next word. If false, modify for all following words until a subsequent change_caps with one_word = 0. (A subsequent call to change_caps with one_word = 1 will take precedence temporarily) **OUTPUTS** *none* """ debug.trace('ManualCaps.change_caps', 'caps, one_word = %s, %d' % (caps, one_word)) self.current_caps = caps if one_word: self.one_word = 1 else: self.ongoing_caps = caps self.one_word = 0
def add_word(self, word, original=None): """appends a new word to the symbol **INPUTS** *STR word* -- the new word *STR original* -- original, unabbreviated version (if word is an abbreviation/substitution) or None if word is not abbreviated **OUTPUTS** *none* """ debug.trace('BuildInterCaps.add_word', 'default_caps = %s' % repr(self.default_caps)) SymBuilder.add_word(self, word, original) if original and not self.abbreviation_state(): word = original self.separator_state() # no separators, but still need to check this once per add_word debug.trace('BuildInterCaps.add_word', 'word before capitalize = %s' % repr(word)) debug.trace('BuildInterCaps.add_word', 'default_caps = %s' % repr(self.default_caps)) word = self.capitalize(word, self.capitalization_state()) debug.trace('BuildInterCaps.add_word', 'word after capitalize = %s' % repr(word)) debug.trace('BuildInterCaps.add_word', 'default_caps = %s' % repr(self.default_caps)) self.symbol = self.symbol + word
def send_mess(self, mess_name, mess_argvals=None): """Sends a message to the external editor. **INPUTS** STR *mess_name* -- Identifier indicating what kind of message this is. {STR: STR} *mess_argvals* -- Dictionary of arguments and values for the message to be sent to the editor. **OUTPUTS** *none* response -- """ trace_id = 'send_mess.%s' % mess_name if tracing(trace_id): ## trace(trace_id, 'self=%s, mess_name=\'%s\'' % (self, mess_name)) trace(trace_id, '--mess_name=\'%s\'' % mess_name) if mess_argvals == None: tmp_args = {} else: tmp_args = copy.copy(mess_argvals) if tracing(trace_id): trace(trace_id, 'mess_argvals=\'%s\'' % tmp_args) unpkd_mess = self.encoder.encode(mess_name, tmp_args) pkd_mess = self.packager.pack_mess(unpkd_mess) self.packager.send_packed_mess(pkd_mess, self.transporter)
def assert_equal_dispatch_according_to_type(self, expected, got, mess, epsilon): self.assert_(self.isnumber(epsilon), "assert_equal called with a value of epsilon that was not a number. Type of epsilon was: %s, value was %s" % (self.what_class(epsilon), epsilon)) if (self.what_class(expected) != self.what_class(got) # Note: Floats can be considered same type as ints and not (self.isnumber(expected) and self.isnumber(got))): mess = mess + "\n----\nThe two objects were not of same type" mess = mess + "\nExpected:\n %s\nGot:\n %s" % (expected, got) self.fail(mess) if (self.issequence(expected)): debug.trace('TestCaseWithHelpers.assert_equal_dispatch_according_to_type', "** comparing sequences") self.assert_equal_sequence(expected, got, mess, epsilon) elif (self.isdictionary(expected)): debug.trace('TestCaseWithHelpers.assert_equal_dispatch_according_to_type', "** comparing dictionaries") self.assert_equal_dictionary(expected, got, mess, epsilon) elif (self.isnumber(expected)): mess = mess + "\n----\nThe two numbers differed significantly." mess = mess + "\nExpected:\n %s\nGot:\n %s" % (expected, got) if epsilon == 0: self.assert_(expected == got, mess) else: self.assert_((got <= expected + epsilon) and (got >= expected - epsilon), mess + "\nValue was not within epsilon=%s range of expected value." % epsilon) elif (self.isstring(expected)): debug.trace('TestCaseWithHelpers.assert_equal_dispatch_according_to_type', "** comparing string types") self.assert_equal_string(expected, got, mess) elif (self.isbasetype(expected)): debug.trace('TestCaseWithHelpers.assert_equal_dispatch_according_to_type', "** comparing simple base types") self.assert_(expected == got, mess) else: debug.trace('TestCaseWithHelpers.assert_equal_dispatch_according_to_type', "** comparing objects") self.assert_equal_objects(expected, got, mess, epsilon)
def add_word(self, word, original = None): """appends a new word to the symbol **INPUTS** *STR word* -- the new word *STR original* -- original, unabbreviated version (if word is an abbreviation/substitution) or None if word is not abbreviated **OUTPUTS** *none* """ SymBuilder.add_word(self, word, original) debug.trace('BuildUnder.add_word', 'word = %s' % word) debug.trace('BuildUnder.add_word', 'original = %s' % original) self.single = 0 if original and not self.abbreviation_state(): word = original state = self.separator_state() word = self.capitalize(word, self.capitalization_state()) debug.trace('BuildUnder.add_word', 'now, word = %s' % word) debug.trace('BuildUnder.add_word', 'symbol = %s' % repr(self.symbol)) if not word: return if state and self.symbol: last_char = self.symbol[-1] first_char = word[0] if not (last_char.isdigit() or first_char.isdigit() \ or last_char == '_' or first_char == '_'): self.symbol = self.symbol + '_' self.symbol = self.symbol + word debug.trace('BuildUnder.add_word', 'symbol = %s' % repr(self.symbol))
def compare_with_current(self, cookie, selection = 0): """compares the current buffer state to its state at the time when the cookie was returned by store_current_state. By default, only the buffer contents are compared, not the selection, unless selection == 1. If the state corresponding to the cookie has been lost, compare_with_current will return false. **INPUTS** *SourceBuffCookie cookie* -- see store_current_state. Note that SourceBuffCookie is a dummy type, not an actual class. The actual type will vary with SourceBuff subclass. *BOOL* selection -- compare selection as well as contents **OUTPUTS** *BOOL* -- true if state is the same, false if it is not, or it cannot be determined due to expiration of the cookie """ if not self.valid_cookie(cookie): debug.trace('SourceBuffWithDiffs.compare_with_current', 'invalid cookie') return 0 index = self.cookie_jar.index(cookie.cookie_key) data = self.cookie_jar.peek(index) level = data.level if not self.no_change(level): debug.trace('SourceBuffWithDiffs.compare_with_current', 'non-empty list of diffs') return 0 if selection: return self.compare_selection_with_current(cookie) return 1
def insert_cbk(self, range, text): """External editor invokes that callback to notify VoiceCode of a deletion event. **INPUTS** (INT, INT) *range* -- Start and end position of text to be replaced by the insertion. If end of range is None, default to the end of the buffer. STR *text* -- Text to be inserted **OUTPUTS** *none* -- """ if tracing('SourceBuffWithDiffs.insert_cbk'): debug.trace('SourceBuffWithDiffs.insert_cbk', 'buff %s: replacing range %s with "%s"' \ % (self.name(), repr(range), text)) if self.undoing: debug.trace('SourceBuffWithDiffs.insert_cbk', 'in process of undoing') self.during_undo(text = text, range = range) else: if self._not_cached('get_text'): # if we don't have the buffer contents cached, we don't know what text # was replaced, so we can't create a reverse diff, and all our previous # change_history is invalid debug.trace('SourceBuffWithDiffs.insert_cbk', 'not cached') if range[1] != range[0] and range[1] != range[0] + 1: debug.trace('SourceBuffWithDiffs.insert_cbk', 'non-empty change') self.clear_stacks() else: # we need the old text, so we have to do all this processing before # calling SourceBuffCached.insert_cbk range_non_nil = [range[0], range[1]] if range_non_nil[1] == None: range_non_nil[1] = len(self._get_cache('get_text')) - 1 replaced = self.cache['get_text'][range_non_nil[0]:range_non_nil[1]] if tracing('SourceBuffWithDiffs.insert_cbk'): debug.trace('SourceBuffWithDiffs.insert_cbk', 'replaced text "%s"' % replaced) # don't record non-changes if replaced != text: # for the reverse diff, we need the range of the new text # The start of the new text is the same as the start of the old text, # but the end is offset from the start by one more than the length of # the new text start = range_non_nil[0] # we use the same convention for ranges as Python's slice end = start + len(text) reverse = ReverseBufferChange(replaced, (start, end)) self._push_change(reverse) SourceBuffCached.insert_cbk(self, range, text)
def assert_equal(self, expected, got, mess="", epsilon=0): debug.trace('assert_equal', 'expected=%s, got=%s' % (expected, got)) try: self.assert_equal_dispatch_according_to_type(expected, got, mess, epsilon) except RuntimeError, err: err = self.check_for_infinite_recursion_error(err) raise err
def paste(self): """Paste content of clipboard into current buffer""" trace('SourceBuffMessaging.paste', '** invoked') self.app.talk_msgr.send_mess('paste', {'buff_name': self.name()}) response = self.app.talk_msgr.get_mess(expect=['paste_resp']) self.app.update_response = 1 self.app.apply_upd_descr(response[1]['updates']) self.app.update_response = 0
def copy_selection(self): """Copy the selected text""" trace('SourceBuffMessaging.copy_selection', '** invoked on buffer %s' % self.name()) self.app.talk_msgr.send_mess('copy_selection', {'buff_name': self.name()}) response = self.app.talk_msgr.get_mess(expect=['copy_selection_resp']) self.app.update_response = 1 self.app.apply_upd_descr(response[1]['updates']) self.app.update_response = 0
def context_applies_for_lang(self, language, context): trace("WhatCanISay.context_applies_for_lang", "** language=%s, context=%s" % (language, context)) answer = False if isinstance(context, ContLanguage) and language in context.language: answer = True elif isinstance(context, ContAny): answer = True return answer