def from_dict(cls, data, validate=True) -> 'ProjectV2': result = super().from_dict(data, validate=validate) if result.name in BANNED_PROJECT_NAMES: raise ValidationError( f'Invalid project name: {result.name} is a reserved word') return result
def from_dict(cls, data, validate=True): result = super().from_dict(data, validate=validate) if result.name in BANNED_PROJECT_NAMES: raise ValidationError( 'Invalid project name: {} is a reserved word'.format( result.name)) return result
def to_python(self, value) -> timedelta: if isinstance(value, timedelta): return value try: return timedelta(seconds=value) except TypeError: raise ValidationError( 'cannot encode {} into timedelta'.format(value)) from None
def validate(cls, data: Any): schema = _validate_schema(cls) validator = jsonschema.Draft7Validator(schema) error = jsonschema.exceptions.best_match( validator.iter_errors(data), key=_relevance_without_strategy, ) if error is not None: raise ValidationError.create_from(error) from error
def to_python(self, value) -> Path: if isinstance(value, Path): return value try: return Path(value) except TypeError: raise ValidationError( 'cannot encode {} into timedelta'.format(value) ) from None
def parse_project_config(data: Dict[str, Any], validate=True) -> Union[ProjectV1, ProjectV2]: config_version = data.get('config-version', 1) if config_version == 1: return ProjectV1.from_dict(data, validate=validate) elif config_version == 2: return ProjectV2.from_dict(data, validate=validate) else: raise ValidationError( f'Got an unexpected config-version={config_version}, expected ' f'1 or 2')
def validate(cls, value): res = re.match(cls.ValidationRegex, value) if res is None: raise ValidationError(f"Invalid value: {value}") # TODO
def to_python(self, value) -> NoValue: if (not isinstance(value, dict) or 'novalue' not in value or value['novalue'] != 'novalue'): raise ValidationError('Got invalid NoValue: {}'.format(value)) return NoValue()
def from_project_config( cls, project_dict: Dict[str, Any], packages_dict: Optional[Dict[str, Any]] = None, selectors_dict: Optional[Dict[str, Any]] = None, required_dbt_version: Optional[List[VersionSpecifier]] = None, ) -> 'Project': """Create a project from its project and package configuration, as read by yaml.safe_load(). :param project_dict: The dictionary as read from disk :param packages_dict: If it exists, the packages file as read from disk. :raises DbtProjectError: If the project is missing or invalid, or if the packages file exists and is invalid. :returns: The project, with defaults populated. """ if required_dbt_version is None: dbt_version = cls._get_required_version(project_dict) else: dbt_version = required_dbt_version try: project_dict = cls._preprocess(project_dict) except RecursionException: raise DbtProjectError( 'Cycle detected: Project input has a reference to itself', project=project_dict ) try: cfg = parse_project_config(project_dict) except ValidationError as e: raise DbtProjectError(validator_error_message(e)) from e # name/version are required in the Project definition, so we can assume # they are present name = cfg.name version = cfg.version # this is added at project_dict parse time and should always be here # once we see it. if cfg.project_root is None: raise DbtProjectError('cfg must have a project root!') else: project_root = cfg.project_root # this is only optional in the sense that if it's not present, it needs # to have been a cli argument. profile_name = cfg.profile # these are all the defaults source_paths: List[str] = value_or(cfg.source_paths, ['models']) macro_paths: List[str] = value_or(cfg.macro_paths, ['macros']) data_paths: List[str] = value_or(cfg.data_paths, ['data']) test_paths: List[str] = value_or(cfg.test_paths, ['test']) analysis_paths: List[str] = value_or(cfg.analysis_paths, []) snapshot_paths: List[str] = value_or(cfg.snapshot_paths, ['snapshots']) all_source_paths: List[str] = _all_source_paths( source_paths, data_paths, snapshot_paths, analysis_paths, macro_paths ) docs_paths: List[str] = value_or(cfg.docs_paths, all_source_paths) asset_paths: List[str] = value_or(cfg.asset_paths, []) target_path: str = value_or(cfg.target_path, 'target') clean_targets: List[str] = value_or(cfg.clean_targets, [target_path]) log_path: str = value_or(cfg.log_path, 'logs') modules_path: str = value_or(cfg.modules_path, 'dbt_modules') # in the default case we'll populate this once we know the adapter type # It would be nice to just pass along a Quoting here, but that would # break many things quoting: Dict[str, Any] = {} if cfg.quoting is not None: quoting = cfg.quoting.to_dict() models: Dict[str, Any] seeds: Dict[str, Any] snapshots: Dict[str, Any] sources: Dict[str, Any] vars_value: VarProvider if cfg.config_version == 1: assert isinstance(cfg, ProjectV1Contract) # extract everything named 'vars' models = cfg.models seeds = cfg.seeds snapshots = cfg.snapshots sources = {} vars_value = V1VarProvider( models=models, seeds=seeds, snapshots=snapshots ) elif cfg.config_version == 2: assert isinstance(cfg, ProjectV2Contract) models = cfg.models seeds = cfg.seeds snapshots = cfg.snapshots sources = cfg.sources if cfg.vars is None: vars_dict: Dict[str, Any] = {} else: vars_dict = cfg.vars vars_value = V2VarProvider(vars_dict) else: raise ValidationError( f'Got unsupported config_version={cfg.config_version}' ) on_run_start: List[str] = value_or(cfg.on_run_start, []) on_run_end: List[str] = value_or(cfg.on_run_end, []) query_comment = _query_comment_from_cfg(cfg.query_comment) try: packages = package_config_from_data(packages_dict) except ValidationError as e: raise DbtProjectError(validator_error_message(e)) from e try: selectors = selector_config_from_data(selectors_dict) except ValidationError as e: raise DbtProjectError(validator_error_message(e)) from e project = cls( project_name=name, version=version, project_root=project_root, profile_name=profile_name, source_paths=source_paths, macro_paths=macro_paths, data_paths=data_paths, test_paths=test_paths, analysis_paths=analysis_paths, docs_paths=docs_paths, asset_paths=asset_paths, target_path=target_path, snapshot_paths=snapshot_paths, clean_targets=clean_targets, log_path=log_path, modules_path=modules_path, quoting=quoting, models=models, on_run_start=on_run_start, on_run_end=on_run_end, seeds=seeds, snapshots=snapshots, dbt_version=dbt_version, packages=packages, selectors=selectors, query_comment=query_comment, sources=sources, vars=vars_value, config_version=cfg.config_version, ) # sanity check - this means an internal issue project.validate() return project
def to_dict(self): raise ValidationError( 'to_dict was called on a v1 vars, but it should only be called ' 'on v2 vars' )