mirror of
				https://github.com/avatao-content/baseimage-tutorial-framework
				synced 2025-11-04 11:52:54 +00:00 
			
		
		
		
	Rearrange project and dockerize
This commit is contained in:
		
							
								
								
									
										30
									
								
								src/app/app.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								src/app/app.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,30 @@
 | 
			
		||||
import logging
 | 
			
		||||
import sys
 | 
			
		||||
 | 
			
		||||
import tornado
 | 
			
		||||
import zmq
 | 
			
		||||
from tornado.web import Application
 | 
			
		||||
from tornado.ioloop import IOLoop
 | 
			
		||||
 | 
			
		||||
from config import WEB_PORT
 | 
			
		||||
from handlers import MainHandler, ZMQWebSocketHandler
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    routes = [
 | 
			
		||||
        (r'/', MainHandler),
 | 
			
		||||
        (r'/ws', ZMQWebSocketHandler),
 | 
			
		||||
    ]
 | 
			
		||||
    application = Application(
 | 
			
		||||
        routes,
 | 
			
		||||
        template_path=r'templates/',
 | 
			
		||||
        static_path=r'static/',
 | 
			
		||||
        autoreload=True
 | 
			
		||||
    )
 | 
			
		||||
    application.listen(WEB_PORT)
 | 
			
		||||
    logging.getLogger().setLevel(logging.DEBUG)
 | 
			
		||||
    logging.debug('Python version: {}'.format(sys.version[:5]))
 | 
			
		||||
    logging.debug('Tornado version: {}'.format(tornado.version))
 | 
			
		||||
    logging.debug('ZeroMQ version: {}'.format(zmq.zmq_version()))
 | 
			
		||||
    logging.debug('PyZMQ version: {}'.format(zmq.pyzmq_version()))
 | 
			
		||||
    logging.info('Tornado application listening on port {}'.format(WEB_PORT))
 | 
			
		||||
    IOLoop.instance().start()
 | 
			
		||||
							
								
								
									
										24
									
								
								src/app/buttons.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								src/app/buttons.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,24 @@
 | 
			
		||||
