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
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-21 21:33 +0000
1#!/usr/bin/env python3
3# Standard libraries
4from re import sub
5from typing import Optional, Tuple
7# Namespaces class
8class Namespaces:
10 # Capitalize
11 @staticmethod
12 def capitalize(text: str, words: bool = False) -> str:
14 # Capitalize words
15 if words:
16 return ' '.join(
17 Namespaces.capitalize(word, words=False) for word in text.split())
19 # Capitalize text
20 return f'{text[:1].capitalize()}{text[1:]}'
22 # Describe
23 @staticmethod
24 def describe(
25 name: str,
26 description: str = '',
27 ) -> str:
29 # Use description
30 if description:
31 return description
33 # Adapt name
34 return Namespaces.capitalize(sub(r'[-_]', ' ', name), words=True)
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 )
52 # Subpath
53 @staticmethod
54 def subpath(
55 parent_path: str,
56 child_path: str,
57 ) -> str:
59 # Parse relative path
60 if child_path.startswith(f'{parent_path}/'):
61 return child_path[len(f'{parent_path}/'):]
63 # Default absolute path
64 return child_path
66 # Text
67 @staticmethod
68 def text(text: Optional[str]) -> str:
70 # Use text
71 if text:
72 return text
74 # Fallback text
75 return '/'