Coverage for src/types/namespaces.py: 50.00%

26 statements  

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

1#!/usr/bin/env python3 

2 

3# Standard libraries 

4from re import sub 

5from typing import Optional, Tuple 

6 

7# Namespaces class 

8class Namespaces: 

9 

10 # Capitalize 

11 @staticmethod 

12 def capitalize(text: str, words: bool = False) -> str: 

13 

14 # Capitalize words 

15 if words: 

16 return ' '.join( 

17 Namespaces.capitalize(word, words=False) for word in text.split()) 

18 

19 # Capitalize text 

20 return f'{text[:1].capitalize()}{text[1:]}' 

21 

22 # Describe 

23 @staticmethod 

24 def describe( 

25 name: str, 

26 description: str = '', 

27 ) -> str: 

28 

29 # Use description 

30 if description: 

31 return description 

32 

33 # Adapt name 

34 return Namespaces.capitalize(sub(r'[-_]', ' ', name), words=True) 

35 

36 # Split namespace 

37 @staticmethod 

38 def split_namespace( 

39 path: str, 

40 relative: bool = False, 

41 ) -> Tuple[str, str]: 

42 return ( 

43 # Namespace 

44 f'/{path[:path.rfind("/")]}' if '/' in path and relative else \ 

45 f'{path[:path.rfind("/")]}' if '/' in path else \ 

46 '', 

47 # Path 

48 f'{path[path.rfind("/") + 1:]}' if '/' in path else \ 

49 path, 

50 ) 

51 

52 # Subpath 

53 @staticmethod 

54 def subpath( 

55 parent_path: str, 

56 child_path: str, 

57 ) -> str: 

58 

59 # Parse relative path 

60 if child_path.startswith(f'{parent_path}/'): 

61 return child_path[len(f'{parent_path}/'):] 

62 

63 # Default absolute path 

64 return child_path 

65 

66 # Text 

67 @staticmethod 

68 def text(text: Optional[str]) -> str: 

69 

70 # Use text 

71 if text: 

72 return text 

73 

74 # Fallback text 

75 return '/'