Source code for irfpy.util.pyanarc

''' Configure file support for irfpy library.

.. codeauthor:: Yoshifumi Futaana

.. warning::

    This module (:mod:`irfpy.util.pyanarc`) is deprecated.
    Use the compatible module :mod:`irfpy.util.irfpyrc`.

Configure file is used to make programs portable.
For example, consider the case you want to access a local folder,
where important data file locates.
The location of the folder can differ computer by computer.
Thus, the location is *not* recommended to be hard-coded into any program.
In such case, by preparing a configure file computer by computer, and
specify the path name in the file.

See more detailed example in :ref:`recipe_irfpyrc`.

*History*

- From v4.0.2, this module (:mod:`irfpy.util.pyanarc`) is deprecated.
  Use the compatible module :mod:`irfpy.util.irfpyrc`.

- From v1.8, using :class:`irfpy.util.pyanarc.RcFile` is not recommended.
  Use :class:`irfpy.util.pyanarc.Rc` class instead.

'''
import os

import logging
_logger = logging.getLogger('pyanarc')
import configparser
import warnings

warnings.warn('''This module (:mod:`irfpy.util.pyanarc`) is deprecated. Use the compatible
module :mod:`irfpy.util.irfpyrc`.''', DeprecationWarning)

[docs]class RcFile: ''' Class treating individual configure file. Class treating individual configure file. From v1.8, the use of RcFile explicitly is **not** recommended, instead, :class:`Rc` class is recommended. :class:`Rc` handles multiple configure file at once. ''' logger = logging.getLogger(__name__ + '.RcFile') def __init__(self, name=None, deprecatedwarn=True): ''' Open a configure file. :param name: Name of the RC file. Default is ${HOME}/.pyanarc ''' if deprecatedwarn: warnings.warn('Direct use of RcFile is outdated. Use Rc class.', category=DeprecationWarning) if name is None: name = os.path.join(os.getenv('HOME'), '.pyanarc') self.logger.debug('Config file name = %s' % name) self.config = configparser.SafeConfigParser() self.name = name if os.path.exists(name): self.logger.info('Config file %s loaded.' % name) self.config.read(name) else: self.logger.warning('Config file %s not found. Using default values.' % name)
[docs] def get(self, subproject, parameter): ''' Get a value of the specified parameter of particular subproject. ''' try: val = self.config.get(subproject, parameter) except (configparser.NoOptionError, configparser.NoSectionError) as e: # self.logger.warn('[%s] %s not found in %s. None returned.' % # (subproject, parameter, self.name)) val = None return val
import irfpy.util.irfpyrc
[docs]class Rc(irfpy.util.irfpyrc.Rc): ''' (Deprecated) Handles configuration files for pyana projects. .. warning:: Use :class:`irfpy.util.irfpyrc.Rc` class. This class is kept just for compatibility for existing codes. ''' def __init__(self, verbose=False): warnings.warn('\n'.join([ 'Using irfpy.util.pyanarc.Rc class is not recommended.' 'Use irfpy.util.irfpyrc.Rc which has compatible functionality.' 'Only the change is remove "verbose" keyword.' ]), DeprecationWarning) irfpy.util.irfpyrc.Rc.__init__(self) @property def rcfilename(self): return [f.name for f in self.rclist]