def register(self, overlay_bridge, metadataDBHandler, session): """ Injects the required dependencies on the instance. @param overlay_bridge: a reference to a working instance of OverlayTrheadingBridge @param metadataDBHandler: a reference to the current instance of L{MetadataDBHandler} @param session: a reference to the running session """ self.overlay_bridge = overlay_bridge self.subtitlesDb = metadataDBHandler self.config_dir = os.path.abspath(session.get_state_dir()) subs_path = os.path.join(self.config_dir, session.get_subtitles_collecting_dir()) # George Milescu, 19.11.2010 # I replaced self.subs_dir because os.path.abspath(subs_path) returns an inexistent path for non-standard tribler paths # self.subs_dir = os.path.abspath(subs_path) self.subs_dir = os.path.abspath(session.get_subtitles_collecting_dir()) self._upload_rate = session.get_subtitles_upload_rate() self.max_subs_message_size = MAX_SUBS_MESSAGE_SIZE self._session = session #the upload rate is controlled by a token bucket. #a token corresponds to 1 KB. #The max burst size corresponds to 2 subtitles of the maximum size (2 MBs) tokenBucket = SimpleTokenBucket(self._upload_rate, self.max_subs_message_size) self._subsMsgHndlr = SubsMessageHandler(self.overlay_bridge, tokenBucket, MAX_SUBTITLE_SIZE) self._subsMsgHndlr.registerListener(self) #assure that the directory exists if os.path.isdir(self.config_dir) : if not os.path.isdir(self.subs_dir): try: os.mkdir(self.subs_dir) except: msg = u"Cannot create collecting dir %s " % self.subs_dir print >> sys.stderr, "Error: %s" % msg raise IOError(msg) else: msg = u"Configuration dir %s does not exists" % self.subs_dir print >> sys.stderr, "Error: %s" % msg raise IOError(msg) #event notifier self._notifier = Notifier.getInstance() self.registered = True
def setUp(self): self.ol_bridge = MockOverlayBridge() self.tokenBucket = MockTokenBucket() self.underTest = SubsMessageHandler(self.ol_bridge,self.tokenBucket,1000000)