1"""
2PySCeS - Python Simulator for Cellular Systems (http://pysces.sourceforge.net)
3
4Copyright (C) 2004-2020 B.G. Olivier, J.M. Rohwer, J.-H.S Hofmeyr all rights reserved,
5
6Brett G. Olivier (bgoli@users.sourceforge.net)
7Triple-J Group for Molecular Cell Physiology
8Stellenbosch University, South Africa.
9
10Permission to use, modify, and distribute this software is given under the
11terms of the PySceS (BSD style) license. See LICENSE.txt that came with
12this distribution for specifics.
13
14NO WARRANTY IS EXPRESSED OR IMPLIED.  USE AT YOUR OWN RISK.
15Brett G. Olivier
16"""
17from __future__ import division, print_function
18from __future__ import absolute_import
19from __future__ import unicode_literals
20
21from pysces.version import __version__
22
23__doc__ = '''
24            PyscesConfig
25            ------------
26
27            This module contains templates for the default configuration files
28            on POSIX and WIN32 systems as well as utility functions for reading
29            and writing them.
30            '''
31
32try:
33    import configparser  # Py 3
34except ImportError:
35    import ConfigParser as configparser   # Py 2
36
37import string
38import os
39
40if os.sys.platform == 'win32':
41    __DefaultWin = {
42        "install_dir"  : os.path.join(os.sys.prefix,'lib','site-packages','pysces'),
43        "model_dir"    : os.path.join(os.getenv('USERPROFILE'),'Pysces','psc'),
44        "output_dir"   : os.path.join(os.getenv('USERPROFILE'),'Pysces'),
45        "gnuplot_dir"  : None,
46        "pitcon"       : True,
47        "nleq2"        : True,
48        "gnuplot"      : False,
49        "matplotlib"   : True,
50        "matplotlib_backend"   : 'TKagg',
51        "silentstart"  : False
52        }
53    __DefaultWinUsr = {
54        "model_dir"    : os.path.join(os.getenv('USERPROFILE'),'Pysces','psc'),
55        "output_dir"   : os.path.join(os.getenv('USERPROFILE'),'Pysces'),
56        "silentstart"  : False
57        }
58else:
59    if hasattr(os.sys, 'lib'):
60        lib = os.sys.lib
61    else:
62        lib = 'lib'
63    __DefaultPosix = {
64        "install_dir"  : os.path.join(os.sys.prefix,lib,"python%d.%d" % tuple(os.sys.version_info[:2]) ,'site-packages','pysces'),
65        "model_dir"    : os.path.join(os.path.expanduser('~'),'Pysces','psc'),
66        "output_dir"   : os.path.join(os.path.expanduser('~'),'Pysces'),
67        "gnuplot_dir"  : None,
68        "pitcon"       : True,
69        "nleq2"        : True,
70        "gnuplot"      : False,
71        "matplotlib"   : True,
72        "matplotlib_backend"   : 'TKagg',
73        "silentstart"  : False
74        }
75    __DefaultPosixUsr = {
76        "model_dir"    : os.path.join(os.path.expanduser('~'),'Pysces','psc'),
77        "output_dir"   : os.path.join(os.path.expanduser('~'),'Pysces'),
78        "silentstart"  : False
79        }
80    # OSX patch by AF
81    if os.sys.platform == 'darwin':
82        __DefaultPosix["matplotlib_backend"] = 'MacOSX'
83
84def ReadConfig(file_path, config={}):
85    """
86    Read a PySCeS configuration file
87
88    - *file_path*	full path to file
89    - *config [default={}]* configuration data
90    """
91    filein = open(file_path,'r')
92    cp = configparser.ConfigParser()
93    cp.readfp(filein)
94    for sec in cp.sections():
95        name = sec.lower()
96        for opt in cp.options(sec):
97            config[opt.lower()] = cp.get(sec, opt).strip()
98    filein.close()
99    return config
100
101def WriteConfig(file_path, config={}, section='Pysces'):
102    """
103    Write a PySCeS configuration file
104
105    - *file_path* full path to file
106    - *config* [default={}]: config dictionary
107    - *section* [default='Pysces']: default man section name
108
109    """
110    cfgfile = open(file_path,'w')
111    cp = configparser.ConfigParser()
112    cp.add_section(section)
113    for key in config:
114        cp.set(section, key, str(config[key]))
115    cp.write(cfgfile)
116    cfgfile.close()
117