home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2420 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  5.4 KB  |  136 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import distutils
  5. import os
  6. from setuptools import Command
  7. from distutils.util import convert_path
  8. from distutils import log
  9. from distutils.errors import *
  10. __all__ = [
  11.     'config_file',
  12.     'edit_config',
  13.     'option_base',
  14.     'setopt']
  15.  
  16. def config_file(kind = 'local'):
  17.     if kind == 'local':
  18.         return 'setup.cfg'
  19.     if kind == 'global':
  20.         return os.path.join(os.path.dirname(distutils.__file__), 'distutils.cfg')
  21.     if kind == 'user':
  22.         if not os.name == 'posix' or '.':
  23.             pass
  24.         dot = ''
  25.         return os.path.expanduser(convert_path('~/%spydistutils.cfg' % dot))
  26.     raise ValueError("config_file() type must be 'local', 'global', or 'user'", kind)
  27.  
  28.  
  29. def edit_config(filename, settings, dry_run = False):
  30.     RawConfigParser = RawConfigParser
  31.     import ConfigParser
  32.     log.debug('Reading configuration from %s', filename)
  33.     opts = RawConfigParser()
  34.     opts.read([
  35.         filename])
  36.     for section, options in settings.items():
  37.         if options is None:
  38.             log.info('Deleting section [%s] from %s', section, filename)
  39.             opts.remove_section(section)
  40.             continue
  41.         if not opts.has_section(section):
  42.             log.debug('Adding new section [%s] to %s', section, filename)
  43.             opts.add_section(section)
  44.         
  45.         for option, value in options.items():
  46.             if value is None:
  47.                 log.debug('Deleting %s.%s from %s', section, option, filename)
  48.                 opts.remove_option(section, option)
  49.                 if not opts.options(section):
  50.                     log.info('Deleting empty [%s] section from %s', section, filename)
  51.                     opts.remove_section(section)
  52.                 
  53.             opts.options(section)
  54.             log.debug('Setting %s.%s to %r in %s', section, option, value, filename)
  55.             opts.set(section, option, value)
  56.         
  57.     
  58.     log.info('Writing %s', filename)
  59.     if not dry_run:
  60.         f = open(filename, 'w')
  61.         opts.write(f)
  62.         f.close()
  63.     
  64.  
  65.  
  66. class option_base(Command):
  67.     user_options = [
  68.         ('global-config', 'g', 'save options to the site-wide distutils.cfg file'),
  69.         ('user-config', 'u', "save options to the current user's pydistutils.cfg file"),
  70.         ('filename=', 'f', 'configuration file to use (default=setup.cfg)')]
  71.     boolean_options = [
  72.         'global-config',
  73.         'user-config']
  74.     
  75.     def initialize_options(self):
  76.         self.global_config = None
  77.         self.user_config = None
  78.         self.filename = None
  79.  
  80.     
  81.     def finalize_options(self):
  82.         filenames = []
  83.         if self.global_config:
  84.             filenames.append(config_file('global'))
  85.         
  86.         if self.user_config:
  87.             filenames.append(config_file('user'))
  88.         
  89.         if self.filename is not None:
  90.             filenames.append(self.filename)
  91.         
  92.         if not filenames:
  93.             filenames.append(config_file('local'))
  94.         
  95.         if len(filenames) > 1:
  96.             raise DistutilsOptionError('Must specify only one configuration file option', filenames)
  97.         len(filenames) > 1
  98.         (self.filename,) = filenames
  99.  
  100.  
  101.  
  102. class setopt(option_base):
  103.     description = 'set an option in setup.cfg or another config file'
  104.     user_options = [
  105.         ('command=', 'c', 'command to set an option for'),
  106.         ('option=', 'o', 'option to set'),
  107.         ('set-value=', 's', 'value of the option'),
  108.         ('remove', 'r', 'remove (unset) the value')] + option_base.user_options
  109.     boolean_options = option_base.boolean_options + [
  110.         'remove']
  111.     
  112.     def initialize_options(self):
  113.         option_base.initialize_options(self)
  114.         self.command = None
  115.         self.option = None
  116.         self.set_value = None
  117.         self.remove = None
  118.  
  119.     
  120.     def finalize_options(self):
  121.         option_base.finalize_options(self)
  122.         if self.command is None or self.option is None:
  123.             raise DistutilsOptionError('Must specify --command *and* --option')
  124.         self.option is None
  125.         if self.set_value is None and not (self.remove):
  126.             raise DistutilsOptionError('Must specify --set-value or --remove')
  127.         not (self.remove)
  128.  
  129.     
  130.     def run(self):
  131.         edit_config(self.filename, {
  132.             self.command: {
  133.                 self.option.replace('-', '_'): self.set_value } }, self.dry_run)
  134.  
  135.  
  136.