Skip to content

Ini

source module isofit.data.ini

ISOFIT environment module

Classes

Functions

  • getWorkingDir Attempts to detect if a configuration file sits in an ISOFIT working_directory

source getWorkingDir(config)

Attempts to detect if a configuration file sits in an ISOFIT working_directory

Parameters

  • config : pathlib.Path Path to a config json file

Returns

  • wd : pathlib.Path | None Path to the working directory, if it's detected to be valid

source class Ini()

Methods

  • keys

  • items Passthrough to the items() function on the working section of the config.

  • changeBase Changes the base path for each directory.

  • changeKey Change the value associated with the specified key in the CONFIG[SECTION].

  • changeSection Changes the working section of the config.

  • changePath Change the path associated with the specified key in the CONFIG[SECTION].

  • load Load environment variables from an ini file.

  • save Save CONFIG variables to the INI (ini) file.

  • path Retrieves a path under one of the env directories and validates the path exists.

  • toTemplate Recursively converts string values in a dict to be template values which can be converted back using Ini.fromTemplate(). Template values are in the form of "{env.[value]}".

  • fromTemplate Recursively replaces the template values in found in string values with the real value from the ini. Template values are in the form of "{env.[value]}". This is an in-place operation.

  • reset Resets the object to the defaults defined by ISOFIT

  • validate Validates known products.

source method Ini.keys()Iterable[str]

source method Ini.items(kind: str = None)Iterable[Tuple[str, str]]

Passthrough to the items() function on the working section of the config.

Parameters

  • kind : "dirs" | "keys" | None Returns an iterable for the specific items:

    • "dirs" only keys in Ini._dirs
    • "dirs" only keys in Ini._keys
    • None returns combined both

source method Ini.changeBase(base: str)None

Changes the base path for each directory.

Parameters

  • base : str Path to base directory to set

source method Ini.changeKey(key: str, value: str = '')None

Change the value associated with the specified key in the CONFIG[SECTION].

Parameters

  • key : str, dict Key to set. Alternatively, can be a dict to iterate over setting multiple keys at once.

  • value : str, default="" The new value to associate with the key.

source method Ini.changeSection(section: str)None

Changes the working section of the config.

Parameters

  • section : str The section of the config to reference for lookups.

source method Ini.changePath(key: str, value: str)None

Change the path associated with the specified key in the CONFIG[SECTION].

Parameters

  • key : str The key whose path needs to be changed.

  • value : str or Path The new path to associate with the key.

source method Ini.load(ini: Optional[str] = None, section: Optional[str] = None)None

Load environment variables from an ini file.

Parameters

  • ini : str or Path, optional The path to the INI file containing config variables. If None, the default INI file path is used. If provided, sets the global INI for the remainder of the session.

  • section : str, optional Sets the working section for the session. Key lookups will use this section.

source method Ini.save(ini: Optional[str] = None, diff_only: bool = True)None

Save CONFIG variables to the INI (ini) file.

Parameters

  • ini : str or Path, optional The path to save the config variables to. If None, the default INI file path is used. If provided, sets the global INI for the remainder of the session.

  • diff_only : bool, default=True Only save if there is a difference between the currently existing ini file and the config in memory. If False, will save regardless, possibly overwriting an existing file

source method Ini.path(dir: str, *path: List[str], key: str = None, template: bool = False)Path

Retrieves a path under one of the env directories and validates the path exists.

Parameters

  • dir : str One of the env directories, eg. "data", "examples"

  • *path : List[str] Path to a file under the dir

  • key : str, default=None Optional key value to append to the resolved path. Assumes the path is a directory and the key will be a file name

  • template : bool, default=False Returns the path as a template string. The path will still be validated, but the return will be "{env.[dir]}/*path", to be used with Ini.replace

Returns

  • pathlib.Path Validated full path

Examples

from isofit.data import env
env.load()
env.path("data")
~/.isofit/data
env.path("examples", "20171108_Pasadena", "configs", "ang20171108t184227_surface.json")
~/.isofit/examples/20171108_Pasadena/configs/ang20171108t184227_surface.json
env.path("srtmnet", key="srtmnet.file")
~/.isofit/srtmnet/sRTMnet_v120.h5
env.path("srtmnet", key="srtmnet.aux")
~/.isofit/srtmnet/sRTMnet_v120_aux.npz

Raises

  • FileNotFoundError

source method Ini.toTemplate(data: str | dict, replace='dirs', save: bool = True, report: bool = True, **kwargs)dict

Recursively converts string values in a dict to be template values which can be converted back using Ini.fromTemplate(). Template values are in the form of "{env.[value]}".

 Parameters


data : str | dict The dictionary to walk over and update values. If string, checks if this exists as a file and loads that in as the data dict replace : "dirs" | "keys" | None, default="dirs" Defines what kind of values from the ini to replace in strings: - "dirs" only replace directory paths - "keys" only replace key strings - None replaces both Recommended to only use "dirs" to remain consistent. "keys" can have unintended consequences and may replace more than it should save : bool, default=True If the data was a file and this is enabled, saves the converted data dict to another file. The new file will simply append ".tmpl" to its name report : bool, default=True Reports if no value in the input data was changed **kwargs : dict Additional strings to replace. The values are replaced in a string with the key of the kwarg. For example:

kwargs = {"xyz": "abc"}
data["some_key"] = "replace abc here"

will be replaced as:
data["some_key"] = "replace {xyz} here"
This is to be used with Ini.fromTemplate to replace values that are not
found in the ini object

 Returns


data : dict | pathlib.Path In-place replaced string values with template values If saved as a new file, returns the path instead

Raises

  • FileNotFoundError

source method Ini.fromTemplate(data: str | dict, save: bool = True, prepend: str = None, **kwargs)dict

Recursively replaces the template values in found in string values with the real value from the ini. Template values are in the form of "{env.[value]}". This is an in-place operation.

 Parameters


data : str | dict The dictionary to walk over and update values. If string, checks if this exists as a file and loads that in as the data dict save : bool, default=True If the data was a file and this is enabled, saves the converted data dict to another file. If the input file ends with ".tmpl" then it will simply be cut. If it doesn't or already exists, then the output filename will be the input filename prepended with prepend value. prepend : str, default=None Prepend a string to the output filename. If not set and the input filename doesn't end with ".tmpl", then this is auto-set to "replaced" **kwargs : dict Additional strings to replace. The values are replaced in a string with the key of the kwarg. For example:

kwargs = {"xyz": "abc"}
data["some_key"] = "replace {xyz} here"

will be replaced as:
data["some_key"] = "replace abc here"
This is to be used with Ini.toTemplate to replace values that are not found
in the ini object

 Returns


data : dict | pathlib.Path In-place replaced template values with actual from a loaded ini If saved as a new file, returns the path instead

Raises

  • FileNotFoundError

source method Ini.reset(save: bool = False)None

Resets the object to the defaults defined by ISOFIT

Parameters

  • save : bool, default=False Saves the reset to the default ini file: ~/.isofit/isofit.ini

source staticmethod Ini.validate(keys: List)bool

Validates known products.

Parameters

  • keys : list List of products to validate

Raises

  • NotImplementedError