Coverage for src/types/environment.py: 100.00%

16 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-10-18 15:02 +0000

1#!/usr/bin/env python3 

2 

3# Standard libraries 

4from os.path import expandvars 

5from re import sub as regex_sub 

6 

7# Environment class, pylint: disable=too-few-public-methods 

8class Environment: 

9 

10 # Expand 

11 @staticmethod 

12 def expand( 

13 value: str, 

14 variable: str = '', 

15 unknowns: bool = False, 

16 ) -> str: 

17 

18 # Variables 

19 last: str = '' 

20 result: str = value 

21 

22 # Avoid nested variable 

23 if variable: 

24 result = regex_sub( 

25 r'(?<!\\)\${' + f'{variable}' + '}', 

26 '', 

27 result, 

28 ) 

29 result = regex_sub( 

30 r'(?<!\\)\$' + f'{variable}' + r'([^A-Za-z_]+|$)', 

31 '\\1', 

32 result, 

33 ) 

34 

35 # Expand while needed 

36 while last != result: 

37 last = result 

38 result = expandvars(result) 

39 

40 # Expand unknown variables 

41 if unknowns: 

42 result = regex_sub( 

43 r'(?<!\\)\${?[A-Za-z_][A-Za-z0-9_]*}?', 

44 '', 

45 result, 

46 ) 

47 

48 # Result 

49 return result