Coverage for /Users/buh/.pyenv/versions/3.12.2/envs/es-testbed/lib/python3.12/site-packages/es_testbed/classes/entitymgrs/entitymgr.py: 91%

51 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-27 20:59 -0600

1"""Entity Class Definition""" 

2 

3import typing as t 

4from es_testbed.defaults import NAMEMAPPER 

5from es_testbed.helpers.utils import getlogger 

6 

7if t.TYPE_CHECKING: 7 ↛ 8line 7 didn't jump to line 8, because the condition on line 7 was never true

8 from elasticsearch8 import Elasticsearch 

9 from dotmap import DotMap 

10 

11# pylint: disable=missing-docstring,broad-exception-caught,too-many-instance-attributes 

12 

13 

14class EntityMgr: 

15 kind = 'entity_type' 

16 listname = 'entity_mgrs' 

17 

18 def __init__( 

19 self, 

20 client: t.Union['Elasticsearch', None] = None, 

21 plan: t.Union['DotMap', None] = None, 

22 autobuild: t.Optional[bool] = True, 

23 ): 

24 self.logger = getlogger('es_testbed.EntityMgr') 

25 self.client = client 

26 self.plan = plan 

27 self.success = False 

28 if autobuild: 

29 self.setup() 

30 

31 @property 

32 def entity_list(self) -> t.List: 

33 """Return the stored list of entities""" 

34 return self.plan[self.listname] 

35 

36 @entity_list.setter 

37 def entity_list(self, value: t.Sequence) -> None: 

38 self.plan[self.listname] = value 

39 

40 @property 

41 def entity_root(self) -> str: 

42 """The entity root name builder""" 

43 return f'{self.plan.prefix}-{self.ident()}-{self.plan.uniq}' 

44 

45 @property 

46 def indexlist(self) -> t.Sequence[str]: 

47 """Empty attribute/property waiting to be overridden""" 

48 return [] 

49 

50 @property 

51 def last(self) -> str: 

52 """Return the most recently appended entity""" 

53 return self.entity_list[-1] 

54 

55 @property 

56 def logdisplay(self) -> str: 

57 """Return a human readable representation of what is managed""" 

58 return self.kind 

59 

60 @property 

61 def name(self) -> str: 

62 """Return the full, incrementing name of a not yet appended entity""" 

63 return f'{self.entity_root}{self.suffix}' 

64 

65 @property 

66 def pattern(self) -> str: 

67 """Return the search pattern for the managed entity""" 

68 return f'*{self.entity_root}*' 

69 

70 @property 

71 def suffix(self) -> str: 

72 """Return the incrementing index suffix""" 

73 return f'-{len(self.entity_list) + 1:06}' 

74 

75 def appender(self, name: str) -> None: 

76 """Append an item to entity_list""" 

77 self.entity_list.append(name) 

78 

79 def ident(self, dkey=None) -> str: 

80 """Get the formatted name string of the managed entity""" 

81 if not dkey: 

82 dkey = self.kind 

83 return NAMEMAPPER[dkey] 

84 

85 def setup(self) -> None: 

86 """Setup the entity manager""" 

87 

88 def track_index(self, name: str) -> None: 

89 """Track an index and append that tracking entity to entity_list"""