Commit 4d9f3a73 authored by Patrick Shriwise's avatar Patrick Shriwise
Browse files

Adding configuration file and value check module.

parent 186ed7e4
Loading
Loading
Loading
Loading

pygriffin/__init__.py

0 → 100644
+1 −0
Original line number Diff line number Diff line
__version__ = '0.0.1'
 No newline at end of file
+0 −0

Empty file added.

pygriffin/config.py

0 → 100644
+82 −0
Original line number Diff line number Diff line
import os
from pathlib import Path

try:
    from ConfigParser import ConfigParser
except ImportError:
    from configparser import ConfigParser

import checkvalue as cv

"""
Module for resolving configuation variables of PyGriffin
"""

class PyGriffinConfigParser():
    """
    Class to manage PyGriffin's configuration

    Parameters
    ----------
    griffin_path : str (default None)
        Path to the Griffin executable
    """

    __MOD_PATH = os.path.abspath(os.path.dirname(__file__))
    __RC_FILE = __MOD_PATH + '/.pygriffin.rc'

    def __init__(self, griffin_exec=None):

        # always read the config file,
        # even if we don't use it
        self.__config = ConfigParser()
        self.__config.read(self.__RC_FILE)

        # if the Griffin path parameter is set, use that
        if griffin_exec is not None:
            self.griffin_exec = griffin_exec
        # next, try the GRIFFFIN_EXEC environment variable
        elif os.getenv('GRIFFIN_EXEC') is not None:
            self.griffin_dir = os.getenv('GRIFFIN_EXEC')
        elif self.get('griffin_exec'):
            self.griffin_exec = self.get('griffin_exec')
        else:
            msg = ('All methods of determining the location '
            'of the Griffin executable have failed.')
            raise RuntimeError(msg)

    def get(self, *args, **kwargs):
        """
        Attempts to get a configuration option from the .rc file.
        If an exeption is raised, the provided default value will be returned.
        If no default value is provided, None is returned.
        """
        default_val = kwargs.pop('default', None)
        if self.__config.has_option(*args, **kwargs):
            return self.__config.get(*args, **kwargs)
        else:
            return default_val

    @property
    def griffin_dir(self):
        return self.griffin_exec.parent

    @property
    def griffin_exec(self):
        return self._griffin_exec

    @griffin_exec.setter
    def griffin_exec(self, exec):
        cv.check_type('Griffin executable path', exec, (Path, str))
        self._griffin_exec = Path(exec).absolute()

    @property
    def module_path(self):
        return config.__MOD_PATH

    @property
    def rc_file(self):
        return self.__RC_FILE

# this is provided by importing this file elsewhere
PyGriffinConfig = PyGriffinConfig()
 No newline at end of file