irfpy.util.irfpyrc

Configure file support for irfpy library.

Code author: Yoshifumi Futaana

Changed in version 4.4.0: The priority order of the setup file in Rc has been changed.

Configure files are used to make irfpy library portable. For example, consider cases you want to access a local file, for example, a data file. The location of the data file may differ user by user, or even computer by computer. Thus, the location is not recommended to be hard-coded into any library code.

Instead, a configuration file should be used.

See more detailed example in How to use configuration files.

Rc([load_default, raises])

Rc class represents the configuration files.

class irfpy.util.irfpyrc.Rc(load_default=True, raises=False)[source]

Bases: object

Rc class represents the configuration files.

Library developer may instance this class to use the configuration system for irfpy. By default, the following files are loaded in the priority order.

Changed in version 4.4.0: The priority order is changed.

At the current directory:

  • ./.irfpyrc – Recommended for Mac/Linux users

  • ./irfpy.ini – Recommended for Windows/Mac/Linux users

  • ./irfpy.rc

  • ./.pyanarc – Not recommended. Will be deprecated.

At the home directory (for Mac/Linux):

  • ${HOME}/.irfpyrc – Recommended for Mac/Linux users

  • ${HOME}/irfpy.ini – Recommended for Mac/Linux users

  • ${HOME}/irfpy.rc

  • ${HOME}/.pyanarc – Not recommended. Will be deprecated.

At the home directory (for Windows):

  • %USERPROFILE%/.irfpyrc

  • %USERPROFILE%/irfpy.ini – Recommended for Windows users

  • %USERPROFILE%/irfpy.rc

  • %USERPROFILE%/.pyanarc – Not recommended. Will be deprecated.

Here ${HOME} represents the home folder for Mac/Linux (e.g. /home/username or /Users/username) and %USERPROFILE% is the home directory for Windows (e.g. /Users/username).

The usual exercise of using the configure files are:

>>> rc = Rc()
>>> rc.get('subproject', 'option')

If no entry is found for all the configure files, None is returned. Instead, ou can specify the default keyword; then the returned will be the default if there is no relevant entry.

>>> prm = rc.get('subproject', 'option', default="https://irfpy.irf.se/")
>>> print(prm)
https://irfpy.irf.se/

If you want to add a file into the configure list, you can use append() method. In this case, the specified file is added as the most prioritized configure file.

>>> if os.path.exists('rcfile/pyanarc.special'):
...     rc.append('rcfile/pyanarc.special')

If you do not want to load the configure file, you may use erase() method. This will clear the loaded configure files.

>>> rc.erase()
>>> print(len(rc))
0
Parameters
  • raises – Raises exception if the entry is not found.

  • load_default – Load default files. If False, no files are loaded. You may use append() method to load manually.

append(filename, first=True)[source]

Append the specified file name to the configure file list

Append the specified file name to the configure file list.

Parameters
  • filename – Configure file name.

  • first – If True (default), the configure file is added to the top of the list. If False, the configure file is added to the end of the list. Usually, user’s configure file should be added to the top.

Returns

A configure file object.

Raises

IOError – When the configure file is not found.

erase()[source]

Erase the existing configure file list.

get(subproject, parameter, error_sample='<relevant_value>', default=None)[source]

Get the value from the configuration files.

Parameters
  • subproject – Subsection of RC file.

  • parameter – Parameter in the subsection of RC file.

  • error_sample – Sample string for displaying in the warning.

  • default – The default return value, in case the subproject and parameter is not in the list.

exception irfpy.util.irfpyrc.NoEntryFoundError(subproject, parameter, error_sample='<relevant_value>')[source]

Bases: irfpy.util.exception.IrfpyError