def register_parser_modules(): """List all parser modules and register them in the database.""" module_dir = os.path.join(os.path.dirname(__file__), 'parser_modules') modules = list_modules(module_dir) with DatabaseSession(disconnect=False): for name in modules: logger.info('Registering parser module `%s\' ...', name) m = importlib.import_module('%s.%s' % (ParserModule.PARSER_MODULES, name)) info = m.get_module_info() assert info['module_name'] == name try: Draft4Validator.check_schema(m.schema()) except Exception: logger.error('module %s schema check failed', name) raise pm = ParserModule.get_by(id=info['id'], single=True) if pm: ParserModule.get_by(id=info['id'], return_query=True).update(info) else: ParserModule(**info).add()
def validate_bot_schema(bot_json, source='bot_json'): try: schema = util.get_schema('bot') jsonschema.validate(bot_json, schema) except jsonschema.exceptions.ValidationError: logger.error('Validation failed for `%s\'!', source) raise
def register_content_modules(): """List all content modules and register them in the database.""" module_dir = os.path.join(os.path.dirname(__file__), 'content_modules') modules = list_modules(module_dir) with DatabaseSession(disconnect=False): for name in modules: logger.info('Registering content module `%s\' ...', name) m = importlib.import_module('%s.%s' % (ContentModule.CONTENT_MODULES, name)) if not hasattr(m, 'get_module_info'): logger.warn('Skip module %s due to lack of get_module_info()' % name) continue info = m.get_module_info() assert info['module_name'] == name try: Draft4Validator.check_schema(m.schema()) except Exception: logger.error('module %s schema check failed', name) raise cm = ContentModule.get_by(id=info['id'], single=True) if cm: ContentModule.get_by(id=info['id'], return_query=True).update(info) else: ContentModule(**info).add()
def send_ga_track_info(): """Send tracking info to GA.""" tracking = get_tracking() if not enable_tracking() or not tracking: return base = u'v=1&tid=%s' % g.ga_id params = u'' for ti in tracking: if ti.ttype == TrackingInfo.Type.Event: params += base + u'&'.join([ u'', u'cid=%s' % ti.cid, u't=%s' % ti.ttype.value, u'ec=%s' % ti.catagory, u'ea=%s' % ti.action, u'el=%s' % ti.label, u'ev=%s' % ti.value ]) + '\n' elif ti.ttype == TrackingInfo.Type.Pageview: params += base + u'&'.join([ u'', u'cid=%s' % ti.cid, u't=%s' % ti.ttype.value, u'dp=%s' % ti.page ]) + '\n' response = requests.request('POST', 'http://www.google-analytics.com/batch', data=params.encode('utf8')) if response.status_code != 200: logger.error('send_ga_track_info: HTTP %d: %s' % (response.status_code, response.text))
def validate_broadcast_schema(broadcast_json): """Validate broadcast request schema.""" try: jsonschema.validate(broadcast_json, Broadcast.schema()) for message in broadcast_json['messages']: jsonschema.validate(message, Message.schema()) except jsonschema.exceptions.ValidationError: logger.error('Validation failed for `broadcast_json\'!') raise
def get_schema(name): """Get schema definition with *name* from bb8/schema directory.""" filename = os.path.join(bb8.SRC_ROOT, 'schema', '%s.schema.json' % name) with open(filename, 'r') as f: schema_def = json.load(f) try: jsonschema.Draft4Validator.check_schema(schema_def) except Exception: logger.error('invalid schema found `%s\'', name) raise return schema_def
def parse_platform(platform_json, to_platform_id=None, source='platform_json'): """Parse Platform from platform definition. If *to_platform_id* is specified, update existing platform specified by *to_platform_id* instead of creating a new platform. If *to_platform_id* is a callable. The result of the call is used as the platform_id. """ validate_platform_schema(platform_json) # Validate plaform-specific schema provider = get_messaging_provider( PlatformTypeEnum(platform_json['type_enum'])) try: jsonschema.validate(platform_json['config'], provider.get_config_schema()) except jsonschema.exceptions.ValidationError: logger.error('Platform config validate failed for `%s\'!', source) raise if callable(to_platform_id): to_platform_id = to_platform_id(platform_json) bot_id = platform_json.get('bot_id') if bot_id: bot = Bot.get_by(id=bot_id, account_id=platform_json['account_id'], single=True) if not bot: raise RuntimeError('bot does not exist') if to_platform_id: # Update existing platform. logger.info(u'Updating existing Platform(id=%d, name=%s) from %s ...', to_platform_id, platform_json['name'], source) platform = Platform.get_by(id=to_platform_id, single=True) platform.bot_id = bot_id platform.name = platform_json['name'] platform.deployed = platform_json['deployed'] platform.type_enum = platform_json['type_enum'] platform.provider_ident = platform_json['provider_ident'] platform.config = platform_json['config'] else: # Create a new platform. logger.info(u'Creating new Platform(name=%s) from %s ...', platform_json['name'], source) platform = Platform(**platform_json).add() DatabaseManager.flush() return platform
def validate_platform_schema(platform_json, source='platform_json'): try: jsonschema.validate(platform_json, Platform.schema()) except jsonschema.exceptions.ValidationError: logger.error('Validation failed for `%s\'!', source) raise
def parse_bot(bot_json, to_bot_id=None, source='bot_json'): """Parse Bot from bot definition. If *to_bot_id* is specified, update existing bot specified by *to_bot_id* instead of creating a new bot. If *to_bot_id* is a callable. The result of the call is used as the bot_id. """ validate_bot_schema(bot_json, source=source) bot_desc = bot_json['bot'] if callable(to_bot_id): to_bot_id = to_bot_id(bot_desc) if to_bot_id: # Update existing bot. logger.info(u'Updating existing Bot(id=%d, name=%s) from %s ...', to_bot_id, bot_desc['name'], source) bot = Bot.get_by(id=to_bot_id, single=True) bot.delete_all_nodes() bot.name = bot_desc['name'] bot.description = bot_desc['description'] bot.interaction_timeout = bot_desc['interaction_timeout'] bot.admin_interaction_timeout = bot_desc['admin_interaction_timeout'] bot.session_timeout = bot_desc['session_timeout'] bot.ga_id = bot_desc.get('ga_id', None) bot.settings = bot_desc['settings'] DatabaseManager.flush() else: # Create a new bot logger.info(u'Creating new Bot(name=%s) from %s ...', bot_desc['name'], source) bot = Bot( name=bot_desc['name'], description=bot_desc['description'], interaction_timeout=bot_desc['interaction_timeout'], admin_interaction_timeout=bot_desc['admin_interaction_timeout'], session_timeout=bot_desc['session_timeout'], ga_id=bot_desc.get('ga_id', None), settings=bot_desc['settings']).add() DatabaseManager.flush() # Bind Platform with Bot platforms = bot_json.get('platforms', {}) for unused_name, provider_ident in platforms.iteritems(): platform = Platform.get_by(provider_ident=provider_ident, single=True) if platform is None: raise RuntimeError('associated platform `%s\' does not exist', provider_ident) # Bind platform.bot_id = bot.id provider = get_messaging_provider(platform.type_enum) if not platform.deployed or (config.DEPLOY and platform.deployed): provider.apply_settings(platform.config, bot.settings) DatabaseManager.flush() nodes = bot_desc['nodes'] id_map = {} # Mapping of stable_id to id # Build node for stable_id, node in nodes.iteritems(): try: cm = ContentModule.get_by(id=node['content_module']['id'], single=True) if cm is None: raise RuntimeError('Content_module `%d\' does not exist', node['content_module']['id']) jsonschema.validate(node['content_module']['config'], cm.get_module().schema()) except jsonschema.exceptions.ValidationError: logger.error( 'Node `%s\' content module config validation ' 'failed', stable_id) raise n = Node(stable_id=stable_id, bot_id=bot.id, name=unicode(node['name']), description=unicode(node['description']), expect_input=node['expect_input'], content_module_id=node['content_module']['id'], content_config=node['content_module']['config']).add() if 'parser_module' in node: n.parser_module_id = node['parser_module']['id'] DatabaseManager.flush() id_map[stable_id] = n.id # Validate that parser module linkages are present in this bot file. for stable_id, node in nodes.iteritems(): n = Node.get_by(id=id_map[stable_id], single=True) if n.parser_module is not None: nodes = bot_json['bot']['nodes'] n.parser_config = node['parser_module']['config'] pm = n.parser_module.get_module() try: jsonschema.validate(n.parser_config, pm.schema()) except jsonschema.exceptions.ValidationError: logger.error( 'Node `%s\' parser module config validation ' 'failed', stable_id) raise for end_node_id in pm.get_linkages(n.parser_config): if end_node_id is not None: if re.search(HAS_VARIABLE_RE, end_node_id): logger.info('Rendered end_node_id `%s\', check ' 'skipped ...' % end_node_id) continue if end_node_id not in id_map.keys(): raise RuntimeError('end_node_id `%s\' is invalid' % end_node_id) DatabaseManager.flush() return bot
def on_app_error(e): logger.error(str(e)) return jsonify(message=e.message, error_code=e.error_code), e.status_code