def generate(): config = Config.getInstance() # Собираем список выбранных тем source_names = [] for key, value in request.form.iteritems(): if key.startswith('sources_'): source_names.append(value) strophes = int(request.form['strophes']) rhyming = request.form['rhyming'] step = request.form['step'] steps = request.form['steps'] # Собираем из выбранных тем рифмы в общий пул rhymes_ds = {} for src_name in source_names: source = config['sources'][src_name] for rhyme in source: # Определяем имя рифмы, стопу, и число стоп r_name, r_step, r_steps = rhyme.split('_') # Скипаем, если стопа не та, или не тот размер, если нам не пофиг if step != r_step or steps != '0' and steps != r_steps: continue if not rhymes_ds.has_key(r_name): rhymes_ds[r_name] = set() rhymes_ds[r_name] |= set(source[rhyme]) # Преобразуем пул к списку списков для индексирования rhymes_ll = map(list, rhymes_ds.itervalues()) rhymes_count = len(rhymes_ll) text = [] for dummy in xrange(strophes): # Выбираем пару разных рифм rhyme_a, rhyme_b = map(rhymes_ll.__getitem__, distinctRandom(rhymes_count, 2)) # Выбираем пары строк для строф string_a1, string_a2 = map(rhyme_a.__getitem__, distinctRandom(len(rhyme_a), 2)) string_b1, string_b2 = map(rhyme_b.__getitem__, distinctRandom(len(rhyme_b), 2)) if rhyming == 'abab': text.append([string_a1, string_b1, string_a2, string_b2, ]) elif rhyming == 'abba': text.append([string_a1, string_b1, string_b2, string_a2, ]) elif rhyming == 'aabb': text.append([string_a1, string_a2, string_b1, string_b2, ]) return config['mako']['lookup'].get_template('result.mako').render(text=text)
def reloadConfig(): config = Config.getInstance() config.clear() with open('config.yaml') as fin: config.update(yaml.load(fin)) for name in config.get('include', []): with open(name) as include_file: config[name] = yaml.safe_load(include_file) lookup = TemplateLookup(directories=[config['mako']['template_dir']], module_directory=config['mako']['tmp_dir'], input_encoding="utf-8", output_encoding="utf-8", encoding_errors="replace") config['mako']['lookup'] = lookup return 'Configuration successfully reloaded'
def welcome(): config = Config.getInstance() return config['mako']['lookup'].get_template('index.mako').render(topics=config['sources'].keys())