def base(path, params, content, client_ip): test = get_newest_fact('kropotkin', 'constitution_element', {'type': 'home_component'}) if not test: return 200, 'Kropotkin HTTP\n', 'text/plain' home_fact = get_newest_fact('kropotkin', 'home_component', {}) substitute_path = '/component/%s' % home_fact['name'] return get_component(substitute_path, params, content, client_ip)
def wait_for_fact(factspace, type_, criteria, timeout): finish = now() + timeout while now() < finish: result = kropotkin.get_newest_fact(factspace, type_, criteria) if result is not None: return True return False
def _get_statements_db(statements_dir, factspace, confidence, fact_type, params, result, number, timeout): if fact_type == 'constitution_element': type_keys = ['keys', 'options', 'translation', 'type'] else: constitution_element = get_newest_fact(factspace, 'constitution_element', {'type': fact_type}) if not constitution_element: raise Exception('queried type that does not exist: %s' % fact_type) type_keys = loads(constitution_element['keys']) type_keys.extend(KROPOTKIN_KEYS) type_keys.sort() connection = connect(join(statements_dir, 'factspace.db')) cursor = connection.cursor() cursor.execute(CHECK_TABLE_SQL, (fact_type,)) if 0 == len(cursor.fetchall()): connection.close() return [] connection.commit() if confidence != 'statement': params['kropotkin_confidence'] = confidence values = [] param_keys = sorted(params.keys()) if param_keys: match_clause = 'WHERE ' + \ ' AND '.join(['%s = ?' % key for key in param_keys]) else: match_clause = '' values.extend([params[key] for key in param_keys]) order = 'DESC' if result == 'newest' else 'ASC' limit = 'LIMIT %d' % number if number else '' select_sql = SELECT_TEMPLATE % (','.join(type_keys), fact_type, match_clause, order, limit) finish = __now_millis() + timeout while True: try: cursor.execute(select_sql, values) statements = [dict(zip(type_keys, r)) for r in cursor.fetchall()] connection.commit() except OperationalError as error: stderr.write('Sqlite error %s\n' % error) stderr.write('Select SQL: %s' % select_sql) statements = [] break if len(statements) > 0 or __now_millis() > finish: break sleep(0.1) connection.close() return statements
def get_component(path, params, content, client_ip): name = path.split('/')[2] component = get_newest_fact('kropotkin', 'component', {'name': name}) if not component: return 404, 'No such component\n', 'text/plain' content = b64decode(component['bytes']) try: content_type = component['content_type'] mime_type = MIME_TYPES[content_type] except KeyError: return 404, 'No mime type for %s\n' % content_type, 'text/plain' return 200, content, mime_type
def get_statements(path, params, content, client_ip): factspace, confidence, fact_type = path.split('/')[2:5] if (factspace == 'kropotkin'): statements_dir = environ['KROPOTKIN_DIR'] else: factspace_info = get_newest_fact('kropotkin', 'factspace', {'name': factspace}) if not factspace_info: return (404, 'Could not locate factspace %s' % factspace, 'text/plain') statements_dir = factspace_info['directory'] statements = _fetch_statements(statements_dir, factspace, confidence, fact_type, params) return 200, dumps(statements), 'application/json'
def check_statement(factspace, fact_type, content_dict): actual_keys = sorted(content_dict.keys()) if fact_type == 'constitution_element': expected_keys = ['keys', 'translation', 'type'] translation = "Constitution amended: type = %(type)s, " \ + "keys = %(keys)s, translation = %(translation)s" else: constitution_element = get_newest_fact(factspace, 'constitution_element', {'type': fact_type}) if not constitution_element: return False expected_keys = sorted(loads(constitution_element['keys'])) return expected_keys == actual_keys
def store_statement(path, params, content, client_ip): factspace, confidence, fact_type = path.split('/')[2:5] content_dict = loads(content) if not check_statement(factspace, fact_type, content_dict): stderr.write("Fact of type %s disallowed\n" % fact_type) return (400, 'Fact of type %s blocked by constitution\n' % fact_type, 'text/plain') if (factspace == 'kropotkin'): statements_dir = environ['KROPOTKIN_DIR'] else: statements_dir = get_newest_fact('kropotkin', 'factspace', {'name': factspace})['directory'] rowid = save_statement(statements_dir, confidence, fact_type, content) return (200, str(rowid), 'text/plain')
def should_save(factspace, fact_type, content_dict): actual_keys = sorted(content_dict.keys()) if fact_type == 'constitution_element': expected_keys = ['keys', 'options', 'translation', 'type'] translation = "Constitution amended: type = %(type)s, " \ + "keys = %(keys)s, options = %(options)s, " \ + "translation = %(translation)s" options = '' else: constitution_element = get_newest_fact(factspace, 'constitution_element', {'type': fact_type}) if not constitution_element: raise Exception('Statement type %s not recognised' % fact_type) expected_keys = sorted(loads(constitution_element['keys'])) options = constitution_element['options'] if expected_keys != actual_keys: raise Exception('Wrong keys for statement type %s' % fact_type) return 'memory_only' not in options
def store_statement(path, params, content, client_ip): factspace, confidence, fact_type = path.split('/')[2:5] content_dict = loads(content) try: if should_save(factspace, fact_type, content_dict) is False: return (200, '0', 'text/plain') except Exception as e: stderr.write("Fact of type %s disallowed:\n%s: %s\n" \ % (fact_type, type(e), str(e))) return (400, 'Fact of type %s blocked by constitution\n' % fact_type, 'text/plain') if (factspace == 'kropotkin'): statements_dir = environ['KROPOTKIN_DIR'] else: statements_dir = get_newest_fact('kropotkin', 'factspace', {'name': factspace})['directory'] rowid = save_statement(statements_dir, confidence, fact_type, content) return (200, str(rowid), 'text/plain')
from time import time def choose(random_value, percentages, default=None): n = 0 for name, percentage in percentages: if random_value in range(n, n + percentage): return name else: n = n + percentage return default def _now(): return int(round(time())) subscribe('whooshingby', 'fact', 'completed_task') while True: fact = get_next_fact('whooshingby', 'completed_task') if not fact: continue percentages_fact = get_newest_fact('whooshingby', 'reward_percentages', {}) reward_percentages = loads(percentages_fact['percentages']) random_value = (hash(fact['name']) * 61 + fact['time'] * 47) % 100 name = choose(random_value, reward_percentages) if name: content = {'name': name, 'task_id': fact['task_id'], 'source': 'python', 'time': _now()} if not store_opinion('whooshingby', 'reward', content): print "Could not store reward opinion" # handle better
'options': 'memory_only'}, {'type': 'user', 'keys': dumps(['name', 'salt', 'hash']), 'translation': 'User %(name)s is registered', 'options': ''}, {'type': 'user_nonce', 'keys': dumps(['name', 'nonce', 'expiry']), 'translation': 'User %(name)s can be identified by %(nonce)s' \ + 'until %(expiry)s', 'options': ''}] for e in elements: if not store_fact('whooshingby', 'constitution_element', e): fail_and_exit("Could not store constitution element fact") reward_percentages = get_newest_fact('whooshingby', 'reward_percentages', {}) if not reward_percentages: with open('rewards.json') as f: reward_percentages = f.read() if not store_fact('whooshingby', 'reward_percentages', {'percentages': reward_percentages}): fail_and_exit("Could not store reward percentages") judge_percentages = get_newest_fact('whooshingby', 'judge_percentages', {}) if not judge_percentages: if not store_fact('whooshingby', 'judge_percentages', {'percentages': '[["python", 100], ["ruby", 0]]'}): fail_and_exit("Could not store judge percentages") for f in listdir('components'): content = {'location': abspath(join('components', f))}
def compare_opinions(opinions, expected_opinions): if len(opinions) < expected_opinions: return False names = set([opinion['name'] for opinion in opinions]) return len(names) == 1 def classify_opinion(opinion): return opinion['task_id'] subscribe_sets('whooshingby', 'opinion', 'reward', classify_opinion, 2, 1) while True: opinions = get_next_set('whooshingby', 'opinion', 'reward') if not compare_opinions(opinions, 2): if not store_fact('whooshingby', 'opinion_difference', {'opinions': dumps(opinions)}): print "Could not store opinion difference fact" percentages_fact = get_newest_fact('whooshingby', 'judge_percentages', {}) percentages = loads(percentages_fact['percentages']) random_value = randrange(100) source = choose(random_value, percentages, default='python') to_promote = next((o for o in opinions if o['source'] == source), None) if to_promote: for key in to_promote.keys(): if key.startswith('kropotkin_'): del to_promote[key] if not store_fact('whooshingby', 'reward', to_promote): print "Could not store reward fact"