From 077fa05720106417358bf9760dc2c1e9d6b0f830 Mon Sep 17 00:00:00 2001 From: Juris Date: Wed, 11 Aug 2021 17:46:52 +0300 Subject: [PATCH] python3 version: -> remove python setup.py test option as obsolete, use just tox instead -> add support only for Python 3.7 up and Django 2.2 up -> add argument --test for start_socket to be able to just quick test the launch of command -> fix syntax errors for print functions as per Python3 -> use migration from Django instead of south -> remove non-used module string.py as it interferes with standard module string -> add simple tests in test_models -> when running tox tests use only python3.8/3.9 and ignore long lines (ignored anyway before) --- CONTRIBUTING.rst | 1 - setup.py | 12 ++--- socket_server/__init__.py | 2 +- .../management/commands/start_socket.py | 26 ++++++++--- socket_server/migrations/0001_initial.py | 44 +++++++------------ socket_server/namespace.py | 3 +- socket_server/server.py | 4 +- tests/test_models.py | 21 ++++----- tox.ini | 5 ++- 9 files changed, 60 insertions(+), 58 deletions(-) diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index d961f68..e99769c 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -78,7 +78,6 @@ Now you can make your changes locally. tests, including testing other Python versions with tox:: $ flake8 socket_server tests - $ python setup.py test $ tox To get flake8 and tox, just pip install them into your virtualenv. diff --git a/setup.py b/setup.py index 02fd019..896cbb7 100755 --- a/setup.py +++ b/setup.py @@ -36,24 +36,24 @@ ], include_package_data=True, install_requires=[ - 'Django>=1.6.1', - 'wheel==0.23.0', + 'Django>=2.2', + 'wheel', 'autobahn', 'twisted', ], license="BSD", zip_safe=False, keywords='django-socket-server', + python_requires=">=3.7", classifiers=[ 'Development Status :: 2 - Pre-Alpha', 'Framework :: Django', 'Intended Audience :: Developers', 'License :: OSI Approved :: BSD License', 'Natural Language :: English', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.6', - 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.3', + 'Programming Language :: Python :: 3.7', + 'Programming Language :: Python :: 3.8', + 'Programming Language :: Python :: 3.9', ], ) diff --git a/socket_server/__init__.py b/socket_server/__init__.py index cba8e59..1f356cc 100644 --- a/socket_server/__init__.py +++ b/socket_server/__init__.py @@ -1 +1 @@ -__version__ = '0.0.4' \ No newline at end of file +__version__ = '1.0.0' diff --git a/socket_server/management/commands/start_socket.py b/socket_server/management/commands/start_socket.py index e150cbe..000e516 100644 --- a/socket_server/management/commands/start_socket.py +++ b/socket_server/management/commands/start_socket.py @@ -1,7 +1,10 @@ -from optparse import make_option +import os +import sys +import time +import signal +from importlib import import_module from django.core.management.base import BaseCommand -from django.utils.importlib import import_module from django.conf import settings from socket_server.server import SocketServerFactory @@ -38,10 +41,13 @@ class Command(BaseCommand): # type='int', # help='Port used for incomings websocket requests default to 3000',) # ) - + + def add_arguments(self, parser): + parser.add_argument('--test',action='store_true',help='Start server and close it') + def handle(self, *args, **options): options.update({'port': 3000}) - print 'Listening on port localhost:%s' % options['port'] + print('Listening on port localhost:%s' % options['port']) import sys @@ -50,8 +56,16 @@ def handle(self, *args, **options): log.startLogging(sys.stdout) - factory = SocketServerFactory("ws://localhost:%s" % options['port'], debug=False) + factory = SocketServerFactory("ws://localhost:%s" % options['port']) factory.setNamespaces(namespaces) reactor.listenTCP(options['port'], factory) - reactor.run() + if 'test' in options and options['test']: + r = os.fork() + if r == 0: + reactor.run() + else: + os.kill(r, signal.SIGKILL) + os.waitpid(r,0) + else: + reactor.run() diff --git a/socket_server/migrations/0001_initial.py b/socket_server/migrations/0001_initial.py index d467fd4..426d645 100644 --- a/socket_server/migrations/0001_initial.py +++ b/socket_server/migrations/0001_initial.py @@ -1,34 +1,22 @@ -# -*- coding: utf-8 -*- -from south.utils import datetime_utils as datetime -from south.db import db -from south.v2 import SchemaMigration -from django.db import models +# Generated by Django 3.2.6 on 2021-08-10 08:35 +from django.db import migrations, models -class Migration(SchemaMigration): - def forwards(self, orm): - # Adding model 'SocketLog' - db.create_table(u'socket_server_socketlog', ( - (u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), - ('created_at', self.gf('django.db.models.fields.DateTimeField')()), - ('content', self.gf('django.db.models.fields.TextField')()), - )) - db.send_create_signal(u'socket_server', ['SocketLog']) +class Migration(migrations.Migration): + initial = True - def backwards(self, orm): - # Deleting model 'SocketLog' - db.delete_table(u'socket_server_socketlog') + dependencies = [ + ] - - models = { - u'socket_server.socketlog': { - 'Meta': {'object_name': 'SocketLog'}, - 'content': ('django.db.models.fields.TextField', [], {}), - 'created_at': ('django.db.models.fields.DateTimeField', [], {}), - u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}) - } - } - - complete_apps = ['socket_server'] \ No newline at end of file + operations = [ + migrations.CreateModel( + name='SocketLog', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('content', models.TextField()), + ('created_at', models.DateTimeField(auto_now_add=True)), + ], + ), + ] diff --git a/socket_server/namespace.py b/socket_server/namespace.py index 98342f9..05ec91e 100644 --- a/socket_server/namespace.py +++ b/socket_server/namespace.py @@ -86,8 +86,7 @@ def _fire_callback(self, client, event, **kwargs): try: self.callbacks[event](client, **kwargs) except TypeError: - print 'An Error occurred calling event [%s]' % event - pass + print('An Error occurred calling event [%s]' % event) def _parse_inbound_message(self, message): """ Parse inbound JSON message """ diff --git a/socket_server/server.py b/socket_server/server.py index 2076d5c..2bb8da1 100644 --- a/socket_server/server.py +++ b/socket_server/server.py @@ -1,5 +1,3 @@ -import json - from autobahn.twisted.websocket import WebSocketServerProtocol, WebSocketServerFactory from .exceptions import NamespaceNotFound @@ -71,4 +69,4 @@ def stopFactory(self): namespaces = self.getNamespaces() for key in namespaces.keys(): namespace = namespaces[key] - namespace.on_stop('500', 'SHUTDOWN') \ No newline at end of file + namespace.on_stop('500', 'SHUTDOWN') diff --git a/tests/test_models.py b/tests/test_models.py index 4515bd4..ed1de27 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -7,22 +7,23 @@ Tests for `django-socket-server` models module. """ - -import os -import shutil +from io import StringIO from django.test import TestCase +from django.core.management import call_command -from socket_server import models +from socket_server.models import SocketLog class TestSocket_server(TestCase): - def setUp(self): - pass - def test_something(self): - pass + def test_model(self): + log = SocketLog(content='test log entry') + log.save() + self.assertTrue(log) - def tearDown(self): - pass + def test_command(self): + out = StringIO() + call_command('start_socket', stdout=out, test=0) + self.assertTrue(True) diff --git a/tox.ini b/tox.ini index 87d5d48..14ae371 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py26, py27, py33 +envlist = py38, py39 [testenv] setenv = @@ -7,3 +7,6 @@ setenv = commands = python runtests.py deps = -r{toxinidir}/requirements-test.txt + +[flake8] +ignore = E501