35
35
MATRIX_MESSAGE_TEMPLATE : str = "<{username} ({uid})> {message}"
36
36
37
37
38
- class Bridge_ConfigException (Exception ):
38
+ class BridgeConfigException (Exception ):
39
39
pass
40
40
41
41
42
- class Bridge_FatalMatrixException (Exception ):
42
+ class BridgeFatalMatrixException (Exception ):
43
43
pass
44
44
45
45
46
- class Bridge_FatalZulipException (Exception ):
46
+ class BridgeFatalZulipException (Exception ):
47
47
pass
48
48
49
49
@@ -86,7 +86,7 @@ async def create(
86
86
# the new messages and not with all the old ones.
87
87
sync_response : Union [SyncResponse , SyncError ] = await matrix_client .sync ()
88
88
if isinstance (sync_response , nio .SyncError ):
89
- raise Bridge_FatalMatrixException (sync_response .message )
89
+ raise BridgeFatalMatrixException (sync_response .message )
90
90
91
91
return matrix_to_zulip
92
92
@@ -117,10 +117,10 @@ async def _matrix_to_zulip(self, room: nio.MatrixRoom, event: nio.Event) -> None
117
117
)
118
118
except Exception as exception :
119
119
# Generally raised when user is forbidden
120
- raise Bridge_FatalZulipException (exception )
120
+ raise BridgeFatalZulipException (exception )
121
121
if result ["result" ] != "success" :
122
122
# Generally raised when API key is invalid
123
- raise Bridge_FatalZulipException (result ["msg" ])
123
+ raise BridgeFatalZulipException (result ["msg" ])
124
124
125
125
# Update the bot's read marker in order to show the other users which
126
126
# messages are already processed by the bot.
@@ -194,14 +194,14 @@ async def matrix_join_rooms(self) -> None:
194
194
for room_id in self .matrix_config ["bridges" ]:
195
195
result : Union [JoinResponse , JoinError ] = await self .matrix_client .join (room_id )
196
196
if isinstance (result , nio .JoinError ):
197
- raise Bridge_FatalMatrixException (str (result ))
197
+ raise BridgeFatalMatrixException (str (result ))
198
198
199
199
async def matrix_login (self ) -> None :
200
200
result : Union [LoginResponse , LoginError ] = await self .matrix_client .login (
201
201
self .matrix_config ["password" ]
202
202
)
203
203
if isinstance (result , nio .LoginError ):
204
- raise Bridge_FatalMatrixException (str (result ))
204
+ raise BridgeFatalMatrixException (str (result ))
205
205
206
206
async def run (self ) -> None :
207
207
print ("Starting message handler on Matrix client" )
@@ -230,7 +230,7 @@ def __init__(
230
230
# Precompute the url of the Zulip server, needed later.
231
231
result : Dict [str , Any ] = self .zulip_client .get_server_settings ()
232
232
if result ["result" ] != "success" :
233
- raise Bridge_FatalZulipException ("cannot get server settings" )
233
+ raise BridgeFatalZulipException ("cannot get server settings" )
234
234
self .server_url : str = result ["realm_uri" ]
235
235
236
236
@classmethod
@@ -250,7 +250,7 @@ def _matrix_send(self, **kwargs: Any) -> None:
250
250
self .matrix_client .room_send (** kwargs ), self .loop
251
251
).result ()
252
252
if isinstance (result , nio .RoomSendError ):
253
- raise Bridge_FatalMatrixException (str (result ))
253
+ raise BridgeFatalMatrixException (str (result ))
254
254
255
255
def _zulip_to_matrix (self , msg : Dict [str , Any ]) -> None :
256
256
logging .debug ("_zulip_to_matrix; msg: %s" , msg )
@@ -303,12 +303,12 @@ def ensure_stream_membership(self) -> None:
303
303
for stream , _ in self .zulip_config ["bridges" ]:
304
304
result : Dict [str , Any ] = self .zulip_client .get_stream_id (stream )
305
305
if result ["result" ] == "error" :
306
- raise Bridge_FatalZulipException (f"cannot access stream '{ stream } ': { result } " )
306
+ raise BridgeFatalZulipException (f"cannot access stream '{ stream } ': { result } " )
307
307
if result ["result" ] != "success" :
308
- raise Bridge_FatalZulipException (f"cannot checkout stream id for stream '{ stream } '" )
308
+ raise BridgeFatalZulipException (f"cannot checkout stream id for stream '{ stream } '" )
309
309
result = self .zulip_client .add_subscriptions (streams = [{"name" : stream }])
310
310
if result ["result" ] != "success" :
311
- raise Bridge_FatalZulipException (f"cannot subscribe to stream '{ stream } ': { result } " )
311
+ raise BridgeFatalZulipException (f"cannot subscribe to stream '{ stream } ': { result } " )
312
312
313
313
def get_matrix_room_for_zulip_message (self , msg : Dict [str , Any ]) -> Optional [str ]:
314
314
"""Check whether we want to process the given message.
@@ -459,10 +459,10 @@ def read_configuration(config_file: str) -> Dict[str, Dict[str, Any]]:
459
459
try :
460
460
config .read (config_file )
461
461
except configparser .Error as exception :
462
- raise Bridge_ConfigException (str (exception ))
462
+ raise BridgeConfigException (str (exception ))
463
463
464
464
if set (config .sections ()) < {"matrix" , "zulip" }:
465
- raise Bridge_ConfigException ("Please ensure the configuration has zulip & matrix sections." )
465
+ raise BridgeConfigException ("Please ensure the configuration has zulip & matrix sections." )
466
466
467
467
result : Dict [str , Dict [str , Any ]] = {"matrix" : {}, "zulip" : {}}
468
468
# For Matrix: create a mapping with the Matrix room_ids as keys and
@@ -484,7 +484,7 @@ def read_configuration(config_file: str) -> Dict[str, Dict[str, Any]]:
484
484
485
485
if section .startswith ("additional_bridge" ):
486
486
if section_keys != bridge_key_set :
487
- raise Bridge_ConfigException (
487
+ raise BridgeConfigException (
488
488
f"Please ensure the bridge configuration section { section } contain the following keys: { bridge_key_set } ."
489
489
)
490
490
@@ -493,7 +493,7 @@ def read_configuration(config_file: str) -> Dict[str, Dict[str, Any]]:
493
493
result ["matrix" ]["bridges" ][section_config ["room_id" ]] = zulip_target
494
494
elif section == "matrix" :
495
495
if section_keys != matrix_full_key_set :
496
- raise Bridge_ConfigException (
496
+ raise BridgeConfigException (
497
497
"Please ensure the matrix configuration section contains the following keys: %s."
498
498
% str (matrix_full_key_set )
499
499
)
@@ -505,10 +505,10 @@ def read_configuration(config_file: str) -> Dict[str, Dict[str, Any]]:
505
505
506
506
# Verify the format of the Matrix user ID.
507
507
if re .fullmatch (r"@[^:]+:.+" , result ["matrix" ]["mxid" ]) is None :
508
- raise Bridge_ConfigException ("Malformatted mxid." )
508
+ raise BridgeConfigException ("Malformatted mxid." )
509
509
elif section == "zulip" :
510
510
if section_keys != zulip_full_key_set :
511
- raise Bridge_ConfigException (
511
+ raise BridgeConfigException (
512
512
"Please ensure the zulip configuration section contains the following keys: %s."
513
513
% str (zulip_full_key_set )
514
514
)
@@ -555,9 +555,9 @@ async def run(zulip_config: Dict[str, Any], matrix_config: Dict[str, Any], no_no
555
555
556
556
await asyncio .gather (matrix_to_zulip .run (), zulip_to_matrix .run ())
557
557
558
- except Bridge_FatalMatrixException as exception :
558
+ except BridgeFatalMatrixException as exception :
559
559
sys .exit (f"Matrix bridge error: { exception } " )
560
- except Bridge_FatalZulipException as exception :
560
+ except BridgeFatalZulipException as exception :
561
561
sys .exit (f"Zulip bridge error: { exception } " )
562
562
except zulip .ZulipError as exception :
563
563
sys .exit (f"Zulip error: { exception } " )
@@ -571,7 +571,7 @@ async def run(zulip_config: Dict[str, Any], matrix_config: Dict[str, Any], no_no
571
571
572
572
def write_sample_config (target_path : str , zuliprc : Optional [str ]) -> None :
573
573
if os .path .exists (target_path ):
574
- raise Bridge_ConfigException (f"Path '{ target_path } ' exists; not overwriting existing file." )
574
+ raise BridgeConfigException (f"Path '{ target_path } ' exists; not overwriting existing file." )
575
575
576
576
sample_dict : OrderedDict [str , OrderedDict [str , str ]] = OrderedDict (
577
577
(
@@ -613,20 +613,20 @@ def write_sample_config(target_path: str, zuliprc: Optional[str]) -> None:
613
613
614
614
if zuliprc is not None :
615
615
if not os .path .exists (zuliprc ):
616
- raise Bridge_ConfigException (f"Zuliprc file '{ zuliprc } ' does not exist." )
616
+ raise BridgeConfigException (f"Zuliprc file '{ zuliprc } ' does not exist." )
617
617
618
618
zuliprc_config : configparser .ConfigParser = configparser .ConfigParser ()
619
619
try :
620
620
zuliprc_config .read (zuliprc )
621
621
except configparser .Error as exception :
622
- raise Bridge_ConfigException (str (exception ))
622
+ raise BridgeConfigException (str (exception ))
623
623
624
624
try :
625
625
sample_dict ["zulip" ]["email" ] = zuliprc_config ["api" ]["email" ]
626
626
sample_dict ["zulip" ]["site" ] = zuliprc_config ["api" ]["site" ]
627
627
sample_dict ["zulip" ]["api_key" ] = zuliprc_config ["api" ]["key" ]
628
628
except KeyError as exception :
629
- raise Bridge_ConfigException ("You provided an invalid zuliprc file: " + str (exception ))
629
+ raise BridgeConfigException ("You provided an invalid zuliprc file: " + str (exception ))
630
630
631
631
sample : configparser .ConfigParser = configparser .ConfigParser ()
632
632
sample .read_dict (sample_dict )
@@ -648,7 +648,7 @@ def main() -> None:
648
648
if options .sample_config :
649
649
try :
650
650
write_sample_config (options .sample_config , options .zuliprc )
651
- except Bridge_ConfigException as exception :
651
+ except BridgeConfigException as exception :
652
652
print (f"Could not write sample config: { exception } " )
653
653
sys .exit (1 )
654
654
if options .zuliprc is None :
@@ -667,7 +667,7 @@ def main() -> None:
667
667
668
668
try :
669
669
config : Dict [str , Dict [str , Any ]] = read_configuration (options .config )
670
- except Bridge_ConfigException as exception :
670
+ except BridgeConfigException as exception :
671
671
print (f"Could not parse config file: { exception } " )
672
672
sys .exit (1 )
673
673
0 commit comments