Coverage for glotter/utils.py: 100%

18 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-04-12 02:25 +0000

1import sys 

2 

3 

4def quote(value: str) -> str: 

5 """ 

6 Enclose a value in quotes 

7 

8 :param value: Value to enclosed 

9 :return: Quoted value 

10 

11 """ 

12 

13 if '"' in value: 

14 if "'" in value: 

15 quote_chars = '"""' 

16 if value.startswith('"'): 

17 value = f"\\{value}" 

18 

19 if value.endswith('"'): 

20 value = f'{value[:-1]}\\"' 

21 else: 

22 quote_chars = "'" 

23 else: 

24 quote_chars = '"' 

25 

26 return f"{quote_chars}{value}{quote_chars}" 

27 

28 

29def indent(value: str, num_spaces: int) -> str: 

30 """ 

31 Indent each line of a string by a specified number of spaces 

32 

33 :param value: String to indent 

34 :param num_spaces: Number of spaces to indent 

35 :return: Indented string 

36 """ 

37 

38 spaces = " " * num_spaces 

39 return "".join(f"{spaces}{line}" for line in value.splitlines(keepends=True)) 

40 

41 

42def error_and_exit(msg): 

43 print(msg) 

44 sys.exit(1)