def insert_shortened_url(textctrl, url, ondone=None, timeoutms=5000): import util.net textctrl.Freeze() textctrl.Enable(False) class C(object): pass c = C() c._finished = False def finish(shorturl=None): if c._finished: return c._finished = True insert_text(textctrl, shorturl or url) textctrl.Thaw() textctrl.Enable() textctrl.SetFocus() if ondone is not None: ondone(shorturl) def get(): short_url = None with util.traceguard: short_url = util.net.get_short_url(url) if short_url is not None and len(short_url) >= len(url): short_url = url wx.CallAfter(lambda: finish(short_url)) util.threaded(get)() if timeoutms is not None: wx.CallLater(timeoutms, finish) def cancel(): finish(None) return cancel
def send_method(self, call, callback = None): url = net.UrlQuery(call.get_endpoint(self.API_BASE), api_key = self.api_key) req = urllib2.Request(url, dict((name, arg.value) for name, arg in call.bound_args.items()), ) util.threaded(urllib2.urlopen)(req, callback = callback)
def stop(self): 'End all communication with the video widget.' self.stop = lambda *a: None # don't stop more than once self._stopped = True log.info('stopping video chat %r', self) # destroy the video window if self.video_frame: gui_call_later(self.video_frame.Destroy) # appear offline to the widget convo = self.widget_convo if convo is not None: netcall(self.widget_convo.buddy.appear_offline_to) # remove IM window link token = self.http.video_token if token is not None: conn = profile.connection if conn is not None: conn.remove_video_chat(token) # tell the server to kill the video info threaded(self.http.close_video)() self.on_stop()
def send_method(self, call, callback = None): url = net.UrlQuery(call.get_endpoint(self.API_BASE) + '.' + call.spec.format) params = dict((name, arg.value) for name, arg in call.bound_args.items()) params['key'] = self.api_key req = urllib2.Request(url, params) util.threaded(urllib2.urlopen)(req, callback = callback)
def send_method(self, call, callback=None): url = net.UrlQuery(call.get_endpoint(self.API_BASE), api_key=self.api_key) req = urllib2.Request( url, dict((name, arg.value) for name, arg in call.bound_args.items()), ) util.threaded(urllib2.urlopen)(req, callback=callback)
def _send_email_form(self, to, subject, body, cc, bcc, callback=None): cfg = self.HM.Network.configuration import ClientForm form = ClientForm.HTMLForm(util.UrlQuery(util.httpjoin( self.inbox_url, '/mail/SendMessageLight.aspx'), _ec=1, n=ntok()), method="POST", request_class=self.RequestFactory) form.new_control('hidden', '__VIEWSTATE', {'value': ''}) form.new_control('hidden', cfg.CanaryToken, {'value': cfg.CanaryValue}) form.new_control('hidden', 'MsgPriority', {'value': '0'}) form.new_control('hidden', 'ToolbarActionItem', {'value': 'SendMessage'}) form.new_control('hidden', 'InfoPaneActionItem', {'value': ''}) form.new_control('hidden', 'folderCache', {'value': ''}) form.new_control('hidden', 'fDraftId', {'value': ''}) form.new_control('hidden', 'fMsgSentState', {'value': 'NOACTION'}) form.new_control('hidden', 'IsSpellChecked', {'value': 'false'}) form.new_control('hidden', 'fFrom', {'value': self.username}) form.new_control('hidden', 'cpselectedAutoCompleteTo', {'value': '[]'}) form.new_control('hidden', 'fTo', {'value': '"" <%s>;' % to}) form.new_control('hidden', 'cpselectedAutoCompleteCc', {'value': ''}) form.new_control('hidden', 'fCc', {'value': cc}) form.new_control('hidden', 'cpselectedAutoCompleteBcc', {'value': ''}) form.new_control('hidden', 'fBcc', {'value': bcc}) form.new_control('hidden', 'fSubject', {'value': subject}) form.new_control('hidden', 'fAttachment_data', {'value': ''}) form.new_control('hidden', 'isFirstPL', {'value': ''}) form.new_control('hidden', 'RTE_MessageType', {'value': 'PlainText'}) form.new_control('hidden', 'fMessageBody', {'value': body}) request = form.click() log.info('Request obj = %r, vars = %r', request, vars(request)) def check_success_send(resp): if resp is None: # assume it worked? return callback.success() data = resp.read() if 'SentMailConfirmation' in data: log.info('sent email!') callback.success() else: log.info('failed to send email: %r', data) callback.error() def check_error_send(exc): log.info('failed to send email: %r', exc) callback.error() util.threaded(self.open)(request, success=check_success_send, error=check_error_send)
def save_data(self): """Saves video data.""" def __save_vid(s, v): print(f"Saving {v}...") s.downloader.get_video_stream(v, s.res, s.fps, s.codec) s.downloader.save_video(s.folder, s.downloader.stream.title) s.downloader.save_video_data(v, s.folder, s.downloader.stream.title) print(f"{v} Saved!") for v in self.video_urls: threaded(__save_vid(self, v))
def _send_email_form(self, to, subject, body, cc, bcc, callback = None): cfg = self.HM.Network.configuration import ClientForm form = ClientForm.HTMLForm(util.UrlQuery(util.httpjoin(self.inbox_url, '/mail/SendMessageLight.aspx'), _ec = 1, n = ntok()), method="POST", request_class = self.RequestFactory) form.new_control('hidden', '__VIEWSTATE', {'value' : ''}) form.new_control('hidden', cfg.CanaryToken, {'value' : cfg.CanaryValue}) form.new_control('hidden', 'MsgPriority', {'value' : '0'}) form.new_control('hidden', 'ToolbarActionItem', {'value' : 'SendMessage'}) form.new_control('hidden', 'InfoPaneActionItem', {'value' : ''}) form.new_control('hidden', 'folderCache', {'value' : ''}) form.new_control('hidden', 'fDraftId', {'value' : ''}) form.new_control('hidden', 'fMsgSentState', {'value' : 'NOACTION'}) form.new_control('hidden', 'IsSpellChecked', {'value' : 'false'}) form.new_control('hidden', 'fFrom', {'value' : self.username}) form.new_control('hidden', 'cpselectedAutoCompleteTo', {'value' : '[]'}) form.new_control('hidden', 'fTo', {'value' : '"" <%s>;' % to}) form.new_control('hidden', 'cpselectedAutoCompleteCc', {'value' : ''}) form.new_control('hidden', 'fCc', {'value' : cc}) form.new_control('hidden', 'cpselectedAutoCompleteBcc', {'value' : ''}) form.new_control('hidden', 'fBcc', {'value' : bcc}) form.new_control('hidden', 'fSubject', {'value' : subject}) form.new_control('hidden', 'fAttachment_data', {'value' : ''}) form.new_control('hidden', 'isFirstPL', {'value' : ''}) form.new_control('hidden', 'RTE_MessageType', {'value' : 'PlainText'}) form.new_control('hidden', 'fMessageBody', {'value' : body}) request = form.click() log.info('Request obj = %r, vars = %r', request, vars(request)) def check_success_send(resp): if resp is None: # assume it worked? return callback.success() data = resp.read() if 'SentMailConfirmation' in data: log.info('sent email!') callback.success() else: log.info('failed to send email: %r', data) callback.error() def check_error_send(exc): log.info('failed to send email: %r', exc) callback.error() util.threaded(self.open)(request, success = check_success_send, error = check_error_send)
def shorten(link=link): # close over link def one_link_shortened(): links.count -= 1 if links.count == 0: finish() def success(short_url): @wx.CallAfter def after(): if links.finished: return # TODO: use adjusting spans. textctrl.SetValue(textctrl.Value.replace(link, short_url)) one_link_shortened() def error(): print >> sys.stderr, 'error shortening link: %r' % link one_link_shortened() threaded(get_short_url)(link, success=success, error=error)
def run(self): def __login(s): s.manager.login() login_button['state'] = DISABLED download_button['state'] = 'normal' upload_button['state'] = 'normal' url = "" url_label = ttk.Label(self.mainframe, text="Enter Video URL To Download") url_input = ttk.Entry(self.mainframe, width=30, textvariable=url) download_button = ttk.Button(self.mainframe, text="Download", command=lambda: threaded(lambda: self.manager.save_videos( url_input.get()))) login_button = ttk.Button(self.mainframe, text="Login", command=lambda: threaded(lambda: __login(self))) browse_button = ttk.Button(self.mainframe, text="Browse", command=self.set_path) upload_button = ttk.Button(self.mainframe, text="Upload", command=lambda: self.threaded_with_path( self.manager.post_video)) login_button.grid(column=0, row=0, sticky=W, pady=10, padx=10) url_label.grid(column=0, row=1, padx=10) url_input.grid(column=1, row=1, padx=10) download_button.grid(column=1, row=3) browse_button.grid(column=0, row=4, pady=10) upload_button.grid(column=2, row=4, pady=10, padx=10) download_button['state'] = DISABLED upload_button['state'] = DISABLED self.root.mainloop()
def __init__(self, profile): Observable.__init__(self) self.accounts_loaded = False self.profile = profile self.connected_accounts = ObservableList() self.reconnect_timers = {} # holds "cancel" objects from Popups self.cancellers = {} self.profile.add_observer(self.on_state_change, 'state') self.profile.add_observer(self.on_offline_change, 'offline_reason') self.profile.add_observer(self.profile_state_changed, 'state') import wx wx.GetApp().OnBuddyListShown.append(lambda *a, **k: Timer(.25, threaded(self.release_accounts), *a, **k).start()) self._hash = sentinel self.got_accounts = False self.acct_calls = Delegate() self.delay_accounts = True self.acct_delay_lock = RLock() self._all_acct_hash = {} self.last_server_order = None self._all_accounts = Storage() for type_ in ('im', 'em', 'so'): s = Storage(accounts = ObservableList(), old = []) setattr(self._all_accounts, type_, s) # when the order of accounts changes, or accounts are added or deleted, # calls profile.accounts_changed('im', list) s.accounts.add_observer(getattr(self, type_ + '_accounts_changed')) self.accounts = self._all_accounts.im.accounts self.emailaccounts = self._all_accounts.em.accounts self.socialaccounts = self._all_accounts.so.accounts self.buddywatcher = BuddyWatcher() import services.service_provider as sp container = sp.ServiceProviderContainer(self.profile) container.on_order_changed += self._set_order
def __init__(self, buddy): self.buddy_info = BuddyInfo(buddy) self.http = VideoChatHttp(profile.username, profile.password) self._widget_jid = None self._stopped = False self.on_stop = Delegate() self.handle_buddy_state(buddy) # Video chat aliases my_alias = buddy.protocol.self_buddy.name friend_alias = buddy.name # fetch a video token on the threadpool create = threaded(self.http.create_video) create(my_alias, friend_alias, success = self.on_token, error = self.error_token)
def send_bug_report(**info): 'called after OK is clicked' from gui.toolbox import progress_dialog from util import Timer # this threadsafe function will show a "neverending" (indeterminate) # progress dialog, since the diagnostics gathering may take awhile line1 = _('Please wait while we process the diagnostic information.') line2 = _('Thanks for your patience!') p_diag = progress_dialog(u'%s\n%s' % (line1, line2), title = _('Processing Diagnostic Info')) def later(): bugreport_error_line1 = _('There was a problem submitting your bug report.') bugreport_error_line2 = _('If the problem persists, please email [email protected]') message = '%s\n\n%s' % (bugreport_error_line1, bugreport_error_line2) try: d = Diagnostic(**info) d.prepare_data() d.do_no_thread_post() message = _('Bug report sent successfully.') except Exception: print_exc() finally: p_diag.stop() # destroys the dialog import wx def later(): if not maybe_open_bug_url(d): wx.MessageBox(message, _('Submit Bug Report')) gui_native.memory_event() wx.CallLater(1000, later) # use a one second timer to give the GUI a chance to display Timer(1, threaded(later)).start()
def __init__(self, buddy): self.buddy_info = BuddyInfo(buddy) self.http = VideoChatHttp(profile.username, profile.password) self._widget_jid = None self._stopped = False self.on_stop = Delegate() self.handle_buddy_state(buddy) # Video chat aliases my_alias = buddy.protocol.self_buddy.name friend_alias = buddy.name # fetch a video token on the threadpool create = threaded(self.http.create_video) create(my_alias, friend_alias, success=self.on_token, error=self.error_token)
def main(): threadpool.ThreadPool(15) for i in range(50): util.threaded(do_something_bad)() time.sleep(1) gc.collect() for i in range(50): util.threaded(do_something_good)() for i in xrange(50): util.threaded(sys.exc_clear)() time.sleep(1) gc.collect() print len(filter(is_match, gc.get_objects()))
def threaded_with_path(self, func): """Runs the function in a separate thread while giving it the current path.""" if self.folder != "": threaded(lambda: func(self.folder))
def _update(self): if not getattr(self, 'should_update', lambda: True)(): return log.info("calling connection.update") util.threaded(self.connection.update)()
def on_offline_change(self, src, attr, old, new): accounts_debug('%s\'s %s changed from %s to %s', src, attr, old, new) assert attr in ('offline_reason', None) attr = 'offline_reason' if new is None: new = getattr(src, attr) Reasons = StateMixin.Reasons conditions = ( old == new, # no change...this function shouldn't have been called in the first place new == StateMixin.Reasons.NONE, # normal offline state, doesn't matter ) if any(conditions): return log.debug('%s offline reason: %r->%r', src, old, new) if getattr(Reasons, 'WILL_RECONNECT', None) in (new, old): # something we set - ignore for now # new means we set it lower down in this function, old means we're moving out of this state, which should # not be an error. log.debug('Skipping the rest because reason is WILL_RECONNECT') return if new == getattr(Reasons, 'BAD_PASSWORD', None) and src is self.profile: if not self.profile.has_authorized: log.debug( 'Wrong password for digsbyprofile - not going to reconnect' ) return else: new = None if src is self.profile and not self.profile.loaded: log.debug( 'DigsbyProfile has never connected, not reconnecting after %s state.', new) return if (is_im_account(src) or src is self.profile) and new not in ( Reasons.BAD_PASSWORD, Reasons.NO_MAILBOX, Reasons.OTHER_USER, Reasons.RATE_LIMIT, Reasons.SERVER_ERROR): maxerror = (pref('%s.max_error_tolerance' % src.protocol, False) or getattr(src, 'max_error_tolerance', False) or pref('login.max_error_tolerance', False) or 4) count = src.error_count src.error_count += 1 log.info( '%s\'s error_count is now %d.', src, src.error_count, ) if (self.reconnect or src is self.profile): #and count < maxerror: if src in self.reconnect_timers: src.error_count -= 1 # account is already scheduled for a reconnect return src.setnotifyif('offline_reason', Reasons.WILL_RECONNECT) # schedule/attempt reconnect reconnect_time = get((1, 10, 30, 300), count, 300) if src in self.accounts or src is self.profile: profile_on_return = False if src is self.profile: log.critical( 'Going to try to reconnect the digsbyprofile. This could get interesting...' ) reconnect_time, profile_on_return = self.get_profile_reconnect_time( ) def rct(): log.info('Reconnecting %s...', src) try: log.warning( 'src=%r...setting on_connect to change_state', src) if src is self.profile: def set_online(*a, **k): src.connection.setnotify( 'state', StateMixin.Statuses.ONLINE) src.on_connect = set_online if getattr(src, 'connection', None) is None: src._reconnect() else: log.error( 'There was already a connection for this account that was supposed to reconnect: %r', src) except Exception, e: log.critical( 'Error while trying to reconnect %s (error was: %r)', src, e) traceback.print_exc() x = self.reconnect_timers.pop(src, None) if x is not None: x.stop() log.info( 'Starting reconnect timer for %s. Will reconnect in %d seconds %r', src, reconnect_time, self.state_desc(src)) self.reconnect_timers[src] = rct_timer = call_later( reconnect_time, threaded(rct)) if profile_on_return: def reconnect_profile_now(*a, **k): rct_timer.done_at = 0 wakeup_timeout_thread() self.profile.OnReturnFromIdle += reconnect_profile_now return else: assert isinstance(src, UpdateMixin) # this is a social or email account -- it has its own timers and things # and will attempt the next update when appropriate return log.info('Error count too high, or reconnect disabled.')
def on_offline_change(self, src, attr, old, new): accounts_debug('%s\'s %s changed from %s to %s', src, attr, old, new) assert attr in ('offline_reason', None) attr = 'offline_reason' if new is None: new = getattr(src, attr) Reasons = StateMixin.Reasons conditions = (old == new, # no change...this function shouldn't have been called in the first place new == StateMixin.Reasons.NONE, # normal offline state, doesn't matter ) if any(conditions): return log.debug('%s offline reason: %r->%r', src, old, new) if getattr(Reasons, 'WILL_RECONNECT', None) in (new, old): # something we set - ignore for now # new means we set it lower down in this function, old means we're moving out of this state, which should # not be an error. log.debug('Skipping the rest because reason is WILL_RECONNECT') return if new == getattr(Reasons, 'BAD_PASSWORD', None) and src is self.profile: if not self.profile.has_authorized: log.debug('Wrong password for digsbyprofile - not going to reconnect') return else: new = None if src is self.profile and not self.profile.loaded: log.debug('DigsbyProfile has never connected, not reconnecting after %s state.', new) return if (is_im_account(src) or src is self.profile) and new not in (Reasons.BAD_PASSWORD, Reasons.NO_MAILBOX, Reasons.OTHER_USER, Reasons.RATE_LIMIT, Reasons.SERVER_ERROR): maxerror = (pref('%s.max_error_tolerance' % src.protocol, False) or getattr(src, 'max_error_tolerance', False) or pref('login.max_error_tolerance', False) or 4 ) count = src.error_count src.error_count += 1 log.info('%s\'s error_count is now %d.', src, src.error_count,) if (self.reconnect or src is self.profile): #and count < maxerror: if src in self.reconnect_timers: src.error_count -= 1 # account is already scheduled for a reconnect return src.setnotifyif('offline_reason', Reasons.WILL_RECONNECT) # schedule/attempt reconnect reconnect_time = get((1,10,30,300), count, 300) if src in self.accounts or src is self.profile: profile_on_return = False if src is self.profile: log.critical('Going to try to reconnect the digsbyprofile. This could get interesting...') reconnect_time, profile_on_return = self.get_profile_reconnect_time() def rct(): log.info('Reconnecting %s...', src) try: log.warning('src=%r...setting on_connect to change_state', src) if src is self.profile: def set_online(*a, **k): src.connection.setnotify('state', StateMixin.Statuses.ONLINE) src.on_connect = set_online if getattr(src, 'connection', None) is None: src._reconnect() else: log.error('There was already a connection for this account that was supposed to reconnect: %r', src) except Exception, e: log.critical('Error while trying to reconnect %s (error was: %r)', src, e) traceback.print_exc() x = self.reconnect_timers.pop(src,None) if x is not None: x.stop() log.info('Starting reconnect timer for %s. Will reconnect in %d seconds %r', src, reconnect_time, self.state_desc(src)) self.reconnect_timers[src] = rct_timer = call_later(reconnect_time, threaded(rct)) if profile_on_return: def reconnect_profile_now(*a, **k): rct_timer.done_at = 0 wakeup_timeout_thread() self.profile.OnReturnFromIdle += reconnect_profile_now return else: assert isinstance(src, UpdateMixin) # this is a social or email account -- it has its own timers and things # and will attempt the next update when appropriate return log.info('Error count too high, or reconnect disabled.')
def markAsRead(self, email): common.emailaccount.EmailAccount.markAsRead(self, email) util.threaded(email.mark_as_read)()
def later(*a, **k): wx.CallLater(1000, threaded(self.logger.on_message), *a, **k)
def delete(self, email): common.emailaccount.EmailAccount.delete(self, email) util.threaded(email.delete)()
def reportSpam(self, email): common.emailaccount.EmailAccount.reportSpam(self, email) util.threaded(email.spam)()
import urllib2 import util import util.httptools as httptools import util.threads.threadpool as threadpool import digsbysite, logextensions, netextensions import tests.testapp as testapp if __name__ == '__main__': _app = testapp.testapp() def success(resp): print 'success:', repr(resp.read()) def error(err = None): print 'error:', None req = urllib2.Request('http://www.google.com') opener = util.net.build_opener() ro = httptools.RequestOpener(util.threaded(opener.open), req) ro.open(success = success, error = error) tp = threadpool.ThreadPool(2) _app.toggle_crust() _app.MainLoop() tp.joinAll()
def _on_timer(self): threaded(self.save_diagnostic)()
import urllib2 import util import util.httptools as httptools import util.threads.threadpool as threadpool import digsbysite, logextensions, netextensions import tests.testapp as testapp if __name__ == "__main__": _app = testapp.testapp() def success(resp): print "success:", repr(resp.read()) def error(err=None): print "error:", None req = urllib2.Request("http://www.google.com") opener = util.net.build_opener() ro = httptools.RequestOpener(util.threaded(opener.open), req) ro.open(success=success, error=error) tp = threadpool.ThreadPool(2) _app.toggle_crust() _app.MainLoop() tp.joinAll()