Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1"""LICENSE 

2Copyright 2018 Hermann Krumrey <hermann@krumreyh.com> 

3 

4This file is part of bokkichat. 

5 

6bokkichat is free software: you can redistribute it and/or modify 

7it under the terms of the GNU General Public License as published by 

8the Free Software Foundation, either version 3 of the License, or 

9(at your option) any later version. 

10 

11bokkichat is distributed in the hope that it will be useful, 

12but WITHOUT ANY WARRANTY; without even the implied warranty of 

13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

14GNU General Public License for more details. 

15 

16You should have received a copy of the GNU General Public License 

17along with bokkichat. If not, see <http://www.gnu.org/licenses/>. 

18LICENSE""" 

19 

20import time 

21import logging 

22from typing import Callable, List, Type 

23from bokkichat.entities.Address import Address 

24from bokkichat.entities.message.Message import Message 

25from bokkichat.settings.Settings import Settings 

26 

27 

28class Connection: 

29 """ 

30 Class that defines methods a Connection must implement. 

31 A connection is the central class in bokkichat and handles all 

32 communications with the chat services. 

33 """ 

34 

35 def __init__(self, settings: Settings): 

36 """ 

37 Initializes the connection, with credentials provided by a 

38 Settings object. 

39 :param settings: The settings for the connection 

40 """ 

41 self.settings = settings 

42 self.logger = logging.getLogger(self.__class__.__name__) 

43 self.looping = False 

44 self.loop_break = False 

45 

46 @classmethod 

47 def name(cls) -> str: 

48 """ 

49 The name of the connection class 

50 :return: The connection class name 

51 """ 

52 raise NotImplementedError() 

53 

54 @property 

55 def address(self) -> Address: 

56 """ 

57 A connection must be able to specify its own entities 

58 :return: The entities of the connection 

59 """ 

60 raise NotImplementedError() 

61 

62 @classmethod 

63 def settings_cls(cls) -> Type[Settings]: 

64 """ 

65 The settings class used by this connection 

66 :return: The settings class 

67 """ 

68 raise NotImplementedError() 

69 

70 def send(self, message: Message): 

71 """ 

72 Sends a message. A message may be either a TextMessage 

73 or a MediaMessage. 

74 :param message: The message to send 

75 :return: None 

76 """ 

77 raise NotImplementedError() 

78 

79 def receive(self) -> List[Message]: 

80 """ 

81 Receives all pending messages. 

82 :return: A list of pending Message objects 

83 """ 

84 raise NotImplementedError() 

85 

86 def loop(self, callback: Callable, sleep_time: int = 1): 

87 """ 

88 Starts a loop that periodically checks for new messages, calling 

89 a provided callback function in the process. 

90 :param callback: The callback function to call for each 

91 received message. 

92 The callback should have the following format: 

93 lambda connection, message: do_stuff() 

94 :param sleep_time: The time to sleep between loops 

95 :return: None 

96 """ 

97 self.looping = True 

98 while True: 

99 for message in self.receive(): 

100 callback(self, message) 

101 

102 if self.loop_break: 

103 self.loop_break = False 

104 break 

105 

106 time.sleep(sleep_time) 

107 self.looping = False 

108 

109 def close(self): 

110 """ 

111 Disconnects the Connection. 

112 :return: None 

113 """ 

114 raise NotImplementedError() 

115 

116 @classmethod 

117 def from_serialized_settings(cls, serialized: str) -> "Connection": 

118 """ 

119 Generates a Connection using serialized settings 

120 :param serialized: The serialized settings 

121 :return: The generated connection 

122 """ 

123 settings = cls.settings_cls().deserialize(serialized) 

124 return cls(settings)