예제 #1
0
 def main(self):
   #ApplicationDownloader.ConfigurationCreation.call(self, self.configuration)
   self.newConfiguration(self.configuration)
   #ApplicationDownloader.LoggerCreation.call(self, self.logger)
   self.newLogger(self.logger)
   #return
    
   try:
     if not self.configuration.usenet_server == "news.foobar.com":
         self.nntp = NNTPDownload(self)
         self._nzbparser = NZBParser(self)
       
         HTTPHandler.application = self
         self.httpd = StoppableHTTPServer((self.configuration.local_server_address,self.configuration.local_port), HTTPHandler)
         thread.start_new_thread(self.httpd.serve, ())
         
         self.is_running_cv.acquire()
         enough_time = False
         while self.is_running:
             #warn("ApplicationDownloader thread: running!")
             
             if self.configuration.do_help is True:
               if self.nntp.enough_time_passed() is True and enough_time is False:
                 enough_time = True
                 warn("enough time passed!")
                 continue
               if self.nntp.enough_time_passed() is False and enough_time is True:
                 enough_time = False      
                 continue  
             self.is_running_cv.wait(1.0)
         self.is_running_cv.release()
         self.httpd.stop()
         
     else:
         warn("----------------------------------")
         warn("Your configuration file isn't set.")
         warn("Please edit %s and relaunch this software."%self.configuration.filename)
   except KeyboardInterrupt, detail:
       debug("Exiting!")
       self.finished = True
예제 #2
0
class ApplicationDownloader(Thread):
    
    ConfigurationCreation = Event()
    LoggerCreation = Event()
    
    ConfigurationCreationCallbacks = []
    LoggerCreationCallbacks = []
    is_running = True
    is_running_cv = threading.Condition()
    httpd = None
    
    def __str__(self):
        return "<ApplicationDownloader instance>"
    
    def __init__(self):
        Thread.__init__(self)
        self.configuration = models.Configuration.ConfigurationFactory().get_config()
        self.logger = Logger()
            
        warn("Software Version: %s"%self.configuration.version)
        #warn("Application __init__: called")

        self.unrar_thread = UnRar()
        self.unrar_thread.start()

    def get_instance(self):
        if instance is None:
            warn("Oooups, application doesn't exist ??!")
            #self.application = Application()
        return instance

    def set_instance(self):
        self.application = self
    
    def get_nntp(self):      
        return self.nntp
    
    def stop_process(self):
        self.is_running = False
        self.is_running_cv.acquire()
        self.is_running_cv.notify()
        self.is_running_cv.release()
        
    def run(self):
        self.main()
        
    def newConfiguration(self, config):
        for callback in self.ConfigurationCreationCallbacks:
            callback(self,config)
    def newLogger(self, logger):
        for callback in self.LoggerCreationCallbacks:
            callback(self,logger)

    def main(self):
      #ApplicationDownloader.ConfigurationCreation.call(self, self.configuration)
      self.newConfiguration(self.configuration)
      #ApplicationDownloader.LoggerCreation.call(self, self.logger)
      self.newLogger(self.logger)
      #return
       
      try:
        if not self.configuration.usenet_server == "news.foobar.com":
            self.nntp = NNTPDownload(self)
            self._nzbparser = NZBParser(self)
          
            HTTPHandler.application = self
            self.httpd = StoppableHTTPServer((self.configuration.local_server_address,self.configuration.local_port), HTTPHandler)
            thread.start_new_thread(self.httpd.serve, ())
            
            self.is_running_cv.acquire()
            enough_time = False
            while self.is_running:
                #warn("ApplicationDownloader thread: running!")
                
                if self.configuration.do_help is True:
                  if self.nntp.enough_time_passed() is True and enough_time is False:
                    enough_time = True
                    warn("enough time passed!")
                    continue
                  if self.nntp.enough_time_passed() is False and enough_time is True:
                    enough_time = False      
                    continue  
                self.is_running_cv.wait(1.0)
            self.is_running_cv.release()
            self.httpd.stop()
            
        else:
            warn("----------------------------------")
            warn("Your configuration file isn't set.")
            warn("Please edit %s and relaunch this software."%self.configuration.filename)
      except KeyboardInterrupt, detail:
          debug("Exiting!")
          self.finished = True

      self.unrar_thread.stop_running()