isofit.data.ini =============== .. py:module:: isofit.data.ini .. autoapi-nested-parse:: ISOFIT environment module Attributes ---------- .. autoapisummary:: isofit.data.ini.Logger Classes ------- .. autoapisummary:: isofit.data.ini.Ini Functions --------- .. autoapisummary:: isofit.data.ini.getWorkingDir Module Contents --------------- .. py:data:: Logger .. py:function:: getWorkingDir(config) Attempts to detect if a configuration file sits in an ISOFIT working_directory :param config: Path to a config json file :type config: pathlib.Path :returns: **wd** -- Path to the working directory, if it's detected to be valid :rtype: pathlib.Path | None .. py:class:: Ini .. py:attribute:: _dirs :type: List[str] :value: ['data', 'examples', 'imagecube', 'srtmnet', 'sixs', 'plots'] .. py:attribute:: _keys :type: Dict[str, str] .. py:attribute:: _path_bak :value: None .. py:method:: __getattr__(key: str) -> Optional[str] Retrieves a value from CONFIG[SECTION] if the key doesn't exist on the module already. :param key: The key to retrieve the value for. :type key: str :returns: The value associated with the key if it exists in CONFIG[SECTION], otherwise None. :rtype: str or None .. py:method:: __getitem__(key: str) -> Optional[str] Simple passthrough function to __getattr__ :param key: The key to retrieve the value for. :type key: str :returns: The value associated with the key if it exists in CONFIG[SECTION], otherwise None. :rtype: str or None .. py:method:: __iter__() -> Iterable .. py:method:: __repr__() -> str .. py:method:: keys() -> Iterable[str] .. py:method:: items(kind: str = None) -> Iterable[Tuple[str, str]] Passthrough to the items() function on the working section of the config. :param kind: Returns an iterable for the specific items: - "dirs" only keys in Ini._dirs - "dirs" only keys in Ini._keys - None returns combined both :type kind: "dirs" | "keys" | None .. py:method:: changeBase(base: str) -> None Changes the base path for each directory. :param base: Path to base directory to set :type base: str .. py:method:: changeKey(key: str, value: str = '') -> None Change the value associated with the specified key in the CONFIG[SECTION]. :param key: Key to set. Alternatively, can be a dict to iterate over setting multiple keys at once. :type key: str, dict :param value: The new value to associate with the key. :type value: str, default="" .. py:method:: changeSection(section: str) -> None Changes the working section of the config. :param section: The section of the config to reference for lookups. :type section: str .. py:method:: changePath(key: str, value: str) -> None Change the path associated with the specified key in the CONFIG[SECTION]. :param key: The key whose path needs to be changed. :type key: str :param value: The new path to associate with the key. :type value: str or Path .. py:method:: load(ini: Optional[str] = None, section: Optional[str] = None) -> None Load environment variables from an ini file. :param ini: 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. :type ini: str or Path, optional :param section: Sets the working section for the session. Key lookups will use this section. :type section: str, optional .. py:method:: save(ini: Optional[str] = None, diff_only: bool = True) -> None Save CONFIG variables to the INI (ini) file. :param ini: 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. :type ini: str or Path, optional :param diff_only: 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 :type diff_only: bool, default=True .. py:method:: path(dir: str, *path: List[str], key: str = None, template: bool = False) -> pathlib.Path Retrieves a path under one of the env directories and validates the path exists. :param dir: One of the env directories, eg. "data", "examples" :type dir: str :param \*path: Path to a file under the `dir` :type \*path: List[str] :param key: Optional key value to append to the resolved path. Assumes the path is a directory and the key will be a file name :type key: str, default=None :param template: 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 :type template: bool, default=False :returns: Validated full path :rtype: pathlib.Path .. rubric:: 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 .. py:method:: 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]}".  :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 :type data: str | dict :param replace: 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 :type replace: "dirs" | "keys" | None, default="dirs" :param save: 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 :type save: bool, default=True :param report: Reports if no value in the input data was changed :type report: bool, default=True :param \*\*kwargs: 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 :type \*\*kwargs: dict :param : :returns: **data** -- In-place replaced string values with template values If saved as a new file, returns the path instead :rtype: dict | pathlib.Path .. py:method:: 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.  :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 :type data: str | dict :param save: 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. :type save: bool, default=True :param prepend: 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" :type prepend: str, default=None :param \*\*kwargs: 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 :type \*\*kwargs: dict :param : :returns: **data** -- In-place replaced template values with actual from a loaded ini If saved as a new file, returns the path instead :rtype: dict | pathlib.Path .. py:method:: reset(save: bool = False) -> None Resets the object to the defaults defined by ISOFIT :param save: Saves the reset to the default ini file: ~/.isofit/isofit.ini :type save: bool, default=False .. py:method:: validate(keys: List) -> bool :staticmethod: :abstractmethod: Validates known products. :param keys: List of products to validate :type keys: list