I don't need that anymore
This commit is contained in:
parent
9dd3b82bf8
commit
667fb2bd4f
18
chatlib.py
18
chatlib.py
|
@ -13,13 +13,11 @@ tracing=False
|
||||||
def trace(*msg):
|
def trace(*msg):
|
||||||
if tracing: print(datetime.datetime.now(),*msg)
|
if tracing: print(datetime.datetime.now(),*msg)
|
||||||
|
|
||||||
class ChatClient():
|
event_queue=asyncio.Queue() #Contains Events. Everything in here must be an Event.
|
||||||
def __init__(self,homeserver,token):
|
listeners=[] # These respond to events. They're executed by workers processing the event queue. They should all be Listeners
|
||||||
self.event_queue=asyncio.Queue() #Contains Events. Everything in here must be an Event.
|
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").
|
||||||
self.listeners=[]
|
|
||||||
self.timers=[]
|
|
||||||
|
|
||||||
def addlistener(self,name=None,match=None):
|
def addlistener(name=None,match=None):
|
||||||
if isinstance(name,func) and match==None: match=name; name=None
|
if isinstance(name,func) and match==None: match=name; name=None
|
||||||
def __wrap__(funky):
|
def __wrap__(funky):
|
||||||
self.listeners.append(Listener(name,match,funky))
|
self.listeners.append(Listener(name,match,funky))
|
||||||
|
@ -33,13 +31,13 @@ class ChatClient():
|
||||||
try: await funky(*args,**kwargs)
|
try: await funky(*args,**kwargs)
|
||||||
except Exception: open('error '+str(datetime.datetime.now()),'w').write(traceback.format_exc())
|
except Exception: open('error '+str(datetime.datetime.now()),'w').write(traceback.format_exc())
|
||||||
await asyncio.sleep(timer)
|
await asyncio.sleep(timer)
|
||||||
self.timers.append(_wrapper)
|
timers.append(_wrapper)
|
||||||
return _wrapper
|
return _wrapper
|
||||||
return _loop
|
return _loop
|
||||||
|
|
||||||
async def process_queue(self):
|
async def process_queue():
|
||||||
item=await self.event_queue.get():
|
item=await event_queue.get():
|
||||||
for listener in self.listeners:
|
for listener in listeners:
|
||||||
if listener==item:
|
if listener==item:
|
||||||
await 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://spec.matrix.org
|
||||||
https://www.matrix.org/docs/develop/
|
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:
|
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')
|
if fetched.status_code!=200: raise Exception('fix ur shit')
|
||||||
try: return await fetched.json()
|
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