diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b292856 --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2016 André Luiz + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..a5021c6 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,2 @@ +include README.rst +include LICENSE diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..45fbbea --- /dev/null +++ b/README.rst @@ -0,0 +1,6 @@ +# Usage + +from internet_sabotage import no_connection + +with no_connection(): + response = requests.get('http://httpbin.org/ip') diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..0577b9c --- /dev/null +++ b/__init__.py @@ -0,0 +1,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() diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..5e40900 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,2 @@ +[wheel] +universal = 1 diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..d07b964 --- /dev/null +++ b/setup.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from setuptools import setup, find_packages + +with open('README.rst', 'r') as f: + long_description = f.read() + +setup( + name='internet-sabotage', + version='0.1.0', + description='Disable network connection for Testing', + long_description=long_description, + author='André Luiz', + author_email='contato@xdvl.info', + url='https://github.com/dvl/python-internet-sabotage', + license='MIT', + classifiers=[ + 'Development Status :: 4 - Beta', + 'Intended Audience :: Developers', + 'License :: OSI Approved :: MIT License', + 'Operating System :: OS Independent', + 'Programming Language :: Python :: 2', + 'Programming Language :: Python :: 3', + 'Topic :: Software Development :: Libraries', + 'Topic :: Software Development :: Testing', + ], + keywords='internet disable unittest', + packages=find_packages(), + install_requires=[ + 'contextdecorator', + ], + test_suite='tests', +)