-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path__init__.py
More file actions
33 lines (21 loc) · 794 Bytes
/
__init__.py
File metadata and controls
33 lines (21 loc) · 794 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# -*- coding: utf-8 -*-
import socket
import sys
from contextdecorator import ContextDecorator
class no_connection(ContextDecorator):
_module = sys.modules[__name__]
def __init__(self, exception=IOError):
self.exception = exception
def _enable_socket(self):
setattr(self._module, '_socket_disabled', False)
def _disable_socket(self):
setattr(self._module, '_socket_disabled', True)
def guarded(*args, **kwargs):
if getattr(self._module, '_socket_disabled', False):
raise self.exception('Internet is disabled')
return socket.SocketType(*args, **kwargs)
socket.socket = guarded
def __enter__(self):
self._disable_socket()
def __exit__(self):
self._enable_socket()