from transitions import Machine
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Buttons:
 | 
			
		||||
    states = ['ayy', 'bee', 'cee']
 | 
			
		||||
    transitions = [
 | 
			
		||||
        {'trigger': 'anchor_a', 'source': 'ayy', 'dest': 'bee'},
 | 
			
		||||
        {'trigger': 'anchor_b', 'source': 'bee', 'dest': 'cee'},
 | 
			
		||||
        {'trigger': 'anchor_c', 'source': 'cee', 'dest': 'ayy'},
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    def __init__(self, handler=None):
 | 
			
		||||
        self.handler = handler
 | 
			
		||||
        self.machine = Machine(model=self,
 | 
			
		||||
                               states=Buttons.states,
 | 
			
		||||
                               transitions=Buttons.transitions,
 | 
			
		||||
                               initial='ayy',
 | 
			
		||||
                               send_event=True,
 | 
			
		||||
                               ignore_invalid_triggers=True,
 | 
			
		||||
                               after_state_change='forward_message')
 | 
			
		||||
 | 
			
		||||
    def forward_message(self, event_data):
 | 
			
		||||
        message = event_data.kwargs.get('message')
 | 
			
		||||
        self.handler.send_message(message)
 | 
			
		||||
							
								
								
									
										2
									
								
								src/app/handlers/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								src/app/handlers/__init__.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,2 @@
 | 
			
		||||
from .main_handler import MainHandler
 | 
			
		||||
from .zmq_websocket_handler import ZMQWebSocketHandler
 | 
			
		||||
							
								
								
									
										6
									
								
								src/app/handlers/main_handler.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								src/app/handlers/main_handler.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
			
		||||
from tornado.web import RequestHandler
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class MainHandler(RequestHandler):
 | 
			
		||||
    def get(self, *args, **kwargs):
 | 
			
		||||
        self.render('index.html')
 | 
			
		||||
							
								
								
									
										60
									
								
								src/app/handlers/zmq_websocket_handler.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								src/app/handlers/zmq_websocket_handler.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,60 @@
 | 
			
		||||
import logging
 | 
			
		||||
 | 
			
		||||
import json
 | 
			
		||||
from tornado.websocket import WebSocketHandler
 | 
			
		||||
import zmq
 | 
			
		||||
from zmq.eventloop.zmqstream import ZMQStream
 | 
			
		||||
from zmq.eventloop import ioloop
 | 
			
		||||
 | 
			
		||||
from config import PUBLISHER_PORT, RECEIVER_PORT
 | 
			
		||||
from buttons import Buttons
 | 
			
		||||
 | 
			
		||||
ioloop.install()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ZMQWebSocketHandler(WebSocketHandler):
 | 
			
		||||
    def __init__(self, application, request, zmq_context=None, **kwargs):
 | 
			
		||||
        super().__init__(application, request, **kwargs)
 | 
			
		||||
        self.zmq_context = zmq_context or zmq.Context.instance()
 | 
			
		||||
        self.zmq_pull_socket = self.zmq_context.socket(zmq.PULL)
 | 
			
		||||
        self.zmq_pull_stream = ZMQStream(self.zmq_pull_socket)
 | 
			
		||||
        self.zmq_pub_socket = self.zmq_context.socket(zmq.PUB)
 | 
			
		||||
        self.fsm = Buttons(self)
 | 
			
		||||
 | 
			
		||||
    def open(self, *args, **kwargs):
 | 
			
		||||
        pub_socket_address = 'tcp://*:{}'.format(PUBLISHER_PORT)
 | 
			
		||||
        self.zmq_pub_socket.bind(pub_socket_address)
 | 
			
		||||
        logging.debug('Pub socket bound to {}'.format(pub_socket_address))
 | 
			
		||||
        pull_socket_address = 'tcp://*:{}'.format(RECEIVER_PORT)
 | 
			
		||||
        self.zmq_pull_socket.bind(pull_socket_address)
 | 
			
		||||
        logging.debug('Pull socket bound to {}'.format(pull_socket_address))
 | 
			
		||||
 | 
			
		||||
        def zmq_callback(msg_parts):
 | 
			
		||||
            anchor, data = msg_parts
 | 
			
		||||
            logging.debug('Received on pull socket: {}'.format(data.decode()))
 | 
			
		||||
            self.write_message(data.decode())
 | 
			
		||||
 | 
			
		||||
        self.zmq_pull_stream.on_recv(zmq_callback)
 | 
			
		||||
 | 
			
		||||
    def on_message(self, message):
 | 
			
		||||
        logging.debug('Received on WebSocket: {}'.format(message))
 | 
			
		||||
        self.fsm.trigger(self._parse_anchor(message), message=message)
 | 
			
		||||
 | 
			
		||||
    def send_message(self, message, anchor: str = None):
 | 
			
		||||
        if not anchor:
 | 
			
		||||
            anchor = self._parse_anchor(message)
 | 
			
		||||
        encoded_message = [part.encode('utf-8') for part in (anchor, message)]
 | 
			
		||||
        self.zmq_pub_socket.send_multipart(encoded_message)
 | 
			
		||||
 | 
			
		||||
    def on_close(self):
 | 
			
		||||
        self.zmq_pull_socket.close()
 | 
			
		||||
        self.zmq_pub_socket.close()
 | 
			
		||||
 | 
			
		||||
    # much secure, very cors, wow
 | 
			
		||||
    def check_origin(self, origin):
 | 
			
		||||
        return True
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def _parse_anchor(message):
 | 
			
		||||
        message_json = json.loads(message)
 | 
			
		||||
        return message_json['anchor']
 | 
			
		||||
							
								
								
									
										28
									
								
								src/app/static/ws_listener.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								src/app/static/ws_listener.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,28 @@
 | 
			
		||||
let ws = new WebSocket('ws://' + document.location.host + '/ws');
 | 
			
		||||
 | 
			
		||||
// ws.onopen = function() {
 | 
			
		||||
//    ws.send(JSON.stringify({
 | 
			
		||||
//        'anchor': '',
 | 
			
		||||
//        'data': 'Hello, World!'
 | 
			
		||||
//    }));
 | 
			
		||||
// };
 | 
			
		||||
 | 
			
		||||
// TODO: annotate objects that can fire events
 | 
			
		||||
// TODO: annotate objects that should receive response from events
 | 
			
		||||
// TODO: work out object notation for events that are fired
 | 
			
		||||
// TODO: work out object notation for responses
 | 
			
		||||
 | 
			
		||||
$('#container').on('click', '.anchor', ( function (event) {
 | 
			
		||||
        let anchorName = $(this).attr('id').replace('_event', '');
 | 
			
		||||
        let data = JSON.stringify({
 | 
			
		||||
            'anchor': anchorName,
 | 
			
		||||
            'data': $('#' + anchorName).text()
 | 
			
		||||
        });
 | 
			
		||||
        console.log("Sending: " + data);
 | 
			
		||||
        ws.send(data);
 | 
			
		||||
    }));
 | 
			
		||||
 | 
			
		||||
ws.onmessage = function (messageEvent) {
 | 
			
		||||
    let message = JSON.parse(messageEvent.data);
 | 
			
		||||
    $('#' + message['anchor']).text(message['data']);
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										33
									
								
								src/app/templates/index.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								src/app/templates/index.html
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,33 @@
 | 
			
		||||
<!DOCTYPE html>
 | 
			
		||||
<html lang="en">
 | 
			
		||||
<head>
 | 
			
		||||
    <meta charset="UTF-8">
 | 
			
		||||
    <title>Babylonian Tutorial Proof of Concept</title>
 | 
			
		||||
 | 
			
		||||
    <link rel="stylesheet" href="{{ static_url('vendor/css/bootstrap.min.css') }}">
 | 
			
		||||
</head>
 | 
			
		||||
<body>
 | 
			
		||||
    <div class="jumbotron text-center">
 | 
			
		||||
        <h1>Tutorial framework Proof of Concept</h1>
 | 
			
		||||
    </div>
 | 
			
		||||
    <div id="container">
 | 
			
		||||
        <div class="row">
 | 
			
		||||
            {% for aid in ('a', 'b', 'c') %}
 | 
			
		||||
            <div class="col-sm m-3 p-3">
 | 
			
		||||
                <p id="anchor_{{ escape(aid) }}" >
 | 
			
		||||
                    Anchor {{ escape(aid).upper() }} content should be inserted here.
 | 
			
		||||
                </p>
 | 
			
		||||
                <button type="button" class="btn btn-outline-primary anchor" id="anchor_{{ escape(aid) }}_event">
 | 
			
		||||
                    Fire anchor {{ escape(aid).upper() }}
 | 
			
		||||
                </button>
 | 
			
		||||
            </div>
 | 
			
		||||
            {% end %}
 | 
			
		||||
        </div>
 | 
			
		||||
    </div>
 | 
			
		||||
 | 
			
		||||
    <script src="{{ static_url('vendor/js/jquery-3.2.1.slim.min.js') }}" defer></script>
 | 
			
		||||
    <script src="{{ static_url('vendor/js/popper.min.js') }}" defer></script>
 | 
			
		||||
    <script src="{{ static_url('vendor/js/bootstrap.min.js') }}" defer></script>
 | 
			
		||||
    <script src="{{ static_url('ws_listener.js') }}" defer></script>
 | 
			
		||||
</body>
 | 
			
		||||
</html>
 | 
			
		||||
							
								
								
									
										31
									
								
								src/components/component.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								src/components/component.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,31 @@
 | 
			
		||||
import json
 | 
			
		||||
from functools import partial
 | 
			
		||||
 | 
			
		||||
import zmq
 | 
			
		||||
from zmq.eventloop.zmqstream import ZMQStream
 | 
			
		||||
from zmq.eventloop import ioloop
 | 
			
		||||
 | 
			
		||||
from config import RECEIVER_PORT, PUBLISHER_PORT
 | 
			
		||||
 | 
			
		||||
ioloop.install()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Component:
 | 
			
		||||
    def __init__(self, anchor, event_handler, zmq_context=None):
 | 
			
		||||
        self.anchor = anchor
 | 
			
		||||
        self.event_handler = event_handler
 | 
			
		||||
        self.zmq_context = zmq_context or zmq.Context.instance()
 | 
			
		||||
        self.zmq_sub_socket = self.zmq_context.socket(zmq.SUB)
 | 
			
		||||
        self.zmq_sub_socket.setsockopt_string(zmq.SUBSCRIBE, anchor)
 | 
			
		||||
        self.zmq_sub_socket.connect('tcp://localhost:{}'.format(PUBLISHER_PORT))
 | 
			
		||||
        self.zmq_sub_stream = ZMQStream(self.zmq_sub_socket)
 | 
			
		||||
        self.zmq_push_socket = self.zmq_context.socket(zmq.PUSH)
 | 
			
		||||
        self.zmq_push_socket.connect('tcp://localhost:{}'.format(RECEIVER_PORT))
 | 
			
		||||
 | 
			
		||||
        def wrapper(msg_parts, handler):
 | 
			
		||||
            anchor, message = msg_parts
 | 
			
		||||
            data_json = json.loads(message)
 | 
			
		||||
            response = json.dumps(handler(data_json)).encode('utf-8')
 | 
			
		||||
            self.zmq_push_socket.send_multipart([anchor, response])
 | 
			
		||||
 | 
			
		||||
        self.zmq_sub_stream.on_recv(partial(wrapper, handler=event_handler))
 | 
			
		||||
							
								
								
									
										32
									
								
								src/components/component_example.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								src/components/component_example.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,32 @@
 | 
			
		||||
import codecs
 | 
			
		||||
 | 
			
		||||
from tornado.ioloop import IOLoop
 | 
			
		||||
 | 
			
		||||
from component import Component
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def echo_handler(data):
 | 
			
		||||
    return data
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def rot13_handler(data):
 | 
			
		||||
    data['data'] = codecs.encode(data['data'], 'rot13')
 | 
			
		||||
    return data
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def change_case_handler(data):
 | 
			
		||||
    data['data'] = data['data'].upper() if data['data'].islower() else data['data'].lower()
 | 
			
		||||
    return data
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def reverse_handler(data):
 | 
			
		||||
    data['data'] = data['data'][::-1]
 | 
			
		||||
    return data
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    anchor_a = Component('anchor_a', change_case_handler)
 | 
			
		||||
    anchor_b = Component('anchor_b', rot13_handler)
 | 
			
		||||
    anchor_c = Component('anchor_c', reverse_handler)
 | 
			
		||||
 | 
			
		||||
    IOLoop.instance().start()
 | 
			
		||||
		Reference in New Issue
	
	Block a user