async def processor(exchange): data = evaluate_expression(data_expression, exchange) if 'util' in params: data.update({'util': params.get('util')()}) data['env'] = json.dumps(evaluate_expression(jinja2_env, exchange)) exchange.set_body(template.render(data)) exchange.set_header('content-type', 'text/html') return exchange
async def put_queue_processor(exchange): if expression is None: value = exchange.get_body() else: value = evaluate_expression(expression, exchange) import collections def put_queue(v): if unique: if not exchange.parent().get_header('channel_dict').get( channel_name).get(v, False): exchange.parent().get_header('channel_dict').get( channel_name)[v] = True else: return False exchange.parent().get_header('queues').get( queue_name).put_nowait((channel_name, v)) return True if isinstance(value, collections.Iterable): for v in value: put_queue(v) else: put_queue(value) return exchange
async def processor(exchange): if mode == 'read': file_name = evaluate_expression(file_name_expression, exchange) f = open(file_name, 'r') exchange.set_body(f.read()) f.close() return exchange
async def update_exchange_processor(exchange): for field_name in params: value = evaluate_expression(params[field_name], exchange) if field_name == 'body': exchange.set_body(value) else: exchange.set_header(field_name, value) return exchange
async def processor(exchange): nonlocal conn if conn is None or conn.closed: conn = await aioredis.create_connection(**conn_setting) print('redis connected') cache_key = str(evaluate_expression(keys_expression, exchange)) exchange.set_body(json.loads(await conn.execute('get', cache_key))) return exchange
async def processor(exchange): cache_key = hashkey( tuple(evaluate_expression(keys_expression, exchange))) if cache_key in cache_object: exchange.set_body(cache_object.get(cache_key)) else: to = params.get('to') if to: exchange = await to.get_consumer().produce(exchange) cache_object[cache_key] = exchange.get_body() return exchange
async def processor(exchange): url = evaluate_expression(url_expression, exchange) import aiohttp async with aiohttp.ClientSession() as session: async with session.get(url) as resp: if isValid: exchange.set_header('validate', resp.status == 200) else: if response_type == 'text': exchange.set_body(await resp.text()) elif response_type == 'data': exchange.set_body(await resp.read()) exchange.set_header('request_url', url) #buggy return exchange
async def split_processor(exchange): import copy to_split = evaluate_expression(from_, exchange) import collections if isinstance(to_split, str): to_split = to_split.split() assert isinstance( to_split, collections.Iterable), 'split target is not iterable.' gatherd = await asyncio.gather(*[ consumer.produce(exchange.create_child(Exchange(sp))) for sp in to_split ]) if aggregate: exchange.set_body(aggregate(gatherd)) return exchange
async def with_queue_processor(exchange): task_queue = asyncio.Queue(loop=loop) init_queues = evaluate_expression(params.get('init_queue', {}), exchange) async def init_queue(exchange): for channel_name in init_queues: init_queue = init_queues.get(channel_name, []) for q in init_queue: await task_queue.put((channel_name, q)) await init_queue(exchange) if task_queue.empty(): return exchange prepare_queue = asyncio.Queue(loop=loop, maxsize=params.get('maxsize', 0)) queues = exchange.get_header('queues', {}) queues[params.get('queue_name', 'default_queue')] = task_queue exchange.set_header('queues', queues) channel_dict = exchange.get_header('channel_dict', {}) for k in channels: channel_dict[k] = {} exchange.set_header('channel_dict', channel_dict) async def create_task(task): channel = task[0] body = task[1] child = exchange.create_child() child.set_body(body) await channels.get(channel).get_consumer().produce(child) prepare_queue.get_nowait() if task_queue.empty() and prepare_queue.empty(): await task_queue.put(None) while True: task = await task_queue.get() if task is None: break await prepare_queue.put('') loop.create_task(create_task(task)) return exchange
async def processor(exchange): #yapf: disable to_dumps = exchange.get_body() if expression is None else evaluate_expression(expression, exchange) import json exchange.set_body(json.dumps(to_dumps)) return exchange
async def processor(exchange): cache_key = hashkey( tuple(evaluate_expression(keys_expression, exchange))) cache_object[cache_key] = exchange.get_body() return exchange
def repl(): #: the built-in scheme forms and special repl commands KEYWORDS = ('lambda', 'macro', 'if', 'quote', 'eval', 'define', 'delay', '.reset', '.exit', '.quit', '.help') # the scheme auto-completer def completer(text, state): # look through SCHEME_KEYWORDS for w in KEYWORDS: if w.startswith(text): if state <= 0: return w state -= 1 # look through the environment names for w in environment.iterkeys(): if w.startswith(text): if state <= 0: return w state -= 1 histfile = os.path.join(os.path.expanduser("~"), ".pyscheme-hist") try: readline.read_history_file(histfile) except IOError: pass atexit.register(readline.write_history_file, histfile) readline.parse_and_bind("tab: complete") readline.parse_and_bind("set blink-matching-paren on") readline.set_completer(completer) environment = make_global_environment() while True: try: text = raw_input(">> ") # test for special commands if text == '.reset': print "reseting environment..." environment = make_global_environment() continue elif text == '.help': print "Just type scheme expression and have fun." continue elif text in ('.exit', '.quit'): break result = evaluate_expression(InterpreterInput(text), environment) print "=>", pretty_print(result) # set % as the last evaluated expression in environment environment['%'] = quote(result) except EOFError: break except KeyboardInterrupt: print "\ninterrupt." except Exception as e: print "error:", e.message print "\nexiting..."
async def processor(exchange): value = evaluate_expression(expression, exchange) exchange.set_body(markdown_module.markdown(value)) return exchange
async def processor(exchange): file_name = evaluate_expression(expression, exchange) (exchange.get_header('zipfile') or exchange.parent().get_header('zipfile')).writestr( file_name, exchange.get_body()) return exchange
async def processor(exchange): zip_file_name = evaluate_expression(expression, exchange) exchange.set_header('zip_file_name', zip_file_name) exchange.set_header( 'zipfile', ZipFile(zip_file_name, 'w', zipfile.ZIP_DEFLATED)) return exchange
async def assert_processor(exchange): value = evaluate_expression(expression, exchange) assert_func(value, *args) print(check_name, 'passed') return exchange
async def processor(exchange): exchange.set_body(evaluate_expression(expression, exchange)) return exchange
async def processor(exchange): exchange.set_header(key, evaluate_expression(expression, exchange)) return exchange
def view_page(page_id): # If there is no session cookie set, redirect to the index login page if "username" not in session: return redirect(url_for('index')) # Check if there is a user in the database with the username from the session cookie user = User.query.filter(User.name == session['username']).all() # TODO how to handle this if len(user) != 1: # we had a cookie but it didn't match a username return redirect(url_for('reset')) user = user[0] page = Page.query.get(page_id) if not page: abort(404) db.session.add(Log(content="Pageload %d (true page ID: %d)" % (page_id, fix_id(page_id)), timestamp=datetime.datetime.now(), user=user)) db.session.commit() # While we do store the user variables dictionary in the session, # the user cannot modify them due to the cookie signing included in Flask. # Still vulnerable to playback attacks, but come on... user_vars = session['variables'] if request.method == 'POST': # We have some post data, which can only come from a link's form, which contains zero or more prompt items # OR, it could be a notebook submission if "notebook_append" in request.form: add_manual_notebook_entry(user, request.form["notebook_append"]) else: # Look up the Link object the user just used to get to this destination page #print "request.form:", request.form link_id = request.form["link_id"] link = Link.query.get(link_id) # Double-check that the user followed a valid link to get to this page #print "link: '%s', page.links_incoming: '%s'" % (str(link), str(page.links_incoming)) assert link in page.links_incoming # TODO how do we handle this better? # Update the user's current_page_id variable session['current_page_id'] = page_id # The link could have multiple feedback variables the user may have filled out, load them up here: feedback_vars = dict( [(key.replace("feedback_", ""), clean_text(request.form[key])) for key in request.form.keys() if key.startswith("feedback_")] ) #print "feedback_vars:", feedback_vars # TODO how do we protect ourselves from funky user data in our string formatting below? # Does the clean_text() function do sufficiently well? # Now, we need to perform the link's actions - More details of each type in models.py for action in link.actions: #print "Performing action", action if action.type == Action.ACTION_LOG: content = action.action_string.format(**feedback_vars) # TODO whoah db.session.add(Log(content=content[:(defines["MAX_LOG_NOTE_LENGTH"])], timestamp=datetime.datetime.now(), user=user)) elif action.type == Action.ACTION_NOTE: content = action.action_string.format(**feedback_vars) # TODO whoah db.session.add(Note(content=content[:(defines["MAX_LOG_NOTE_LENGTH"])], timestamp=datetime.datetime.now(), user=user, icon=action.icon)) elif action.type == Action.ACTION_VAR: # Our action_string will be something like "foo = bar + baz * 2", so we split on the "=" #print action.action_string lhs, rhs = map(strip, action.action_string.split("=")) #print "lhs: '%s', rhs: '%s'" % (lhs, rhs) # Now, we need to evaluate the rhs to find the value to assign to lhs eval_vars = dict(user_vars.items() + feedback_vars.items()) rvalue = evaluate_expression(rhs, eval_vars) #print "rvalue:", rvalue user_vars[lhs] = rvalue elif action.type == Action.ACTION_LIB: print "Library action types are not yet supported, sorry..." else: print "Found an action with unrecognized type %d, as part of link %d" % link_id #abort(500) # Commit any database objects we created or updated db.session.commit() # To avoid breaking the refresh button, we redirect to the same page but without the POST data return redirect(url_for('view_page', page_id=page_id)) # else # if request.method == "POST" # evaluate each page section "show" and "content" expressions sections = [] for section in page.sections: show = evaluate_expression(section.show, user_vars) content = section.content.format(**user_vars) #print section, section.show, show sections.append({"order": section.order, "show": show, "content": content}) # Evaluate each outgoing link "show" expression links = [] for link in page.links_outgoing: show = evaluate_expression(link.show, user_vars) #print link, link.show, show links.append({"order": link.order, "show": show, "link": link}) sim = Simulation.query.get(session['sim_id']) return render_template("page.html", sim=sim, page=page, sections=sections, links=links, user=user, user_vars=user_vars)
def view_page(page_id): # If there is no session cookie set, redirect to the index login page if "username" not in session: return redirect(url_for('index')) # Check if there is a user in the database with the username from the session cookie user = User.query.filter( User.name == session['username']).all() # TODO how to handle this if len(user) != 1: # we had a cookie but it didn't match a username return redirect(url_for('reset')) user = user[0] page = Page.query.get(page_id) if not page: abort(404) db.session.add( Log(content="Pageload %d (true page ID: %d)" % (page_id, fix_id(page_id)), timestamp=datetime.datetime.now(), user=user)) db.session.commit() # While we do store the user variables dictionary in the session, # the user cannot modify them due to the cookie signing included in Flask. # Still vulnerable to playback attacks, but come on... user_vars = session['variables'] if request.method == 'POST': # We have some post data, which can only come from a link's form, which contains zero or more prompt items # OR, it could be a notebook submission if "notebook_append" in request.form: add_manual_notebook_entry(user, request.form["notebook_append"]) else: # Look up the Link object the user just used to get to this destination page #print "request.form:", request.form link_id = request.form["link_id"] link = Link.query.get(link_id) # Double-check that the user followed a valid link to get to this page #print "link: '%s', page.links_incoming: '%s'" % (str(link), str(page.links_incoming)) assert link in page.links_incoming # TODO how do we handle this better? # Update the user's current_page_id variable session['current_page_id'] = page_id # The link could have multiple feedback variables the user may have filled out, load them up here: feedback_vars = dict([(key.replace("feedback_", ""), clean_text(request.form[key])) for key in request.form.keys() if key.startswith("feedback_")]) #print "feedback_vars:", feedback_vars # TODO how do we protect ourselves from funky user data in our string formatting below? # Does the clean_text() function do sufficiently well? # Now, we need to perform the link's actions - More details of each type in models.py for action in link.actions: #print "Performing action", action if action.type == Action.ACTION_LOG: content = action.action_string.format( **feedback_vars) # TODO whoah db.session.add( Log(content=content[:(defines["MAX_LOG_NOTE_LENGTH"])], timestamp=datetime.datetime.now(), user=user)) elif action.type == Action.ACTION_NOTE: content = action.action_string.format( **feedback_vars) # TODO whoah db.session.add( Note( content=content[:(defines["MAX_LOG_NOTE_LENGTH"])], timestamp=datetime.datetime.now(), user=user, icon=action.icon)) elif action.type == Action.ACTION_VAR: # Our action_string will be something like "foo = bar + baz * 2", so we split on the "=" #print action.action_string lhs, rhs = map(strip, action.action_string.split("=")) #print "lhs: '%s', rhs: '%s'" % (lhs, rhs) # Now, we need to evaluate the rhs to find the value to assign to lhs eval_vars = dict(user_vars.items() + feedback_vars.items()) rvalue = evaluate_expression(rhs, eval_vars) #print "rvalue:", rvalue user_vars[lhs] = rvalue elif action.type == Action.ACTION_LIB: print "Library action types are not yet supported, sorry..." else: print "Found an action with unrecognized type %d, as part of link %d" % link_id #abort(500) # Commit any database objects we created or updated db.session.commit() # To avoid breaking the refresh button, we redirect to the same page but without the POST data return redirect(url_for('view_page', page_id=page_id)) # else # if request.method == "POST" # evaluate each page section "show" and "content" expressions sections = [] for section in page.sections: show = evaluate_expression(section.show, user_vars) content = section.content.format(**user_vars) #print section, section.show, show sections.append({ "order": section.order, "show": show, "content": content }) # Evaluate each outgoing link "show" expression links = [] for link in page.links_outgoing: show = evaluate_expression(link.show, user_vars) #print link, link.show, show links.append({"order": link.order, "show": show, "link": link}) sim = Simulation.query.get(session['sim_id']) return render_template("page.html", sim=sim, page=page, sections=sections, links=links, user=user, user_vars=user_vars)