hydrus/include/TestConstants.py

134 lines
4.1 KiB
Python
Raw Normal View History

2013-07-10 20:25:57 +00:00
import ClientConstants
2013-12-11 22:09:25 +00:00
import collections
2013-07-10 20:25:57 +00:00
import HydrusConstants as HC
2013-07-17 20:56:13 +00:00
import HydrusTags
2013-07-10 20:25:57 +00:00
import os
import random
2013-12-11 22:09:25 +00:00
import threading
2014-06-18 21:53:48 +00:00
import weakref
2015-02-25 19:34:30 +00:00
import wx
2013-07-31 21:26:38 +00:00
tinest_gif = '\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x00\xFF\x00\x2C\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x00\x3B'
2013-07-10 20:25:57 +00:00
2014-02-12 23:09:38 +00:00
class FakeHTTPConnectionManager():
2013-07-31 21:26:38 +00:00
2014-02-12 23:09:38 +00:00
def __init__( self ):
2013-11-06 18:22:07 +00:00
2014-02-12 23:09:38 +00:00
self._fake_responses = {}
2013-07-31 21:26:38 +00:00
2014-02-12 23:09:38 +00:00
def Request( self, method, url, request_headers = {}, body = '', return_everything = False, return_cookies = False, report_hooks = [], response_to_path = False, long_timeout = False ):
2013-07-31 21:26:38 +00:00
2014-02-12 23:09:38 +00:00
( response, size_of_response, response_headers, cookies ) = self._fake_responses[ ( method, url ) ]
2013-07-31 21:26:38 +00:00
2014-02-12 23:09:38 +00:00
if response_to_path:
2013-11-06 18:22:07 +00:00
2014-02-12 23:09:38 +00:00
temp_path = HC.GetTempPath()
2013-11-06 18:22:07 +00:00
2014-02-12 23:09:38 +00:00
with open( temp_path, 'wb' ) as f: f.write( response )
response = temp_path
2013-11-06 18:22:07 +00:00
2013-07-31 21:26:38 +00:00
2014-02-12 23:09:38 +00:00
if return_everything: return ( response, size_of_response, response_headers, cookies )
elif return_cookies: return ( response, cookies )
else: return response
2013-07-31 21:26:38 +00:00
2014-02-12 23:09:38 +00:00
def SetResponse( self, method, url, response, size_of_response = 100, response_headers = {}, cookies = [] ):
2013-07-31 21:26:38 +00:00
2014-02-12 23:09:38 +00:00
self._fake_responses[ ( method, url ) ] = ( response, size_of_response, response_headers, cookies )
2013-07-31 21:26:38 +00:00
2013-12-04 22:44:16 +00:00
class FakeWebSessionManager():
def GetCookies( self, *args, **kwargs ): return { 'session_cookie' : 'blah' }
2013-12-11 22:09:25 +00:00
class FakePubSub():
def __init__( self ):
self._pubsubs = collections.defaultdict( list )
2014-06-18 21:53:48 +00:00
self._callables = []
2013-12-11 22:09:25 +00:00
self._lock = threading.Lock()
2014-06-18 21:53:48 +00:00
self._topics_to_objects = {}
self._topics_to_method_names = {}
def _GetCallables( self, topic ):
callables = []
if topic in self._topics_to_objects:
try:
objects = self._topics_to_objects[ topic ]
for object in objects:
method_names = self._topics_to_method_names[ topic ]
for method_name in method_names:
if hasattr( object, method_name ):
try:
callable = getattr( object, method_name )
callables.append( callable )
except wx.PyDeadObjectError: pass
except TypeError as e:
if '_wxPyDeadObject' not in str( e ): raise
except: pass
return callables
2013-12-11 22:09:25 +00:00
def ClearPubSubs( self ): self._pubsubs = collections.defaultdict( list )
def GetPubSubs( self, topic ): return self._pubsubs[ topic ]
2014-06-18 21:53:48 +00:00
def NoJobsQueued( self ): return True
2013-12-11 22:09:25 +00:00
def WXProcessQueueItem( self ): pass
def pub( self, topic, *args, **kwargs ):
with self._lock:
self._pubsubs[ topic ].append( ( args, kwargs ) )
2014-06-18 21:53:48 +00:00
def sub( self, object, method_name, topic ):
if topic not in self._topics_to_objects: self._topics_to_objects[ topic ] = weakref.WeakSet()
if topic not in self._topics_to_method_names: self._topics_to_method_names[ topic ] = set()
self._topics_to_objects[ topic ].add( object )
self._topics_to_method_names[ topic ].add( method_name )
2013-12-11 22:09:25 +00:00
def WXpubimmediate( self, topic, *args, **kwargs ):
2014-01-29 21:59:42 +00:00
2013-12-11 22:09:25 +00:00
with self._lock:
2014-06-18 21:53:48 +00:00
callables = self._GetCallables( topic )
for callable in callables: callable( *args, **kwargs )
2013-12-11 22:09:25 +00:00
2013-12-04 22:44:16 +00:00