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

  1. from threading import RLock
  2. import os
  3. import platformcfg
  4. import util
  5. import resource
  6.  
  7. __appConfig = None
  8. __data = None
  9. __lock = RLock()
  10. __callbacks = set()
  11.  
  12. class Pref:
  13.     def __init__(self, **kwds):
  14.         self.__dict__.update(kwds)
  15.  
  16.     def __eq__(self, other):
  17.         return self.key == other.key
  18.     def __ne__(self, other):
  19.         return self.key != other.key
  20.  
  21. # These are normal user preferences.
  22. MAIN_WINDOW_FRAME           = Pref( key='mainWindowFrame',       default=None,  platformSpecific=False )
  23. LEFT_VIEW_SIZE              = Pref( key='leftViewSize',          default=None,  platformSpecific=False )
  24. RIGHT_VIEW_SIZE             = Pref( key='rightViewSize',         default=None,  platformSpecific=False )
  25. RUN_DTV_AT_STARTUP          = Pref( key='runAtStartup',          default=False, platformSpecific=False )
  26. CHECK_CHANNELS_EVERY_X_MN   = Pref( key='checkChannelsEveryXMn', default=60,    platformSpecific=False )
  27. LIMIT_UPSTREAM              = Pref( key='limitUpstream',         default=True,  platformSpecific=False )
  28. UPSTREAM_LIMIT_IN_KBS       = Pref( key='upstreamLimitInKBS',    default=4,     platformSpecific=False )
  29. PRESERVE_DISK_SPACE         = Pref( key='preserveDiskSpace',     default=False, platformSpecific=False )
  30. PRESERVE_X_GB_FREE          = Pref( key='preserveXGBFree',       default=1,     platformSpecific=False )
  31. EXPIRE_AFTER_X_DAYS         = Pref( key='expireAfterXDays',      default=6,     platformSpecific=False )
  32. DOWNLOADS_TARGET            = Pref( key='DownloadsTarget',       default=3,     platformSpecific=False )
  33. MAX_MANUAL_DOWNLOADS        = Pref( key='MaxManualDownloads',    default=10,    platformSpecific=False )
  34. VOLUME_LEVEL                = Pref( key='VolumeLevel',           default=1.0,   platformSpecific=False )
  35.  
  36. # These have a hardcoded default which can be overridden by setting an
  37. # environment variable.
  38.  
  39. if 'DTV_CHANNELGUIDE_URL' in os.environ:
  40.     effectiveChannelGuide = os.environ['DTV_CHANNELGUIDE_URL']
  41. else:
  42.     effectiveChannelGuide = 'https://channelguide.participatoryculture.org/'
  43.  
  44. if 'DTV_VIDEOBOMB_URL' in os.environ:
  45.     effectiveVideobomb = os.environ['DTV_VIDEOBOMB_URL']
  46. else:
  47.     effectiveVideobomb = 'http://www.videobomb.com/api/submit_or_bomb'
  48.  
  49. if 'DTV_AUTOUPDATE_URL' in os.environ:
  50.     effectiveAutoupdate = os.environ['DTV_AUTOUPDATE_URL']
  51. else:
  52.     effectiveAutoupdate = 'http://www.participatoryculture.org/democracy-version.xml'
  53.  
  54. CHANNEL_GUIDE_URL = Pref(key='ChannelGuideURL', default=effectiveChannelGuide,
  55.                          platformSpecific=False)
  56. VIDEOBOMB_URL     = Pref(key='VideobombURL',    default=effectiveVideobomb,
  57.                          platformSpecific=False)
  58. AUTOUPDATE_URL    = Pref(key='AutoupdateURL',   default=effectiveAutoupdate,
  59.                          platformSpecific=False)
  60.  
  61. # These are computed by special platform code.
  62. RUN_AT_STARTUP     = Pref( key='RunAtStartup',  default=False,  platformSpecific=True )
  63. MOVIES_DIRECTORY = \
  64.     Pref(key='MoviesDirectory',  default=None, platformSpecific=True)
  65. SUPPORT_DIRECTORY = \
  66.     Pref(key='SupportDirectory', default=None, platformSpecific=True)
  67. DB_PATHNAME = \
  68.     Pref(key='DBPathname',       default=None, platformSpecific=True)
  69. LOG_PATHNAME = \
  70.     Pref(key='LogPathname',      default=None, platformSpecific=True)
  71. DOWNLOADER_LOG_PATHNAME = \
  72.     Pref(key='DownloaderLogPathname', default=None, platformSpecific=True)
  73.  
  74. # These are normally read from resources/app.config.
  75. SHORT_APP_NAME = \
  76.     Pref(key='shortAppName', default=None, platformSpecific=False)
  77. LONG_APP_NAME = \
  78.     Pref(key='longAppName',  default=None, platformSpecific=False)
  79. PROJECT_URL = \
  80.     Pref(key='projectURL',   default=None, platformSpecific=False)
  81. PUBLISHER = \
  82.     Pref(key='publisher',    default=None, platformSpecific=False)
  83. APP_VERSION = \
  84.     Pref(key='appVersion',   default=None, platformSpecific=False)
  85. APP_REVISION = \
  86.     Pref(key='appRevision',  default=None, platformSpecific=False)
  87. APP_PLATFORM = \
  88.     Pref(key='appPlatform',  default=None, platformSpecific=False)
  89. APP_SERIAL = \
  90.     Pref(key='appSerial-unknown',    default="0", platformSpecific=False)
  91.  
  92. def addChangeCallback(callback):
  93.     __callbacks.add(callback)
  94.  
  95. def removeChangeCallback(callback):
  96.     __callbacks.discard(callback)
  97.  
  98. def load():
  99.     global __data
  100.     __lock.acquire()
  101.     try:
  102.         # There's some sleight-of-hand here. The Windows port needs to
  103.         # look up config.LONG_APP_NAME and config.PUBLISHER to compute
  104.         # the path to the data file that is read when load() is
  105.         # called. Setting __appConfig to a true value (and populating
  106.         # it with those keys) before calling load() ensures that (a)
  107.         # the values will be available and (b) we won't get caught in
  108.         # an infinite loop of load()s. But in general, you shouldn't
  109.         # call config.get() or config.set() from platformcfg.load()
  110.         # unless you know exactly what you are doing, and maybe not
  111.         # even then.
  112.         global __appConfig
  113.         __appConfig = util.readSimpleConfigFile(resource.path('app.config'))
  114.  
  115.         __data = platformcfg.load()
  116.         if __data is None:
  117.             __data = dict()
  118.  
  119.         # This is a bit of a hack to automagically get the serial
  120.         # number for this platform
  121.         APP_SERIAL.key = ('appSerial-%s' % get(APP_PLATFORM))
  122.  
  123.     finally:
  124.         __lock.release()
  125.  
  126. def save():
  127.     __lock.acquire()
  128.     try:
  129.         __checkValidity()
  130.         platformcfg.save( __data )
  131.     finally:
  132.         __lock.release()
  133.  
  134. def get(descriptor):
  135.     __lock.acquire()
  136.     try:
  137.         __checkValidity()
  138.  
  139.         if descriptor.platformSpecific:
  140.             return platformcfg.get(descriptor)
  141.         elif descriptor.key in __appConfig:
  142.             return __appConfig[descriptor.key]
  143.         else:
  144.             return __data.get(descriptor.key, descriptor.default)
  145.     finally:
  146.         __lock.release()
  147.  
  148. def getAppConfig():
  149.     __lock.acquire()
  150.     try:
  151.         __checkValidity()
  152.         return __appConfig.copy()
  153.     finally:
  154.         __lock.release()
  155.     
  156. def set(descriptor, value):
  157.     __lock.acquire()
  158.     try:
  159.         __checkValidity()
  160.         __data[ descriptor.key ] = value
  161.         __notifyListeners(descriptor.key, value)
  162.     finally:
  163.         __lock.release()
  164.  
  165. def __checkValidity():
  166.     if __appConfig == None:
  167.         load()
  168.  
  169. def __notifyListeners(key, value):
  170.     for callback in __callbacks:
  171.         callback(key, value)
  172.  
  173.  
  174. def ensureMigratedMoviePath(pathname):
  175.     if hasattr(platformcfg, 'ensureMigratedMoviePath'):
  176.         pathname = platformcfg.ensureMigratedMoviePath(pathname)
  177.     return pathname
  178.  
  179. # Hack. Getting the support directory path here forces it to be created at
  180. # import time (ie, before the application standard startup sequence is running)
  181. # which solves bug #1260994: the bittorent-dtv folder won't end up at the
  182. # root of the user's home folder. Warning, 'config' MUST be import BEFORE
  183. # 'downloader' in app.py for this to work.
  184. get(SUPPORT_DIRECTORY)
  185.