"""
PySCeS - Python Simulator for Cellular Systems (http://pysces.sourceforge.net)

Copyright (C) 2004-2012 B.G. Olivier, J.M. Rohwer, J.-H.S Hofmeyr all rights reserved,

Brett G. Olivier (bgoli@users.sourceforge.net)
Triple-J Group for Molecular Cell Physiology
Stellenbosch University, South Africa.

Permission to use, modify, and distribute this software is given under the
terms of the PySceS (BSD style) license. See LICENSE.txt that came with
this distribution for specifics.

NO WARRANTY IS EXPRESSED OR IMPLIED.  USE AT YOUR OWN RISK.
Brett G. Olivier
"""

from pysces.version import __version__

__doc__ = '''
            PyscesConfig
            ------------

            This module contains templates for the default configuration files
            on POSIX and WIN32 systems as well as utility functions for reading
            and writing them.
            '''

import ConfigParser
import string
import os

if os.sys.platform == 'win32':
    __DefaultWin = {
        "install_dir"  : os.path.join(os.sys.prefix,'lib','site-packages','pysces'),
        "model_dir"    : "os.path.join(os.getenv('HOMEDRIVE')+os.path.sep,'Pysces','psc')",
        "output_dir"   : "os.path.join(os.getenv('HOMEDRIVE')+os.path.sep,'Pysces')",
        "gnuplot_dir"  : "None",
        "pitcon"       : True,
        "nleq2"        : True,
        "gnuplot"      : False,
        "matplotlib"   : True
        }
    __DefaultWinUsr = {
        "model_dir"    : os.path.join(os.getenv('HOMEDRIVE')+os.path.sep,'Pysces','psc'),
        "output_dir"   : os.path.join(os.getenv('HOMEDRIVE')+os.path.sep,'Pysces'),
        }
else:
    if hasattr(os.sys, 'lib'):
        lib = os.sys.lib
    else:
        lib = 'lib'
    __DefaultPosix = {
        "install_dir"  : os.path.join(os.sys.prefix,lib,"python%d.%d" % tuple(os.sys.version_info[:2]) ,'site-packages','pysces'),
        "model_dir"    : "os.path.join(os.path.expanduser('~'),'Pysces','psc')",
        "output_dir"   : "os.path.join(os.path.expanduser('~'),'Pysces')",
        "gnuplot_dir"  : "None",
        "pitcon"       : True,
        "nleq2"        : True,
        "gnuplot"      : False,
        "matplotlib"   : True
        }
    __DefaultPosixUsr = {
        "model_dir"    : os.path.join(os.path.expanduser('~'),'Pysces','psc'),
        "output_dir"   : os.path.join(os.path.expanduser('~'),'Pysces'),
        }

def ReadConfig(file_path, config={}):
    """
    Read a PySCeS configuration file

    - *file_path*	full path to file
    - *config [default={}]* configuration data
    """
    filein = open(file_path,'r')
    cp = ConfigParser.ConfigParser()
    cp.readfp(filein)
    for sec in cp.sections():
        name = string.lower(sec)
        for opt in cp.options(sec):
            config[string.lower(opt)] = string.strip(cp.get(sec, opt))
    filein.close()
    return config

def WriteConfig(file_path, config={}, section='Pysces'):
    """
    Write a PySCeS configuration file

    - *file_path* full path to file
    - *config* [default={}]: config dictionary
    - *section* [default='Pysces']: default man section name

    """
    cfgfile = open(file_path,'w')
    cp = ConfigParser.ConfigParser()
    cp.add_section(section)
    for key in config:
        cp.set(section,key, config[key])
    cp.write(cfgfile)
    cfgfile.close()
