Debian Codegraph @

python/proton/_selectable.py

raw
1 #
2 # Licensed to the Apache Software Foundation (ASF) under one
3 # or more contributor license agreements. See the NOTICE file
4 # distributed with this work for additional information
5 # regarding copyright ownership. The ASF licenses this file
6 # to you under the Apache License, Version 2.0 (the
7 # "License"); you may not use this file except in compliance
8 # with the License. You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing,
13 # software distributed under the License is distributed on an
14 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 # KIND, either express or implied. See the License for the
16 # specific language governing permissions and limitations
17 # under the License.
18 #
19
20 from typing import Optional, Union, TYPE_CHECKING, Any
21
22 from ._events import Event
23 from ._io import PN_INVALID_SOCKET
24
25 if TYPE_CHECKING:
26 from ._events import EventType
27 from ._reactor import Container, EventInjector
28 from socket import socket
29
30
31 class Selectable(object):
32
33 def __init__(
34 self,
35 delegate: Optional[Union['EventInjector', 'socket']],
36 reactor: 'Container',
37 ) -> None:
38 self._delegate = delegate
39 self.reading = False
40 self.writing = False
41 self._deadline = 0
42 self._terminal = False
43 self._released = False
44 self._terminated = False
45 self._reactor = reactor
46 self.push_event(self, Event.SELECTABLE_INIT)
47
48 def close(self) -> None:
49 if self._delegate and not self._released:
50 self._delegate.close()
51
52 def fileno(self) -> int:
53 if self._delegate:
54 return self._delegate.fileno()
55 else:
56 return PN_INVALID_SOCKET
57
58 def __getattr__(self, name: str) -> Any:
59 return getattr(self._delegate, name)
60
61 @property
62 def deadline(self) -> Optional[float]:
63 tstamp = self._deadline
64 if tstamp:
65 return tstamp
66 else:
67 return None
68
69 @deadline.setter
70 def deadline(self, deadline: Optional[float]) -> None:
71 if not deadline:
72 self._deadline = 0
73 else:
74 self._deadline = deadline
75
76 def push_event(
77 self,
78 context: 'Selectable',
79 etype: 'EventType',
80 ) -> None:
81 self._reactor.push_event(context, etype)
82
83 def update(self) -> None:
84 if not self._terminated:
85 if self._terminal:
86 self._terminated = True
87 self.push_event(self, Event.SELECTABLE_FINAL)
88 else:
89 self.push_event(self, Event.SELECTABLE_UPDATED)
90
91 def readable(self) -> None:
92 self.push_event(self, Event.SELECTABLE_READABLE)
93
94 def writable(self) -> None:
95 self.push_event(self, Event.SELECTABLE_WRITABLE)
96
97 def expired(self) -> None:
98 self.push_event(self, Event.SELECTABLE_EXPIRED)
99
100 @property
101 def is_terminal(self) -> bool:
102 return self._terminal
103
104 def terminate(self) -> None:
105 self._terminal = True
106
107 def release(self) -> None:
108 self._released = True