Skip to content
This repository was archived by the owner on Apr 29, 2022. It is now read-only.

Commit 4cd13b0

Browse files
committed
Merge pull request #32 from oblalex/master
Add support of Flask blueprints
2 parents a52542a + 2b414ea commit 4cd13b0

File tree

4 files changed

+141
-3
lines changed

4 files changed

+141
-3
lines changed

.gitignore

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
5+
# C extensions
6+
*.so
7+
8+
# Distribution / packaging
9+
.Python
10+
env/
11+
bin/
12+
build/
13+
develop-eggs/
14+
dist/
15+
eggs/
16+
lib/
17+
lib64/
18+
parts/
19+
sdist/
20+
var/
21+
*.egg-info/
22+
.installed.cfg
23+
*.egg
24+
25+
# Installer logs
26+
pip-log.txt
27+
pip-delete-this-directory.txt
28+
29+
# Unit test / coverage reports
30+
htmlcov/
31+
.tox/
32+
.coverage
33+
.cache
34+
nosetests.xml
35+
coverage.xml
36+
37+
# Mr Developer
38+
.mr.developer.cfg
39+
.project
40+
.pydevproject
41+
42+
# Rope
43+
.ropeproject
44+
45+
# Sphinx documentation
46+
docs/_build/
47+
48+
# System
49+
.DS_Store
50+

README.rst

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,26 @@ Elegant WebSockets for your Flask apps.
55

66
.. image:: http://farm4.staticflickr.com/3689/9755961864_577e32a106_c.jpg
77

8+
9+
Simple usage of ``route`` decorator:
10+
811
.. code-block:: python
912
1013
from flask import Flask
1114
from flask_sockets import Sockets
1215
16+
1317
app = Flask(__name__)
1418
sockets = Sockets(app)
1519
20+
1621
@sockets.route('/echo')
1722
def echo_socket(ws):
1823
while not ws.closed:
1924
message = ws.receive()
2025
ws.send(message)
2126
27+
2228
@app.route('/')
2329
def hello():
2430
return 'Hello World!'
@@ -31,6 +37,43 @@ Elegant WebSockets for your Flask apps.
3137
server.serve_forever()
3238
3339
40+
Usage of `Flask blueprints`_:
41+
42+
.. code-block:: python
43+
44+
from flask import Flask, Blueprint
45+
from flask_sockets import Sockets
46+
47+
48+
html = Blueprint(r'html', __name__)
49+
ws = Blueprint(r'ws', __name__)
50+
51+
52+
@html.route('/')
53+
def hello():
54+
return 'Hello World!'
55+
56+
@ws.route('/echo')
57+
def echo_socket(socket):
58+
while not socket.closed:
59+
message = socket.receive()
60+
socket.send(message)
61+
62+
63+
app = Flask(__name__)
64+
sockets = Sockets(app)
65+
66+
app.register_blueprint(html, url_prefix=r'/')
67+
sockets.register_blueprint(ws, url_prefix=r'/')
68+
69+
70+
if __name__ == "__main__":
71+
from gevent import pywsgi
72+
from geventwebsocket.handler import WebSocketHandler
73+
server = pywsgi.WSGIServer(('', 5000), app, handler_class=WebSocketHandler)
74+
server.serve_forever()
75+
76+
3477
Serving WebSockets in Python was really difficult. Now it's not.
3578

3679

@@ -85,6 +128,12 @@ The basic methods are fairly straightforward — 
85128
Release History
86129
---------------
87130

131+
v0.2.1
132+
~~~~~~
133+
134+
- Add support of `Flask blueprints`_.
135+
136+
88137
v0.2.0
89138
~~~~~~
90139

@@ -98,5 +147,7 @@ v0.1.0
98147
- Initial release.
99148

100149

150+
.. _Flask blueprints: http://flask.pocoo.org/docs/latest/blueprints/
151+
101152

102153

flask_sockets.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# -*- coding: utf-8 -*-
2+
23
from werkzeug.routing import Map, Rule
34
from werkzeug.exceptions import NotFound
45

6+
57
def log_request(self):
68
log = self.server.log
79
if log:
@@ -18,13 +20,13 @@ def log_request(self):
1820
except ImportError:
1921
pass
2022

23+
2124
if 'gevent' in locals():
2225
# Freedom-Patch logger for Gunicorn.
2326
if hasattr(gevent, 'pywsgi'):
2427
gevent.pywsgi.WSGIHandler.log_request = log_request
2528

2629

27-
2830
class SocketMiddleware(object):
2931

3032
def __init__(self, wsgi_app, app, socket):
@@ -49,7 +51,19 @@ def __call__(self, environ, start_response):
4951
class Sockets(object):
5052

5153
def __init__(self, app=None):
54+
#: Compatibility with 'Flask' application.
55+
#: The :class:`~werkzeug.routing.Map` for this instance. You can use
56+
#: this to change the routing converters after the class was created
57+
#: but before any routes are connected.
5258
self.url_map = Map()
59+
60+
#: Compatibility with 'Flask' application.
61+
#: All the attached blueprints in a dictionary by name. Blueprints
62+
#: can be attached multiple times so this dictionary does not tell
63+
#: you how often they got attached.
64+
self.blueprints = {}
65+
self._blueprint_order = []
66+
5367
if app:
5468
self.init_app(app)
5569

@@ -67,6 +81,29 @@ def decorator(f):
6781
def add_url_rule(self, rule, _, f, **options):
6882
self.url_map.add(Rule(rule, endpoint=f))
6983

84+
def register_blueprint(self, blueprint, **options):
85+
"""
86+
Registers a blueprint for web sockets like for 'Flask' application.
87+
88+
Decorator :meth:`~flask.app.setupmethod` is not applied, because it
89+
requires ``debug`` and ``_got_first_request`` attributes to be defined.
90+
"""
91+
first_registration = False
92+
93+
if blueprint.name in self.blueprints:
94+
assert self.blueprints[blueprint.name] is blueprint, (
95+
'A blueprint\'s name collision occurred between %r and '
96+
'%r. Both share the same name "%s". Blueprints that '
97+
'are created on the fly need unique names.'
98+
% (blueprint, self.blueprints[blueprint.name], blueprint.name))
99+
else:
100+
self.blueprints[blueprint.name] = blueprint
101+
self._blueprint_order.append(blueprint)
102+
first_registration = True
103+
104+
blueprint.register(self, options, first_registration)
105+
106+
70107
# CLI sugar.
71108
if 'Worker' in locals():
72109
worker = Worker

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
setup(
1313
name='Flask-Sockets',
14-
version='0.2.0',
14+
version='0.2.1',
1515
url='https://github.com/kennethreitz/flask-sockets',
1616
license='See License',
1717
author='Kenneth Reitz',
@@ -36,4 +36,4 @@
3636
'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
3737
'Topic :: Software Development :: Libraries :: Python Modules'
3838
]
39-
)
39+
)

0 commit comments

Comments
 (0)