Coverage for src/system/platform.py: 100.00%

33 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-21 21:20 +0000

1#!/usr/bin/env python3 

2 

3# Standard libraries 

4from os import access, environ, R_OK, sep 

5from os.path import expanduser 

6from pathlib import Path 

7from sys import platform, stdin, stdout 

8 

9# Platform 

10class Platform: 

11 

12 # Environment 

13 ENV_SIMULATE_MAC_OS: str = 'SIMULATE_MAC_OS' 

14 ENV_SUDO_USER: str = 'SUDO_USER' 

15 

16 # Constants 

17 IS_LINUX: bool = platform in ['linux', 'linux2'] 

18 IS_MAC_OS: bool = platform in ['darwin'] or ENV_SIMULATE_MAC_OS in environ 

19 IS_SIMULATED: bool = ENV_SIMULATE_MAC_OS in environ 

20 IS_WINDOWS: bool = platform in ['win32', 'win64'] 

21 

22 # Separators 

23 PATH_SEPARATOR: str = sep 

24 

25 # TTYs 

26 IS_TTY_STDIN: bool = stdin.isatty() and stdin.encoding != 'cp1252' 

27 IS_TTY_STDOUT: bool = stdout.isatty() 

28 IS_TTY_UTF8: bool = str(stdout.encoding).lower() == 'utf-8' 

29 

30 # Outputs 

31 IS_FLUSH_ENABLED: bool = IS_TTY_STDOUT or IS_WINDOWS 

32 

33 # Users 

34 IS_USER_SUDO: bool = ENV_SUDO_USER in environ 

35 USER_SUDO: str = environ[ENV_SUDO_USER] if IS_USER_SUDO else '' 

36 

37 # Flush 

38 @staticmethod 

39 def flush() -> None: 

40 

41 # Flush output 

42 print( 

43 '', 

44 end='', 

45 flush=Platform.IS_FLUSH_ENABLED, 

46 ) 

47 

48 # Userspace 

49 @staticmethod 

50 def userspace(name: str) -> Path: 

51 

52 # Variables 

53 home: None | Path = None 

54 

55 # Elevated home 

56 if Platform.IS_USER_SUDO: # pragma: linux cover 

57 home = Path(expanduser(f'~{Platform.USER_SUDO}')) 

58 if not access(home, R_OK): # pragma: no cover 

59 home = None 

60 

61 # Default home 

62 if not home or not home.is_dir(): 

63 home = Path.home() 

64 

65 # Windows userspace 

66 if Platform.IS_WINDOWS: # pragma: windows cover 

67 return home / 'AppData' / 'Local' / name 

68 

69 # macOS userspace 

70 if Platform.IS_MAC_OS: # pragma: macos cover 

71 return home / 'Library' / 'Preferences' / name 

72 

73 # Linux userspace 

74 return home / '.config' / name # pragma: linux cover