def createRule(parent,options, identity): print ">", parent, options, identity try : intent = Item.get("intent",parent) except Exception: raise errors.ObjectNotFound(parent) bot = Item.get("bot",intent.parent) accountid = parent.split(":")[2] botname = parent.split(":")[3] intentname = parent.split(":")[4] name = options["name"] uri = "uri:rule:%(accountid)s:%(botname)s:%(intentname)s:%(name)s" % vars() print uri data={} data["ID"] = uri data["name"] = options["name"] data["objecttype"] = "rule" data["parent"] = parent if parent is not None else "service" data["search"] = name + options.get("description", "-") + options.get("tags", "-") data["createdat"] = ID.now() data["creator"] = identity data["doc"] = { "replacements" : options["replacements"] } item = Item(**data) item.save() d = item.json() d.update(d["doc"]) del d["doc"] return d
def createVariable(parent, options, identity): print ">", parent, options, identity try: entity = Item.get("entity", parent) except Exception: raise errors.ObjectNotFound(parent) if entity.doc.status != ENTITYSTATES.READY: raise errors.InvalidParameter( "Could not add variable to unparsed entity") bot = Item.get("bot", entity.parent) accountid = parent.split(":")[2] botname = parent.split(":")[3] entityname = parent.split(":")[4] name = options["name"] uri = "uri:variable:%(accountid)s:%(botname)s:%(entityname)s:%(name)s" % vars( ) print uri data = {} data["ID"] = uri data["name"] = options["name"] data["objecttype"] = "variable" data["parent"] = parent if parent is not None else "service" data["search"] = name + options.get("description", "-") + options.get( "tags", "-") data["createdat"] = ID.now() data["creator"] = identity data["doc"] = { "field": options["field"], "type": options.get("type", "dimension"), "aliases": options["aliases"] } item = Item(**data) item.save() syncVariableValues(variableid=uri) d = item.json() d.update(d["doc"]) del d["doc"] return d '''
def create_intent(botid, input, identity): name = input["name"] print "create received", botid, input, identity accountid = botid.split(":")[2] botname = botid.split(":")[3] uri = "uri:intent:%(accountid)s:%(botname)s:%(name)s" % vars() objectid = ID.get() input["ID"] = uri input["objecttype"] = "intent" input["createdat"] = ID.now() input["creator"] = identity input["parent"] = botid input["search"] = input.get("name") + input.get( "description", "-") + input.get("tags", "-") input["doc"] = {} print pprint.pformat(input) item = Item(**input) item.save() return item.json()
def createVariable(parent, options, identity): print ">", parent, options, identity try: entity = Item.get("entity", parent) except Exception: raise errors.ObjectNotFound(parent) if entity.doc.status != ENTITYSTATES.READY: raise errors.InvalidParameter( "Could not add variable to unparsed entity") bot = Item.get("bot", entity.parent) accountid = parent.split(":")[2] botname = parent.split(":")[3] entityname = parent.split(":")[4] name = options["name"] uri = "uri:variable:%(accountid)s:%(botname)s:%(entityname)s:%(name)s" % vars( ) print uri data = {} data["ID"] = uri data["name"] = options["name"] data["objecttype"] = "variable" data["parent"] = parent if parent is not None else "service" data["search"] = name + options.get("description", "-") + options.get( "tags", "-") data["createdat"] = ID.now() data["creator"] = identity data["doc"] = {"aliases": options["aliases"], "field": options["field"]} item = Item(**data) item.save() database = bot.doc.database tablename = entity.doc.tablename columnname = options["field"] sql = 'select %(columnname)s from "%(database)s"."%(tablename)s" group by %(columnname)s limit 30' % vars( ) response = AthenaQuery.run(**{"sql": sql}) values = [ r[columnname].replace('"', '') for r in response["data"]["records"] ] for i, val in enumerate(values): if len(val): slug = unicodedata.normalize('NFKD', val).encode('ascii', 'ignore') cache = Item( **{ "name": slug, "parent": uri, "doc": {}, "createdat": ID.now(), "objecttype": "value", "ID": uri + ":" + str(i), "search": slug }) print cache cache.save() d = item.json() d.update(d["doc"]) del d["doc"] return d