1+ from __future__ import annotations
2+
13from abc import ABC , abstractmethod
24from logging import Logger
3- from typing import Dict , Optional , Any , ClassVar
5+ from typing import Any , Dict , Optional , Protocol , TypeVar , TYPE_CHECKING
46
57from JXTABLES .XTablesClient import XTablesClient
68
79from ..Utils .network import DEVICEIP
8- from ..Operators .LogStreamOperator import LogStreamOperator
9- from ..Operators .UpdateOperator import UpdateOperator
10- from ..Operators .PropertyOperator import PropertyOperator
11- from ..Operators .ConfigOperator import ConfigOperator
12- from ..Operators .ShareOperator import ShareOperator
13- from ..Operators .StreamOperator import StreamProxy
14- from ..Operators .TimeOperator import TimeOperator , Timer
15- from ..Constants .Teams import TEAM
16- from ..Constants .AgentConstants import Proxy , ProxyType
1710
11+ if TYPE_CHECKING :
12+ from ..Constants .AgentConstants import Proxy , ProxyType
13+ from ..Constants .Teams import TEAM
14+ from ..Operators .ConfigOperator import ConfigOperator
15+ from ..Operators .LogStreamOperator import LogStreamOperator
16+ from ..Operators .PropertyOperator import PropertyOperator
17+ from ..Operators .ShareOperator import ShareOperator
18+ from ..Operators .StreamOperator import StreamProxy
19+ from ..Operators .TimeOperator import TimeOperator , Timer
20+ from ..Operators .UpdateOperator import UpdateOperator
21+
22+
23+ class Agent (Protocol ):
24+ hasShutdown : bool = False
25+ hasClosed : bool = False
26+ isCleanedUp : bool = False
27+ isMainThread : bool = False
28+ agentName : str = ""
29+ xclient : XTablesClient
30+ propertyOperator : PropertyOperator
31+ configOperator : ConfigOperator
32+ updateOp : UpdateOperator
33+ timeOp : TimeOperator
34+ Sentinel : Logger
35+ timer : Timer
36+
37+ def _injectCore (
38+ self , shareOperator : ShareOperator , isMainThread : bool , agentName : str
39+ ) -> None :
40+ ...
41+
42+ def _injectNEW (
43+ self ,
44+ xclient : XTablesClient , # new
45+ propertyOperator : PropertyOperator , # new
46+ configOperator : ConfigOperator , # new
47+ updateOperator : UpdateOperator , # new
48+ timeOperator : TimeOperator , # new
49+ logger : Logger , # static/new
50+ ) -> None :
51+ ...
1852
53+ def getProxy (self , proxyName : str ) -> Optional [Proxy ]:
54+ ...
55+
56+ def _setProxies (self , proxies ) -> None :
57+ ...
58+
59+ def _cleanup (self ) -> None :
60+ ...
61+
62+ def getTimer (self ) -> Timer :
63+ ...
64+
65+ def getTeam (self ) -> Optional [TEAM ]:
66+ ...
67+
68+ def _runOwnCreate (self ):
69+ ...
70+
71+ @classmethod
72+ def getName (cls ) -> str :
73+ ...
74+
75+ def create (self ) -> None :
76+ ...
77+
78+ def runPeriodic (self ) -> None :
79+ ...
1980
20- class Agent (ABC ):
81+ def isRunning (self ) -> bool :
82+ ...
83+
84+ def getDescription (self ) -> str :
85+ ...
86+
87+ def getIntervalMs (self ) -> int :
88+ ...
89+
90+ def forceShutdown (self ) -> None :
91+ ...
92+
93+ def onClose (self ) -> None :
94+ ...
95+
96+ @classmethod
97+ def requestProxies (cls ) -> None :
98+ ...
99+
100+ @classmethod
101+ def addProxyRequest (cls , proxyName : str , proxyType : ProxyType ) -> None :
102+ ...
103+
104+ @classmethod
105+ def _getProxyRequests (cls ) -> Dict [str , ProxyType ]:
106+ ...
107+
108+
109+ TAgent = TypeVar ("TAgent" , bound = Agent )
110+
111+
112+ class AgentBase (ABC ):
21113 """
22114 Base class for all agents.
23115 """
116+
117+ _proxyRequests : Dict [str , ProxyType ] = {}
24118 DEFAULT_LOOP_TIME : int = 0 # 0 ms
25119 TIMERS = "timers"
26120
@@ -31,8 +125,7 @@ def __init__(self, **kwargs: Any) -> None:
31125 self .isCleanedUp : bool = False
32126 self .isMainThread : bool = False
33127 self .agentName = ""
34- self .__proxies : Dict [str , Proxy ] = {}
35-
128+ self .__proxies : Dict [str , Proxy ] = {}
36129
37130 def _injectCore (
38131 self , shareOperator : ShareOperator , isMainThread : bool , agentName : str
@@ -70,23 +163,24 @@ def _injectNEW(
70163 self .timer = self .timeOp .getTimer (self .TIMERS )
71164 # other than setting variables, nothing should go here
72165
73- def _setProxies (self , proxies ):
166+ def _setProxies (self , proxies ) -> None :
74167 self .__proxies = proxies
75168
76169 def _updateNetworkProxyInfo (self ):
77- """ Put information about the proxies used on xtables. This can be used for the dashboard, and other things"""
170+ """Put information about the proxies used on xtables. This can be used for the dashboard, and other things"""
78171 streamPaths = []
79172 for proxyName , proxy in self .__proxies .items ():
80173 if isinstance (proxy , StreamProxy ):
81174 streamPaths .append (f"{ proxyName } |{ proxy .getStreamPath ()} " )
82175
83- self .propertyOperator .createCustomReadOnlyProperty ("streamPaths" , streamPaths , addBasePrefix = True , addOperatorPrefix = True )
84-
176+ self .propertyOperator .createCustomReadOnlyProperty (
177+ "streamPaths" , streamPaths , addBasePrefix = True , addOperatorPrefix = True
178+ )
85179
86- def getProxy (self , proxyName : str ) -> Optional [Proxy ]:
180+ def getProxy (self , proxyName : str ) -> Optional [Proxy ]:
87181 return self .__proxies .get (proxyName )
88182
89- def _cleanup (self ):
183+ def _cleanup (self ) -> None :
90184 # xclient shutdown occasionally failing?
91185 # self.xclient.shutdown()
92186 self .propertyOperator .deregisterAll ()
@@ -96,7 +190,7 @@ def getTimer(self) -> Timer:
96190 """Use only when needed, and only when associated with agent"""
97191 if self .timer is None :
98192 raise ValueError ("Timer not initialized" )
99-
193+
100194 return self .timer
101195
102196 def getTeam (self ) -> Optional [TEAM ]:
@@ -112,18 +206,19 @@ def getTeam(self) -> Optional[TEAM]:
112206 return TEAM .BLUE
113207 else :
114208 return TEAM .RED
115-
209+
116210 def _runOwnCreate (self ):
117- """ The agent wants to do its own stuff too... okay."""
211+ """The agent wants to do its own stuff too... okay."""
118212
119213 logIp = f"http://{ DEVICEIP } :5000/{ self .agentName } /{ LogStreamOperator .LOGPATH } "
120214
121- self .propertyOperator .createCustomReadOnlyProperty ("logIP" , logIp , addBasePrefix = True , addOperatorPrefix = True )
215+ self .propertyOperator .createCustomReadOnlyProperty (
216+ "logIP" , logIp , addBasePrefix = True , addOperatorPrefix = True
217+ )
122218
123219 self .__ensureProxies ()
124220 self ._updateNetworkProxyInfo ()
125221
126-
127222 @classmethod
128223 def getName (cls ):
129224 return cls .__name__
@@ -176,28 +271,29 @@ def onClose(self) -> None:
176271 pass
177272
178273 # ----- proxy methods -----
179- def __ensureProxies (self ):
274+ def __ensureProxies (self ) -> None :
180275 for proxyName , proxyType in self ._getProxyRequests ().items ():
181- if proxyName not in self .__proxies or type (self .__proxies [proxyName ]) is not proxyType :
276+ if (
277+ proxyName not in self .__proxies
278+ or type (self .__proxies [proxyName ]) is not proxyType
279+ ):
182280 print (type (self .__proxies [proxyName ]) is proxyType )
183- raise RuntimeError (f"Agent proxies are not correcty initialized!\n { self ._getProxyRequests ()= } \n { self .__proxies .items ()= } " )
281+ raise RuntimeError (
282+ f"Agent proxies are not correcty initialized!\n { self ._getProxyRequests ()= } \n { self .__proxies .items ()= } "
283+ )
184284
185285 @classmethod
186286 def requestProxies (cls ):
187- """ Override this, and add all of your proxy requests"""
287+ """Override this, and add all of your proxy requests"""
188288 pass
189289
190290 @classmethod
191- def addProxyRequest (cls , proxyName : str , proxyType : ProxyType ) -> None :
192- """ Method to request that a stream proxy will be given to this agent to display streams
193- NOTE: you must override requestProxies() and add your calls to this there, or else it will not be used!
291+ def addProxyRequest (cls , proxyName : str , proxyType : ProxyType ) -> None :
292+ """Method to request that a stream proxy will be given to this agent to display streams
293+ NOTE: you must override requestProxies() and add your calls to this there, or else it will not be used!
194294 """
195- if not hasattr (cls , '_proxyRequests' ):
196- cls ._proxyRequests = {}
197-
198295 cls ._proxyRequests [proxyName ] = proxyType
199296
200297 @classmethod
201298 def _getProxyRequests (cls ) -> Dict [str , ProxyType ]:
202- return getattr (cls , '_proxyRequests' , {})
203-
299+ return getattr (cls , "_proxyRequests" , {})
0 commit comments