Coverage for src/P4OO/Counter.py: 89%

18 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-09-07 17:17 +0000

1###################################################################### 

2# Copyright (c)2024, David L. Armstrong 

3# Copyright (c)2013, Cisco Systems, Inc. 

4# 

5# P4OO.Counter.py 

6# 

7###################################################################### 

8 

9""" 

10Perforce Counter objects 

11 

12P4OO.Counter provides common behaviors for all P4OO Counter objects. 

13 

14Unlike SpecObj objects, we do not cache the values from Perforce for 

15counters. Since they are simply name/value pairs, assume the caller 

16will keep track of them as appropriate, and always query Perforce. 

17 

18Counters are designed to change frequently, so when queried multiple 

19times it's likely a use case where the counter is expected to have 

20changed. 

21""" 

22 

23from dataclasses import dataclass, field 

24from P4OO._Base import _P4OOBase 

25from P4OO._Set import _P4OOSet 

26 

27@dataclass 

28class P4OOCounter(_P4OOBase): 

29 """ 

30 Perforce Counter Object 

31 

32 id Required: Yes 

33 

34 Forcible: No 

35 

36 Attributes: 

37 id (str): Name of the Counter 

38 

39 See Also: 

40 Perforce Helix Core Command Reference: 

41 https://www.perforce.com/manuals/cmdref/Content/CmdRef/p4_counter.html 

42 """ 

43 id: str = field(default=None, compare=True) 

44 

45 def __repr__(self): 

46 return '%s(%s)' % (self.__class__.__name__, self.id) 

47 

48 def getValue(self): 

49 """ 

50 Retrieve the current value of the counter 

51 

52 Returns: 

53 (int): Current value of the counter 

54 """ 

55 

56 p4ConnObj = self._getP4Connection() 

57 return p4ConnObj.readCounter(self.id) 

58 

59 def setValue(self, newValue): 

60 """ 

61 Set the new value of the counter 

62 

63 Args: 

64 newValue (int): New value for the counter 

65 

66 Returns: 

67 (int): New current value for the counter 

68 """ 

69 

70 p4ConnObj = self._getP4Connection() 

71 return p4ConnObj.setCounter(self.id, newValue) 

72 

73 

74class P4OOCounterSet(_P4OOSet): 

75 """ `P4OOSet` of `P4OOCounter` objects """ 

76 

77 def query(self, maxresults: int=None, namefilter: str=None, **kwargs): 

78 """ 

79 Executes `p4 counters` query 

80 

81 Args: 

82 maxresults (int, optional): Return only the first [max] results 

83 namefilter (str, optional): Case-sensitive filter on counter name 

84 

85 Returns: 

86 (P4OOCounterSet): `P4OOSet` of `P4OOCounter` objects matching query 

87 parameters 

88 

89 See Also: 

90 Perforce Helix Core Command Reference: 

91 https://www.perforce.com/manuals/cmdref/Content/CmdRef/p4_counters.html 

92 """ 

93 

94 return self._query(setObjType='counters', maxresults=maxresults, 

95 namefilter=namefilter, **kwargs)