"""Operating system information."""from__future__importannotationsimportosimportplatformfromfunctoolsimportcached_propertyfrompathlibimportPathfromtypingimportTYPE_CHECKING,ClassVar,cast,finalfromplatformdirs.uniximportUnixfromplatformdirs.windowsimportWindowsifTYPE_CHECKING:importplatformdirs
[docs]@finalclassOsInfo:"""Information about the operating system running on the current system."""__instance:ClassVar[OsInfo|None]=None
[docs]def__new__(cls,*args:object,**kwargs:object)->OsInfo:"""Create a new instance of class. This class is a singleton so it will always return the same instance. """ifcls.__instanceisNone:cls.__instance=cast(OsInfo,super().__new__(cls,*args,**kwargs))returncls.__instance
@cached_propertydef_platform_dirs(self)->platformdirs.PlatformDirsABC:"""Instance of platformdirs class to get platform specific directories. ``appname``, ``appauthor``, and ``version`` are not passed to the class. This is so that the base directory is returned. """ifself.is_windows:returnWindows(appname="f-lib",appauthor="finley")# platformdirs does no handle macOS the way I would like it to so alway use unixreturnUnix(appname="f-lib",appauthor="finley")@cached_propertydefis_darwin(self)->bool:"""Operating system is Darwin."""returnself.name=="darwin"@cached_propertydefis_linux(self)->bool:"""Operating system is Linux."""returnself.name=="linux"@cached_propertydefis_macos(self)->bool:"""Operating system is macOS. Does not differentiate between macOS and Darwin. """returnself.is_darwin@cached_propertydefis_posix(self)->bool:"""Operating system is posix."""returnos.name=="posix"@cached_propertydefis_windows(self)->bool:"""Operating system is Windows."""returnself.namein("windows","mingw64","msys_nt","cygwin_nt")@cached_propertydefname(self)->str:"""Operating system name set to lowercase for consistency."""returnplatform.system().lower()@cached_propertydefuser_config_dir(self)->Path:"""Path to the config directory for the user. - ``~/.config`` - ``%USERPROFILE%/AppData/Local`` - ``%USERPROFILE%/AppData/Roaming`` """returnPath(self._platform_dirs.user_config_dir)@cached_propertydefuser_data_dir(self)->Path:"""Path to data directory tied to the user. - ``~/.local/share`` - ``%USERPROFILE%/AppData/Local`` - ``%USERPROFILE%/AppData/Roaming`` """returnPath(self._platform_dirs.user_data_dir)
[docs]@classmethoddefclear_singleton(cls)->None:"""Clear singleton instances. Intended to only be used for running tests. """cls.__instance=None