U
    BZ3^
                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZm	Z	m
Z
mZmZmZ d dl mZ d dlmZmZmZ d dlmZ ej  edZG dd dZdS )	    N)DictTypeSetCallableList	Awaitable)CancelledError)ProtoBusAttachmentHost)defaultdictbridgec                   @   s   e Zd ZU eeee f ed< eeef ed< eeee f ed< e	e
g ef  ed< ddddZe
g ef dd	d
dZeee ddddZeddddZedddZddddZddddZddddZddddZdS )Bridge	protocols	instancesroutesdestructorsN)returnc              	   C   s   i | _ g | _i | _tt| _t | _tdd}t	
|| _W 5 Q R X tj }| | _| jD ]}t|dr`||  q`| jd D ](\}}| j| | | j| | qd S )Nzconfig.tomlrinitr   )r   r   r   r   setr   r
   busopentomlloadconfigcoreZpluginsZPluginLoaderZload_allhasattrr   add)selffZplugin_loaderZpluginab r#   7/home/sam/code/telegram-teamspeak-bridge/core/bridge.py__init__   s    




zBridge.__init__)fnr   c                 C   s   | j | d S N)r   append)r   r&   r#   r#   r$   add_destructor)   s    zBridge.add_destructor)nameprotor   c                 C   s   || j |< d S r'   )r   )r   r*   r+   r#   r#   r$   add_protocol,   s    zBridge.add_protocol)hostr   c                 C   s
   || _ d S r'   Zattachment_host)r   r-   r#   r#   r$   set_attachment_host/   s    zBridge.set_attachment_hostc                 C   s   | j S r'   r.   r   r#   r#   r$   get_attachment_host2   s    zBridge.get_attachment_hostc                    sJ   z |   I d H  |  I d H  W n$ tk
rD   |  I d H   Y nX d S r'   )	constructrun_loopr   run_destructorsr0   r#   r#   r$   start5   s    zBridge.startc                    s   | j D ]}| I d H  qd S r'   )r   )r   Z
destructorr#   r#   r$   r4   =   s    
zBridge.run_destructorsc              	      s   t  | _| jd  D ]\}}z| j|d  }W n0 tk
rb   td|d   t	
  Y nX td|  | }|| | j||I d H  || j|< qd S )Nr   r+   zUnknown protocol: zConstructing instance: )asyncioget_event_looploopr   itemsr   KeyErrorloggererrorsysexitinfor5   r   Zport_forr   )r   r*   Zcfgr+   Z
proto_instr#   r#   r$   r2   A   s    
zBridge.constructc                    s|   | j  I d H \}}td| d|  | j| d|j  D ]4}|d\}}|| jkr@| j| ||I d H  q@q d S )NzGot message on bus from z: #)	r   Zget_messager;   r?   r   Zchannelsplitr   Zsend_message)r   instmessagedestZ
to_channelr#   r#   r$   r3   Q   s    
zBridge.run_loop)__name__
__module____qualname__r   strr   r	   __annotations__r   r   r   r   r%   r)   r,   r   r/   r1   r5   r4   r2   r3   r#   r#   r#   r$   r      s   
r   )r6   loggingZcore.pluginsr   Zcore.loggingr   r=   typingr   r   r   r   r   r   r   Z
core.typesr	   r
   r   collectionsr   r   	getLoggerr;   r   r#   r#   r#   r$   <module>   s    

