[docs]classEnvironment:"""Object to simplify getting information about the runtime environment."""root_dir:Path"""Root directory of the environment."""sys:SystemInfo"""Information about the system."""
[docs]def__init__(self,*,environ:dict[str,str]|None=None,root_dir:Path|None=None,)->None:"""Instantiate class. Args: environ: Environment variables. root_dir: Root directory of the environment (e.g. :meth:`pathlib.Path.cwd`). """self.root_dir=root_dirorPath.cwd()self.sys=SystemInfo()self.vars=environifisinstance(environ,dict)elseos.environ.copy()
@propertydefci(self)->bool:"""Return CI status. Returns: bool """return"CI"inself.vars@ci.setterdefci(self,value:object)->None:"""Set the value of CI."""ifvalue:self._update_vars({"CI":"1"})else:self.vars.pop("CI",None)@ci.deleterdefci(self)->None:"""Delete the value of CI."""self.vars.pop("CI",None)@propertydefdebug(self)->bool:"""Get debug setting from the environment."""return"DEBUG"inself.vars@debug.setterdefdebug(self,value:object)->None:"""Set the value of DEBUG."""ifvalue:self._update_vars({"DEBUG":"1"})else:self.vars.pop("DEBUG",None)@propertydefverbose(self)->bool:"""Get verbose setting from the environment."""return"VERBOSE"inself.vars@verbose.setterdefverbose(self,value:object)->None:"""Set the value of VERBOSE."""ifvalue:self._update_vars({"VERBOSE":"1"})else:self.vars.pop("VERBOSE",None)
[docs]defcopy(self:Self)->Self:"""Copy the contents of this object into a new instance. Returns: New instance with the same contents. """returnself.__class__(environ=self.vars.copy(),root_dir=self.root_dir,)
def_update_vars(self,env_vars:dict[str,str])->None:"""Update vars and log the change. Args: env_vars (Dict[str, str]): Dict to update self.vars with. """self.vars.update(env_vars)LOGGER.debug("updated environment variables: %s",json.dumps(env_vars))