def _gen(json): for k, v in json.items(): if isinstance(v, dict): for name, message in v.items(): if isinstance( message, dict): # New-style facts.json with attribution yield Fact(name=name, lang=k, message=message['fact'], author=message.get('author')) else: # Newer-style facts.json with language but not attribution -- or explicit deletion of fact. yield Fact(name=name, lang=k, message=message) else: # Old-style facts.json, single language yield Fact(name=k, lang=lang, message=v)
def cmd_fact_edit(bot, trigger): if not option: bot.reply("Missing fact.") return NOLIMIT if '-' not in option: bot.reply( "Fact must include a language specifier. (Perhaps you meant '{name}-{lang}'?)" .format(name=option, lang=bot.memory['ratfacts']['lang'][0])) return NOLIMIT name, lang = option.rsplit('-', 1) if command in ('add', 'set'): message = extra.strip() if extra else None if not message: bot.reply("Can't add a blank fact.") return NOLIMIT fact = db.merge( Fact(name=name, lang=lang, message=extra, author=str(trigger.nick))) is_new = not inspect(fact).persistent db.commit() bot.reply(("Added " if is_new else "Updated ") + format_fact(fact)) return NOLIMIT fact = Fact.find(db, name=name, lang=lang) if fact: db.delete(fact) db.commit() bot.reply("Deleted " + format_fact(fact)) else: bot.reply("No such fact.") return NOLIMIT