isofit.data.ini

ISOFIT environment module

Attributes

Logger

Classes

Ini

Functions

getWorkingDir(config)

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

Module Contents

Logger[source]
getWorkingDir(config)[source]

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 – Path to the working directory, if it’s detected to be valid

Return type:

pathlib.Path | None

class Ini[source]
_dirs: List[str] = ['data', 'examples', 'imagecube', 'srtmnet', 'sixs', 'plots'][source]
_keys: Dict[str, str][source]
_path_bak = None[source]
__getattr__(key: str) str | None[source]

Retrieves a value from CONFIG[SECTION] if the key doesn’t exist on the module already.

Parameters:

key (str) – The key to retrieve the value for.

Returns:

The value associated with the key if it exists in CONFIG[SECTION], otherwise None.

Return type:

str or None

__getitem__(key: str) str | None[source]

Simple passthrough function to __getattr__

Parameters:

key (str) – The key to retrieve the value for.

Returns:

The value associated with the key if it exists in CONFIG[SECTION], otherwise None.

Return type:

str or None

__iter__() Iterable[source]
__repr__() str[source]
keys() Iterable[str][source]
items(kind: str = None) Iterable[Tuple[str, str]][source]

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

changeBase(base: str) None[source]

Changes the base path for each directory.

Parameters:

base (str) – Path to base directory to set

changeKey(key: str, value: str = '') None[source]

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.

changeSection(section: str) None[source]

Changes the working section of the config.

Parameters:

section (str) – The section of the config to reference for lookups.

changePath(key: str, value: str) None[source]

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.

load(ini: str | None = None, section: str | None = None) None[source]

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.

save(ini: str | None = None, diff_only: bool = True) None[source]

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

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

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:

Validated full path

Return type:

pathlib.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
toTemplate(data: str | dict, replace='dirs', save: bool = True, report: bool = True, **kwargs) dict[source]

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]}”.

 :param data: 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

Parameters:
  • 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 – In-place replaced string values with template values If saved as a new file, returns the path instead

Return type:

dict | pathlib.Path

fromTemplate(data: str | dict, save: bool = True, prepend: str = None, **kwargs) dict[source]

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.

 :param data: 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

Parameters:
  • 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 – In-place replaced template values with actual from a loaded ini If saved as a new file, returns the path instead

Return type:

dict | pathlib.Path

reset(save: bool = False) None[source]

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

static validate(keys: List) bool[source]
Abstractmethod:

Validates known products.

Parameters:

keys (list) – List of products to validate