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

  1. # The contents of this file are subject to the BitTorrent Open Source License
  2. # Version 1.0 (the License).  You may not copy or use this file, in either
  3. # source code or executable form, except in compliance with the License.  You
  4. # may obtain a copy of the License at http://www.bittorrent.com/license/.
  5. #
  6. # Software distributed under the License is distributed on an AS IS basis,
  7. # WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the License
  8. # for the specific language governing rights and limitations under the
  9. # License.
  10.  
  11. # Written by Uoti Urpala
  12.  
  13. import config
  14. import os
  15. import sys
  16. # Python 2.2 doesn't have RawConfigParser
  17. try:
  18.     from ConfigParser import RawConfigParser
  19. except ImportError:
  20.     from ConfigParser import ConfigParser as RawConfigParser
  21.  
  22. from BitTorrent import parseargs
  23. from BitTorrent import ERROR
  24. from BitTorrent import version
  25. from __init__ import get_config_dir
  26.  
  27. def get_config(defaults, section):
  28.     # GRS: Get our state directory by asking DTV for it
  29. #    dir_root = get_config_dir()
  30. #
  31. #    if dir_root is None:
  32. #        return {}
  33. #
  34. #    configdir = os.path.join(dir_root, 'bittorrent-dtv')
  35.     configdir = os.path.join(config.get(config.SUPPORT_DIRECTORY),
  36.                              'Bittorrent')
  37.  
  38.     if not os.path.isdir(configdir):
  39.         try:
  40.             os.mkdir(configdir, 0700)
  41.         except:
  42.             pass
  43.  
  44.     p = RawConfigParser()
  45.     p.read(os.path.join(configdir, 'config'))
  46.     values = {}
  47.     if p.has_section(section):
  48.         for name, value in p.items(section):
  49.             if name in defaults:
  50.                 values[name] = value
  51.     if p.has_section('common'):
  52.         for name, value in p.items('common'):
  53.             if name in defaults and name not in values:
  54.                 values[name] = value
  55.     if defaults.get('data_dir') == '' and \
  56.            'data_dir' not in values and os.path.isdir(configdir):
  57.         datadir = os.path.join(configdir, 'data')
  58.         values['data_dir'] = datadir
  59.     parseargs.parse_options(defaults, values)
  60.     return values
  61.  
  62.  
  63. def save_ui_config(defaults, section, save_options, error_callback):
  64.     p = RawConfigParser()
  65.     filename = os.path.join(defaults['data_dir'], 'ui_config')
  66.     p.read(filename)
  67.     p.remove_section(section)
  68.     p.add_section(section)
  69.     for name in save_options:
  70.         p.set(section, name, defaults[name])
  71.     try:
  72.         f = file(filename, 'w')
  73.         p.write(f)
  74.         f.close()
  75.     except Exception, e:
  76.         try:
  77.             f.close()
  78.         except:
  79.             pass
  80.         error_callback(ERROR, 'Could not permanently save options: '+
  81.                        str(e))
  82.  
  83.  
  84. def parse_configuration_and_args(defaults, uiname, arglist=[], minargs=0,
  85.                                  maxargs=0):
  86.     defconfig = dict([(name, value) for (name, value, doc) in defaults])
  87.     if arglist[0:] == ['--version']:
  88.         print version
  89.         sys.exit(0)
  90.  
  91.     if arglist[0:] in (['--help'], ['-h'], ['--usage'], ['-?']): 
  92.         parseargs.printHelp(uiname, defaults)
  93.         sys.exit(0)
  94.     
  95.     presets = get_config(defconfig, uiname)
  96.     config, args = parseargs.parseargs(arglist, defaults, minargs, maxargs,
  97.                                        presets)
  98.     datadir = config['data_dir']
  99.     if datadir:
  100.         if uiname in ('btdownloadgui', 'btmaketorrentgui'):
  101.             p = RawConfigParser()
  102.             values = {}
  103.             p.read(os.path.join(datadir, 'ui_config'))
  104.             if p.has_section(uiname):
  105.                 for name, value in p.items(uiname):
  106.                     if name in defconfig:
  107.                         values[name] = value
  108.             parseargs.parse_options(defconfig, values)
  109.             presets.update(values)
  110.             config, args = parseargs.parseargs(arglist, defaults, minargs,
  111.                                                maxargs, presets)
  112.         rdir = os.path.join(datadir, 'resume')
  113.         mdir = os.path.join(datadir, 'metainfo')
  114.         try:
  115.             if not os.path.exists(datadir):
  116.                 os.mkdir(datadir, 0700)
  117.             if not os.path.exists(mdir):
  118.                 os.mkdir(mdir, 0700)
  119.             if not os.path.exists(rdir):
  120.                 os.mkdir(rdir, 0700)
  121.         except:
  122.             pass
  123.     return config, args
  124.