def init(self, **kw): if kw.has_key('stdin'): cmd.Cmd.__init__(self, None, stdin=kw['stdin']) self.use_rawinput = False else: cmd.Cmd.__init__(self) # initialize a new local namespace. namespaces.new_local_dict() # import readline history, if available. if readline: try: readline.read_history_file('.twill-history') except IOError: pass # fail on unknown commands? for test-shell, primarily. self.fail_on_unknown = kw.get('fail_on_unknown', False) # handle initial URL argument if kw.get('initial_url'): commands.go(kw['initial_url']) self._set_prompt() self.names = [] global_dict, local_dict = namespaces.get_twill_glocals() ### add all of the commands from twill. for command in parse.command_list: fn = global_dict.get(command) self.add_command(command, fn.__doc__)
def _execute_script(inp, **kw): """ Execute lines taken from a file-like iterator. """ # initialize new local dictionary & get global + current local namespaces.new_local_dict() globals_dict, locals_dict = namespaces.get_twill_glocals() locals_dict['__url__'] = commands.browser.get_url() # reset browser if not kw.get('no_reset'): commands.reset_browser() # go to a specific URL? init_url = kw.get('initial_url') if init_url: commands.go(init_url) locals_dict['__url__'] = commands.browser.get_url() # should we catch exceptions on failure? catch_errors = False if kw.get('never_fail'): catch_errors = True # sourceinfo stuff sourceinfo = kw.get('source', "<input>") try: for n, line in enumerate(inp): if not line.strip(): # skip empty lines continue cmdinfo = "%s:%d" % (sourceinfo, n,) print 'AT LINE:', cmdinfo cmd, args = parse_command(line, globals_dict, locals_dict) if cmd is None: continue try: execute_command(cmd, args, globals_dict, locals_dict, cmdinfo) except SystemExit: # abort script execution, if a SystemExit is raised. return except TwillAssertionError, e: print>>commands.ERR, '''\ Oops! Twill assertion error on line %d of '%s' while executing >> %s %s ''' % (n, sourceinfo, line.strip(), e) if not catch_errors: raise except Exception, e: print>>commands.ERR, '''\ EXCEPTION raised at line %d of '%s' %s Error message: '%s' ''' % (n, sourceinfo, line.strip(),str(e).strip(),) if not catch_errors: raise
def _execute_script(inp, **kw): """ Execute lines taken from a file-like iterator. """ # initialize new local dictionary & get global + current local namespaces.new_local_dict() globals_dict, locals_dict = namespaces.get_twill_glocals() locals_dict['__url__'] = commands.browser.get_url() # reset browser if not kw.get('no_reset'): commands.reset_browser() # go to a specific URL? init_url = kw.get('initial_url') if init_url: commands.go(init_url) locals_dict['__url__'] = commands.browser.get_url() # should we catch exceptions on failure? catch_errors = False if kw.get('never_fail'): catch_errors = True # sourceinfo stuff sourceinfo = kw.get('source', "<input>") try: for n, line in enumerate(inp): if not line.strip(): # skip empty lines continue cmdinfo = "%s:%d" % ( sourceinfo, n, ) print 'AT LINE:', cmdinfo cmd, args = parse_command(line, globals_dict, locals_dict) if cmd is None: continue try: execute_command(cmd, args, globals_dict, locals_dict, cmdinfo) except SystemExit: # abort script execution, if a SystemExit is raised. return except TwillAssertionError, e: print >> commands.ERR, '''\ Oops! Twill assertion error on line %d of '%s' while executing >> %s %s ''' % (n, sourceinfo, line.strip(), e) if not catch_errors: raise except Exception, e: print >> commands.ERR, '''\ EXCEPTION raised at line %d of '%s' %s Error message: '%s' ''' % ( n, sourceinfo, line.strip(), str(e).strip(), ) if not catch_errors: raise