#! /usr/bin/env python
# Last Change: Sun Jan 06 09:00 PM 2008 J

# Module for support to look for external code (replacement of
# numpy.distutils.system_info). KEEP THIS INDEPENDANT OF SCONS !
import os
import ConfigParser

from numscons.numdist import default_lib_dirs, default_include_dirs, \
                             default_src_dirs, get_standard_file

# Think about a cache mechanism, to avoid reparsing the config file everytime.
def get_config():
    """ This tries to read .cfg files in several locations, and merge its
    information into a ConfigParser object for the first found file.
    
    Returns the ConfigParser instance. This copies the logic in system_info
    from numpy.distutils."""
    # Below is the feature we are copying from numpy.distutils:
    # 
    # The file 'site.cfg' is looked for in

    # 1) Directory of main setup.py file being run.
    # 2) Home directory of user running the setup.py file as ~/.numpy-site.cfg
    # 3) System wide directory (location of this file...)

    # The first one found is used to get system configuration options The
    # format is that used by ConfigParser (i.e., Windows .INI style). The
    # section DEFAULT has options that are the default for each section. The
    # available sections are fftw, atlas, and x11. Appropiate defaults are
    # used if nothing is specified.

    section = 'DEFAULT'
    defaults = {}
    defaults['libraries'] = ''
    defaults['library_dirs'] = os.pathsep.join(default_lib_dirs)
    defaults['include_dirs'] = os.pathsep.join(default_include_dirs)
    defaults['src_dirs'] = os.pathsep.join(default_src_dirs)
    cp = ConfigParser.ConfigParser(defaults)
    files = []
    files.extend(get_standard_file('.numpy-site.cfg'))
    files.extend(get_standard_file('site.cfg'))

    def parse_config_files():
        cp.read(files)
        if not cp.has_section(section):
            cp.add_section(section)

    parse_config_files()
    return cp, files

def parse_config_param(var):
    """Given var, the output of ConfirParser.get(section, name), returns a list
    of each item of its content."""
    varl = var.split(',')
    return [i.strip() for i in varl]

def get_paths(var):
    """Given var, the output of ConfirParser.get(section, name), returns a list
    of each item of its content, assuming the content is a list of directoris.
    
    Example: if var is foo:bar, it will return ['foo', 'bar'] on posix."""
    return var.split(os.pathsep)

def get_config_from_section(siteconfig, section):
    """For the given siteconfig and section, return the found information.
    
    Returns a tuple (info, found), where:
        info : tuple (cpppath, libs, libpath), containing a list of path or libraries
        found: 1 if the section was found, 0 otherwise."""
    if siteconfig.has_section(section):
        try:
            libpath = get_paths(siteconfig.get(section, 'library_dirs'))
        except ConfigParser.NoOptionError, e:
            libpath = []

        try:
            cpppath = get_paths(siteconfig.get(section, 'include_dirs'))
        except ConfigParser.NoOptionError, e:
            cpppath = []

        try:
            libs = parse_config_param(siteconfig.get(section, 'libraries'))
        except ConfigParser.NoOptionError, e:
            libs = []
        return (cpppath, libs, libpath), 1
    else:
        return ([], [], []), 0

@@ -353,16 +353,736 @@
 ard_file
+%0Afrom numscons.core.utils import DefaultDict%0A%0A# List of options that ConfigOpts can keep. If later additional variables%0A# should be added (e.g. cpp flags, etc...), they should be added here.%0A_SITE_OPTS_FLAGS = ('libraries', 'library_dirs', 'include_dirs')%0A%0Aclass ConfigOpts(DefaultDict):%0A    %22%22%22Small container class to keep all necessary options to build with a%0A    given library/package, such as cflags, libs, library paths, etc...%22%22%22%0A    _keys = _SITE_OPTS_FLAGS%0A    def __init__(self):%0A        DefaultDict.__init__(self, BuildOpts._keys)%0A        for k in _keys:%0A            self%5Bk%5D = %5B%5D%0A%0A    def __repr__(self):%0A        msg = %5Br'%25s : %25s' %25 (k, i) for k, i in self.items() if len(i) %3E 0%5D%0A        return '%5Cn'.join(msg)
 %0A%0A# Thin

Add a ConfigOpts class, which will be used instead of lists for site.cfg options