From 8bce986b1c57513ef9d128d35d241300feeebf0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Wed, 31 Jan 2018 11:35:07 +0100 Subject: [PATCH] Remove dead code from project --- lib/source_code.py | 26 ------ src/app/buttons.py | 16 ---- src/event_handlers/event_handler_main.py | 83 ------------------- src/event_handlers/stateful_event_handler.py | 43 ---------- src/event_handlers/users.db | Bin 16384 -> 0 bytes 5 files changed, 168 deletions(-) delete mode 100644 lib/source_code.py delete mode 100644 src/app/buttons.py delete mode 100644 src/event_handlers/stateful_event_handler.py delete mode 100644 src/event_handlers/users.db diff --git a/lib/source_code.py b/lib/source_code.py deleted file mode 100644 index 0b8867d..0000000 --- a/lib/source_code.py +++ /dev/null @@ -1,26 +0,0 @@ -import ast -import inspect -import re -from io import StringIO - - -def find_local_variable_value(func, local_variable_name): - func_src = inspect.getsource(func) - func_ast = ast.parse(func_src) - for node in ast.walk(func_ast): - if isinstance(node, ast.Assign): - for target in node.targets: - if isinstance(target, ast.Name): - if target.id == local_variable_name: - return node.value.s - - -def get_source_code(func, strip_comments=False): - source = inspect.getsource(func) - if strip_comments: - # TODO: less fragile way to do this (tokenizer sadly inserts whitespaces all around) - comment_pattern = re.compile('^(\s.*)#.*$') - source = ''.join( - line for line in StringIO(source).readlines() if re.match(comment_pattern, line) is None - ) - return source diff --git a/src/app/buttons.py b/src/app/buttons.py deleted file mode 100644 index 0aa6210..0000000 --- a/src/app/buttons.py +++ /dev/null @@ -1,16 +0,0 @@ -from fsm_base import FSMBase - - -class Buttons(FSMBase): - states = ['A', 'B', 'C'] - transitions = [ - {'trigger': 'anchor_a', 'source': 'A', 'dest': 'B'}, - {'trigger': 'anchor_b', 'source': 'B', 'dest': 'C'}, - {'trigger': 'anchor_c', 'source': 'C', 'dest': 'A'}, - ] - - def __init__(self): - super().__init__('A') - - -fsm = Buttons() diff --git a/src/event_handlers/event_handler_main.py b/src/event_handlers/event_handler_main.py index 421cf21..add1bb1 100644 --- a/src/event_handlers/event_handler_main.py +++ b/src/event_handlers/event_handler_main.py @@ -1,92 +1,9 @@ -import codecs -import sqlite3 -from functools import partial - -import source_code -from event_handler_base import EventHandlerBase from source_code_event_handler import SourceCodeEventHandler from terminado_event_handler import TerminadoEventHandler from tornado.ioloop import IOLoop -from login_component import authorize_login -from util import create_source_code_response_data - -login_component_py_response = partial( - create_source_code_response_data, filename='login_component.py', language='python' -) - - -class EchoHandler(EventHandlerBase): - def handle_event(self, anchor, data_json): - return data_json - - -class Rot13Handler(EventHandlerBase): - def handle_event(self, anchor, data_json): - data_json['data'] = codecs.encode(data_json['data'], 'rot13') - return data_json - - -class ChangeCaseHandler(EventHandlerBase): - def handle_event(self, anchor, data_json): - data_json['data'] = data_json['data'].upper() if data_json['data'].islower() else data_json['data'].lower() - return data_json - - -class ReverseHandler(EventHandlerBase): - def handle_event(self, anchor, data_json): - data_json['data'] = data_json['data'][::-1] - return data_json - - -class LoginHandler(EventHandlerBase): - def handle_event(self, anchor, data_json): - email, password = data_json['data']['email'], data_json['data']['password'] - try: - sql_statement = source_code.find_local_variable_value(authorize_login, 'sql_statement') - yield ( - 'anchor_logger', - 'The SQL statement executed by the server will look like this:\n `{}`'.format(sql_statement) - ) - - yield ('anchor_webide', - login_component_py_response(content=source_code.get_source_code(authorize_login, strip_comments=False))) - - sql_statement_with_values = sql_statement.format(email, password) - yield ( - 'anchor_logger', - 'After the submitted parameters are substituted it looks like this:\n `{}`'.format( - sql_statement_with_values - ) - ) - - logged_in_email, is_admin = authorize_login(email, password) - - yield ( - 'anchor_logger', - 'After the query is executed, it returns _{}_ as email address, and _{}_ for is_admin'.format( - logged_in_email, is_admin - ) - ) - - if logged_in_email is not None: - response = 'Logged in as _{}_. You __{}have__ admin privileges.'.format( - logged_in_email, - '' if is_admin else 'don\'t ' - ) - else: - response = 'Bad username/password!' - except sqlite3.Warning: - response = 'Invalid request!' - - yield ('anchor_login', '# Login page\n' + response) - if __name__ == '__main__': - anchor_a = ChangeCaseHandler('anchor_a') - anchor_b = Rot13Handler('anchor_b') - anchor_c = ReverseHandler('anchor_c') anchor_webide = SourceCodeEventHandler('anchor_webide', 'login_component.py', 'login') anchor_terminado = TerminadoEventHandler('anchor_terminado', 'terminado') IOLoop.instance().start() - diff --git a/src/event_handlers/stateful_event_handler.py b/src/event_handlers/stateful_event_handler.py deleted file mode 100644 index a9d2e34..0000000 --- a/src/event_handlers/stateful_event_handler.py +++ /dev/null @@ -1,43 +0,0 @@ -import json - -from event_handler_base import EventHandlerBase - - -class StatefulEventHandler(EventHandlerBase): - def __init__(self, anchor, event_handler_function): - super().__init__(anchor) - self.event_handler_function = event_handler_function - self.generator = None - self.subscribe('reset') - - def event_handler_callback(msg_parts): - anchor, message = msg_parts - if anchor == b'reset': - self.reset() - return - data_json = json.loads(message) - if self.generator is None: - self.generator = self.event_handler_function(data_json) - try: - response_anchor, response_data = next(self.generator) - except StopIteration: - self.reset() - return - self.subscribe(response_anchor) - response_data = json.dumps({ - 'anchor': response_anchor, - 'data': response_data, - }) - response = [r.encode('utf-8') for r in (response_anchor, response_data)] - self.server_connector.send(*response) - - self.server_connector.register_callback(event_handler_callback) - - def unsubscribe_all(self): - super().unsubscribe_all() - self.subscribe('reset') - - def reset(self): - self.generator = None - self.unsubscribe_all() - self.subscribe(self.anchor) diff --git a/src/event_handlers/users.db b/src/event_handlers/users.db deleted file mode 100644 index cde24ab8ce11ba9fa09b780a92d9c80f7f8c04b2..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16384 zcmeI(zfRjg90%|_oBtBM4i#>NHLa^nm9H%m(WbsK@Q1M97CK4*7iyI z3f*`FmOeorp>w5Tn930^lc$7IQstZw zV(7A<%QUF&%#Wwne%Bgg_xb%g`%26w9|$wqN8KPG009U<00Izz00bZa0SG_<0)G}b zF_*0EZQ32l^S;Wj`}*xQ&eio;4KC(~3r^(P!sWs~@?AccyETxh+R-yhZ|ii~vMl;8 zE$%ta)i5tAb%*QE79?M>u45m+?J)%@HmaIdIl4Q_R zw-4);<55|@=HBD|g30%P%DxbNARqt%2tWV=5P$##AOHafKmY;|_@4w;scF&g Hzc_pYU}u|_