Skip to content

Commit

Permalink
initial files
Browse files Browse the repository at this point in the history
  • Loading branch information
dvl committed Apr 8, 2016
0 parents commit a945785
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 0 deletions.
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2016 André Luiz <[email protected]>

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.

2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include README.rst
include LICENSE
6 changes: 6 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Usage

from internet_sabotage import no_connection

with no_connection():
response = requests.get('http://httpbin.org/ip')
33 changes: 33 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[wheel]
universal = 1
34 changes: 34 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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='[email protected]',
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',
)

0 comments on commit a945785

Please sign in to comment.