I don't need that anymore
This commit is contained in:
parent
9dd3b82bf8
commit
667fb2bd4f
20
chatlib.py
20
chatlib.py
|
@ -13,19 +13,17 @@ tracing=False
|
|||
def trace(*msg):
|
||||
if tracing: print(datetime.datetime.now(),*msg)
|
||||
|
||||
class ChatClient():
|
||||
def __init__(self,homeserver,token):
|
||||
self.event_queue=asyncio.Queue() #Contains Events. Everything in here must be an Event.
|
||||
self.listeners=[]
|
||||
self.timers=[]
|
||||
event_queue=asyncio.Queue() #Contains Events. Everything in here must be an Event.
|
||||
listeners=[] # These respond to events. They're executed by workers processing the event queue. They should all be Listeners
|
||||
timers=[] # These just execute on a schedule. That schedule can be "as soon as it exits". Should all be Timers, which include the property of whether to wait for exit before starting the next call or not (rarely relevant, so defaults to "no").
|
||||
|
||||
def addlistener(self,name=None,match=None):
|
||||
def addlistener(name=None,match=None):
|
||||
if isinstance(name,func) and match==None: match=name; name=None
|
||||
def __wrap__(funky):
|
||||
self.listeners.append(Listener(name,match,funky))
|
||||
return funky
|
||||
|
||||
def loop(timer=0):
|
||||
def loop(timer=0):
|
||||
def _loop(funky):
|
||||
@functools.wraps(funky)
|
||||
async def _wrapper(*args,**kwargs):
|
||||
|
@ -33,13 +31,13 @@ class ChatClient():
|
|||
try: await funky(*args,**kwargs)
|
||||
except Exception: open('error '+str(datetime.datetime.now()),'w').write(traceback.format_exc())
|
||||
await asyncio.sleep(timer)
|
||||
self.timers.append(_wrapper)
|
||||
timers.append(_wrapper)
|
||||
return _wrapper
|
||||
return _loop
|
||||
|
||||
async def process_queue(self):
|
||||
item=await self.event_queue.get():
|
||||
for listener in self.listeners:
|
||||
async def process_queue():
|
||||
item=await event_queue.get():
|
||||
for listener in listeners:
|
||||
if listener==item:
|
||||
await listener(item)
|
||||
|
||||
|
|
|
@ -8,3 +8,6 @@ Raw events will not be exposed, only processed events. This is not a "you have f
|
|||
|
||||
https://spec.matrix.org
|
||||
https://www.matrix.org/docs/develop/
|
||||
|
||||
Main chat lib defines API - is NOT a class, the module itself is a singleton
|
||||
Specific proto libs are classes, instansiate with the module + account info, it'll hook itself into the module. For convenience, if the module isn't provided they'll do it themselves, and provide a .run() wrapper. If that's used with a provided module (set a flag), they emit a warning.
|
15
matrixapi.py
15
matrixapi.py
|
@ -1,4 +1,17 @@
|
|||
async def request(self,endpoint='sync',method='GET', ver=0,headers={} *args,**kwargs):
|
||||
class MatrixLibHttp():
|
||||
def __init__(self,instance,token=None,username=None,password=None,chatlib=None):
|
||||
self.instance=instance
|
||||
self.https=True
|
||||
self.token=token
|
||||
if chatlib is None:
|
||||
import chatlib
|
||||
chatlib.addlistener() # TODO: probably should make that a decorator actually. Lol, it already is.
|
||||
|
||||
@property
|
||||
def baseurl(self):
|
||||
return 'http'+['','s'][self.https]+'://'+self.instance+'/_matrix/client'
|
||||
|
||||
async def request(self,endpoint='sync',method='GET', ver=0,headers={}, *args,**kwargs):
|
||||
async with self.session.request(method, f'{self.baseurl}/r{ver}/{endpoint}', headers=headers|{'Authorization':f'Bearer {self.token}'}, *args,**kwargs) as fetched:
|
||||
if fetched.status_code!=200: raise Exception('fix ur shit')
|
||||
try: return await fetched.json()
|
||||
|
|
13
matrixbot.py
13
matrixbot.py
|
@ -1,13 +0,0 @@
|
|||
from .chatlib import *
|
||||
import .listeners
|
||||
real_listeners=dict(filter(lambda x:isinstance(x,Listener),listeners.__dict__.items()))
|
||||
|
||||
class MatrixClient(MatrixClient):
|
||||
def __init__(self,*args,**kwargs):
|
||||
super().__init__(*args,**kwargs)
|
||||
self.listeners=real_listeners
|
||||
self.homeserver=homeserver
|
||||
self.token=token # Deal with login stuff later
|
||||
self.baseurl=f'https://{self.homeserver}/_matrix/client'
|
||||
self.session=ah.ClientSession()
|
||||
self.since=None
|
Loading…
Reference in New Issue
Block a user