1- import os
2- import json
3- import base64
41from urllib .parse import urlparse
52from .publisher import Publisher
63from .subscriber import Subscriber
74from .parameter import Parameter
85from .readme import Readme
96from .manifest import Manifest
7+ import asyncio
8+ import json
9+ import base64
10+ import os
1011import pkg_resources
1112
1213class Extension :
1314 def __init__ (self , token = None ):
14- token = token or os .environ .get ('TALKOPS_TOKEN' )
15- if token :
16- mercure = json .loads (base64 .b64decode (token ).decode ())
15+ self ._callbacks = {}
16+ self ._category = None
17+ self ._demo = False
18+ self ._features = []
19+ self ._functions = []
20+ self ._function_schemas = []
21+ self ._icon = None
22+ self ._installation_steps = []
23+ self ._instructions = None
24+ self ._name = None
25+ self ._parameters = []
26+ self ._software_version = None
27+ self ._token = token or os .environ .get ('TALKOPS_TOKEN' )
28+ self ._website = None
29+
30+ async def _setup (self ):
31+ await asyncio .sleep (0.5 )
32+ if self ._token :
33+ mercure = json .loads (base64 .b64decode (self ._token ).decode ())
1734 self ._publisher = Publisher (
1835 lambda : {'mercure' : mercure },
1936 lambda : {
@@ -43,7 +60,7 @@ def __init__(self, token=None):
4360 }
4461 )
4562
46- if os .environ .get ('NODE_ENV ' ) == 'development' :
63+ if os .environ .get ('ENV ' ) == 'development' :
4764 Readme (
4865 lambda : {
4966 'features' : self ._features ,
@@ -59,26 +76,16 @@ def __init__(self, token=None):
5976 'name' : self ._name ,
6077 'sdk' : {
6178 'name' : 'python' ,
62- 'version' : '2.14.1' ,
79+ 'version' : pkg_resources . get_distribution ( 'talkops' ). version ,
6380 },
6481 'softwareVersion' : self ._software_version ,
6582 'website' : self ._website ,
6683 }
6784 )
6885
69- self ._callbacks = {}
70- self ._category = None
71- self ._demo = False
72- self ._features = []
73- self ._functions = []
74- self ._function_schemas = []
75- self ._icon = None
76- self ._installation_steps = []
77- self ._instructions = None
78- self ._name = None
79- self ._parameters = []
80- self ._software_version = None
81- self ._website = None
86+ def start (self ):
87+ asyncio .run (self ._setup ())
88+ return self
8289
8390 def on (self , event_type , callback ):
8491 if event_type not in self ._get_event_types ():
@@ -155,37 +162,55 @@ def set_instructions(self, instructions):
155162 return self
156163
157164 def set_function_schemas (self , function_schemas ):
158- if not isinstance (function_schemas , list ):
159- raise ValueError ('function_schemas must be an array.' )
165+ if (
166+ not isinstance (function_schemas , list ) or
167+ not all (isinstance (schema , dict ) and schema is not None for schema in function_schemas )
168+ ):
169+ raise ValueError ('functionSchemas must be an array of non-null objects.' )
160170 self ._function_schemas = function_schemas
161171 return self
162172
163173 def set_functions (self , functions ):
164- if not isinstance (functions , list ):
165- raise ValueError ('functions must be an array.' )
174+ if (
175+ not isinstance (functions , list ) or
176+ not all (callable (fn ) for fn in functions ) or
177+ not all (hasattr (fn , '__name__' ) and fn .__name__ .strip () for fn in functions )
178+ ):
179+ raise ValueError ('functions must be an array of named functions.' )
166180 self ._functions = functions
167181 return self
168182
169183 def enable_alarm (self ):
170- self ._publisher .enable_alarm ( )
184+ self ._publisher .publish_event ({ 'type' : 'alarm' } )
171185 return self
172186
173187 def send_medias (self , medias ):
174- self ._publisher .send_medias (medias )
188+ if not isinstance (medias , list ):
189+ medias = [medias ]
190+ if not all (isinstance (media , Media ) for media in medias ):
191+ raise ValueError ("medias must be a list of Media instances." )
192+ self ._publisher .publish_event ({
193+ 'type' : 'medias' ,
194+ 'medias' : [media .to_json () for media in medias ]
195+ })
175196 return self
176197
177198 def send_message (self , text ):
178- self ._publisher .send_message (text )
199+ if not isinstance (text , str ) or not text .strip ():
200+ raise ValueError ('text must be a non-empty string.' )
201+ self ._publisher .publish_event ({ 'type' : 'message' , 'text' : text })
179202 return self
180203
181204 def send_notification (self , text ):
182- self ._publisher .send_notification (text )
205+ if not isinstance (text , str ) or not text .strip ():
206+ raise ValueError ('text must be a non-empty string.' )
207+ self ._publisher .publish_event ({ 'type' : 'notification' , 'text' : text })
183208 return self
184209
185- def _get_event_types (self ):
186- with open (os .path .join (os .path .dirname (__file__ ), 'event_types.json' ), 'r' ) as f :
187- return json .load (f )
188-
189210 def _get_categories (self ):
190211 with open (os .path .join (os .path .dirname (__file__ ), 'categories.json' ), 'r' ) as f :
191212 return json .load (f )
213+
214+ def _get_event_types (self ):
215+ with open (os .path .join (os .path .dirname (__file__ ), 'event-types.json' ), 'r' ) as f :
216+ return json .load (f )
0 commit comments