Coverage for .nox/test-3-9/lib/python3.9/site-packages/nskit/recipes/python/__init__.py: 0%

37 statements  

« prev     ^ index     » next       coverage.py v7.4.2, created at 2024-02-25 17:38 +0000

1"""Python Recipes.""" 

2from pathlib import Path 

3import re 

4 

5from pydantic import Field 

6 

7from nskit import __version__ 

8from nskit.mixer import CodeRecipe, RepoMetadata 

9 

10_DELIMITERS = ['.', ',', '-'] 

11 

12 

13class PyRepoMetadata(RepoMetadata): 

14 """Repo Metadata for python templates.""" 

15 

16 _name: str = None 

17 

18 @property 

19 def name(self): 

20 """Get repo name.""" 

21 return self._name 

22 

23 @name.setter 

24 def name(self, value): 

25 """Set repo name.""" 

26 if isinstance(value, str): 

27 self._name = value 

28 

29 def _get_name_parts(self): 

30 return re.split('|'.join(map(re.escape, list(set(_DELIMITERS+[self.repo_separator])))), self.name) 

31 

32 @property 

33 def py_name(self): 

34 """Get python module name.""" 

35 return '.'.join(self._get_name_parts()) 

36 

37 @property 

38 def py_root(self): 

39 """Get root python module name.""" 

40 return self._get_name_parts()[0] 

41 

42 @property 

43 def src_path(self): 

44 """Get module folder structure (src not included).""" 

45 return Path(*self._get_name_parts()).as_posix() 

46 

47 @property 

48 def module_depth(self) -> int: 

49 """Get the module depth. 

50 

51 ``a.b.c`` has a depth of 3, ``a`` has a depth of 1 

52 """ 

53 return len(self._get_name_parts()) 

54 

55 

56class PyRecipe(CodeRecipe): 

57 """Base recipe for python recipes.""" 

58 

59 version: str = __version__ 

60 """Version of the recipe.""" 

61 

62 repo: PyRepoMetadata = Field(...) 

63 """Python repo metadata.""" 

64 

65 @staticmethod 

66 def _to_pep8(value): 

67 return str(value).lower().replace(' ', '_').replace('-', '_') 

68 

69 def model_post_init(self, *args): # noqa: U100 

70 """Set repo name handling.""" 

71 self.repo.name = self.name