home *** CD-ROM | disk | FTP | other *** search
/ Chip 2006 June / CHIP 2006-06.2.iso / program / freeware / Democracy-0.8.2.exe / xulrunner / python / dl_daemon / command.py < prev    next >
Encoding:
Python Source  |  2006-04-10  |  4.2 KB  |  134 lines

  1. import random
  2. import cPickle
  3. import traceback
  4. from time import sleep
  5.  
  6. class Command:
  7.     def __init__(self, daemon, *args, **kws):
  8.         self.id = "cmd%08d" % random.randint(0,99999999)
  9.         self.orig = True
  10.         self.args = args
  11.         self.kws = kws
  12.         self.daemon = daemon
  13.  
  14.     def setDaemon(self, daemon):
  15.         self.daemon = daemon
  16.  
  17.     def send(self, block = True, retry = True):
  18.         while not self.daemon.shutdown:
  19.             try:
  20.                 return self.daemon.send(self, block)
  21.             except:
  22.                 if retry:
  23.                     print "dtv: dl daemon retrying %s %s" % (str(self), self.id)
  24.                     print "orig is ", self.orig
  25.                     traceback.print_exc()
  26.                     sleep(5)
  27.                 else:
  28.                     #print "dtv: dl daemon send failed %s %s" % (str(self), self.id)
  29.                     break
  30.  
  31.     def setReturnValue(self, ret):
  32.         self.orig = False
  33.         self.ret = ret
  34.  
  35.     def getReturnValue(self):
  36.         return self.ret
  37.  
  38.     def action(self):
  39.         print "WARNING: no action defined for command %s" % self.id
  40.         #for overrriding
  41.  
  42.     def __getstate__(self):
  43.         out = {"id":self.id, "args":self.args, "kws":self.kws, "orig":self.orig}
  44.         try:
  45.             out["ret"] = self.ret
  46.         except:
  47.             pass
  48.         return out
  49.  
  50.     def __setstate__(self, data):
  51.         self.id = data["id"]
  52.         self.kws = data["kws"]
  53.         self.args = data["args"]
  54.         self.orig = data["orig"]
  55.         try:
  56.             self.ret = data["ret"]
  57.         except:
  58.             pass
  59.  
  60. #############################################################################
  61. #  Downloader to App commands                                               #
  62. #############################################################################
  63. class GetConfigCommand(Command):
  64.     def action(self):
  65.         import config
  66.         return config.get(*self.args, **self.kws)
  67.  
  68. class GetResourcePathCommand(Command):
  69.     def action(self):
  70.         import resource
  71.         return resource.path(*self.args, **self.kws)
  72.  
  73. class GetResourceURLCommand(Command):
  74.     def action(self):
  75.         import resource
  76.         return resource.url(*self.args, **self.kws)
  77.  
  78. class FindHTTPAuthCommand(Command):
  79.     def action(self):
  80.         import downloader
  81.         return downloader.findHTTPAuth(*self.args, **self.kws)
  82.  
  83. class UpdateDownloadStatus(Command):
  84.     def action(self):
  85.         from downloader import RemoteDownloader
  86.         return RemoteDownloader.updateStatus(*self.args, **self.kws)
  87.  
  88. class ReadyCommand(Command):
  89.     def action(self):
  90.         self.daemon.ready.set() # go
  91.  
  92. #############################################################################
  93. #  App to Downloader commands                                               #
  94. #############################################################################
  95. class StartNewDownloadCommand(Command):
  96.     def action(self):
  97.         from dl_daemon import download
  98.         return download.startNewDownload(*self.args, **self.kws)
  99.  
  100. class StartDownloadCommand(Command):
  101.     def action(self):
  102.         from dl_daemon import download
  103.         return download.startDownload(*self.args, **self.kws)
  104.  
  105. class PauseDownloadCommand(Command):
  106.     def action(self):
  107.         from dl_daemon import download
  108.         return download.pauseDownload(*self.args, **self.kws)
  109.  
  110. class StopDownloadCommand(Command):
  111.     def action(self):
  112.         from dl_daemon import download
  113.         return download.stopDownload(*self.args, **self.kws)
  114.  
  115. class GetDownloadStatusCommand(Command):
  116.     def action(self):
  117.         from dl_daemon import download
  118.         return download.getDownloadStatus(*self.args, **self.kws)
  119.  
  120. class RestoreDownloaderCommand(Command):
  121.     def action(self):
  122.         from dl_daemon import download
  123.         return download.restoreDownloader(*self.args, **self.kws)
  124.  
  125. class GenerateDownloadID(Command):
  126.     def action(self):
  127.         from dl_daemon import download
  128.         return download.generateDownloadID()
  129.  
  130. # This is a special command that's trapped by the daemon
  131. class ShutDownCommand(Command):
  132.     def action(self):
  133.         pass
  134.