def validate(sc, fp): """ Validate a sous chef schema: First chef against the canoncial json schema. Then check if the `runs` field is a valid python module or an executable script that exists where it has been declared. Then check special metrics options and merge the default sous-chef options with the provied ones. """ try: # check the json schema _validate_sous_chef_json_schema(sc) # check for checkbox option sous chefs _validate_input_and_value_types(sc) # check if `runs` is a python module that inherits from # newslynx.sc.SousChef if not '/' in sc['runs']: sc['is_command'] = False _validate_python_sous_chef(sc) # otherwise validate the command else: sc['is_command'] = True _validate_command_sous_chef(sc) # special cases for sous chefs that create metrics if 'metric' in sc.get('creates', 'null'): _validate_metrics_sous_chef(sc) # if 'report' in sc['creates']: # sc = _validate_report_sous_chef(sc, fp) # hack if not sc.get('creates', None): sc['creates'] = 'null' # if everything is kosher, merge the sous-chef options # with the defaults sc['options'] = update_nested_dict(sc['options'], SOUS_CHEF_DEFAULT_OPTIONS) return sc except: msg = "{}\n{}".format(format_exc(), "failed on file {}".format(fp)) raise SousChefSchemaError(msg)
def validate(sc, fp): """ Validate a sous chef schema: First chef against the canoncial json schema. Then check if the `runs` field is a valid python module or an executable script that exists where it has been declared. Then check special metrics options and merge the default sous-chef options with the provied ones. """ try: # check the json schema _validate_sous_chef_json_schema(sc) # check for checkbox option sous chefs _validate_input_and_value_types(sc) # check if `runs` is a python module that inherits from # newslynx.sc.SousChef if not '/' in sc['runs']: sc['is_command'] = False _validate_python_sous_chef(sc) # otherwise validate the command else: sc['is_command'] = True _validate_command_sous_chef(sc) # special cases for sous chefs that create metrics if 'metric' in sc.get('creates', 'null'): _validate_metrics_sous_chef(sc) # if 'report' in sc['creates']: # sc = _validate_report_sous_chef(sc, fp) # hack if not sc.get('creates', None): sc['creates'] = 'null' # if everything is kosher, merge the sous-chef options # with the defaults sc['options'] = update_nested_dict( sc['options'], SOUS_CHEF_DEFAULT_OPTIONS) return sc except: msg = "{}\n{}".format(format_exc(), "failed on file {}".format(fp)) raise SousChefSchemaError(msg)
def load(fp): """ Load a sous chef allowing for include statements. """ try: sc = yaml_stream_to_obj(open(fp)) # update with includes. for incl_fp in sc.get('includes', []): if not incl_fp.endswith('.yaml'): incl_fp += ".yaml" incl_fp = os.path.join(os.path.dirname(fp), incl_fp) incl = yaml_stream_to_obj(open(incl_fp)) sc = update_nested_dict(sc, incl, overwrite=True) # validate return validate(sc, fp) except: msg = "{}\n{}".format(format_exc(), "failed on file {}".format(fp)) raise SousChefSchemaError(msg)
def update(old_sous_chef, new_sous_chef): """ Given a partial or completely new sous-chef, update the souf-chef and re-validate it. """ # if the old sous chef is a SousChef object, coerce it to and from json. if isinstance(old_sous_chef, SousChef): old_sous_chef = json_to_obj(obj_to_json(old_sous_chef)) # pop the id old_sous_chef.pop('id', None) # update the previous version. new_sous_chef = update_nested_dict( old_sous_chef, new_sous_chef, overwrite=True) return validate(new_sous_chef, None)
def update(old_sous_chef, new_sous_chef): """ Given a partial or completely new sous-chef, update the souf-chef and re-validate it. """ # if the old sous chef is a SousChef object, coerce it to and from json. if isinstance(old_sous_chef, SousChef): old_sous_chef = json_to_obj(obj_to_json(old_sous_chef)) # pop the id old_sous_chef.pop('id', None) # update the previous version. new_sous_chef = update_nested_dict(old_sous_chef, new_sous_chef, overwrite=True) return validate(new_sous_chef)
def update(old_recipe, new_recipe, sous_chef): """ Given a partial or completely new recipe, update the old recipe and re-validate it. """ # if the old recipe is a Recipe object, coerce it to and from json. if isinstance(old_recipe, Recipe): old_recipe = json_to_obj(obj_to_json(old_recipe)) # format it correctly first. _rs = RecipeSchema(new_recipe, sous_chef) _rs.format_recipe() new_recipe = copy.copy(_rs.recipe) # update the previous version. new_recipe = update_nested_dict(old_recipe, new_recipe, overwrite=True) # revalidate. rs = RecipeSchema(new_recipe, sous_chef) return rs.validate()
def validate(sc): """ Validate a sous chef schema: First chef against the canoncial json schema. Then check if the `runs` field is a valid python module or an executable script that exists where it has been declared. Then check special metrics options and merge the default sous-chef options with the provied ones. """ # check the json schema _validate_sous_chef_json_schema(sc) # check for checkbox option sous chefs _validate_input_and_value_types(sc) # check if `runs` is a python module that inherits from # newslynx.sc.SousChef if not '/' in sc['runs']: sc['is_command'] = False _validate_python_sous_chef(sc) # otherwise validate the command else: sc['is_command'] = True _validate_command_sous_chef(sc) # special cases for sous chefs that create metrics if sc['creates'] == 'metrics': _validate_metrics_sous_chef(sc) # if everything is kosher, merge the sous-chef options # with the defaults sc['options'] = update_nested_dict(sc['options'], SOUS_CHEF_DEFAULT_OPTIONS) return sc
def validate(sc): """ Validate a sous chef schema: First chef against the canoncial json schema. Then check if the `runs` field is a valid python module or an executable script that exists where it has been declared. Then check special metrics options and merge the default sous-chef options with the provied ones. """ # check the json schema _validate_sous_chef_json_schema(sc) # check for checkbox option sous chefs _validate_input_and_value_types(sc) # check if `runs` is a python module that inherits from # newslynx.sc.SousChef if not '/' in sc['runs']: sc['is_command'] = False _validate_python_sous_chef(sc) # otherwise validate the command else: sc['is_command'] = True _validate_command_sous_chef(sc) # special cases for sous chefs that create metrics if sc['creates'] == 'metrics': _validate_metrics_sous_chef(sc) # if everything is kosher, merge the sous-chef options # with the defaults sc['options'] = update_nested_dict( sc['options'], SOUS_CHEF_DEFAULT_OPTIONS) return sc