def validate(self) -> None: exceptions: List[ValidationError] = [] # verify that the metadata file is present and valid try: metadata = load_metadata(self.contents) except ValidationError as exc: exceptions.append(exc) metadata = None for file_name, content in self.contents.items(): prefix = file_name.split("/")[0] schema = schemas.get(f"{prefix}/") if schema: try: config = load_yaml(file_name, content) schema.load(config) self._configs[file_name] = config except ValidationError as exc: exc.messages = {file_name: exc.messages} exceptions.append(exc) # validate that the type declared in METADATA_FILE_NAME is correct if metadata: type_validator = validate.Equal(SqlaTable.__name__) try: type_validator(metadata["type"]) except ValidationError as exc: exc.messages = {METADATA_FILE_NAME: {"type": exc.messages}} exceptions.append(exc) if exceptions: exception = CommandInvalidError("Error importing dataset") exception.add_list(exceptions) raise exception
def validate(self) -> None: exceptions: List[ValidationError] = [] # load existing databases so we can apply the password validation db_passwords = { str(uuid): password for uuid, password in db.session.query(Database.uuid, Database.password).all() } # verify that the metadata file is present and valid try: metadata: Optional[Dict[str, str]] = load_metadata(self.contents) except ValidationError as exc: exceptions.append(exc) metadata = None # validate objects for file_name, content in self.contents.items(): prefix = file_name.split("/")[0] schema = self.schemas.get(f"{prefix}/") if schema: try: config = load_yaml(file_name, content) # populate passwords from the request or from existing DBs if file_name in self.passwords: config["password"] = self.passwords[file_name] elif prefix == "databases" and config[ "uuid"] in db_passwords: config["password"] = db_passwords[config["uuid"]] schema.load(config) self._configs[file_name] = config except ValidationError as exc: exc.messages = {file_name: exc.messages} exceptions.append(exc) # validate that the type declared in METADATA_FILE_NAME is correct if metadata: type_validator = validate.Equal( self.dao.model_cls.__name__) # type: ignore try: type_validator(metadata["type"]) except ValidationError as exc: exc.messages = {METADATA_FILE_NAME: {"type": exc.messages}} exceptions.append(exc) if exceptions: exception = CommandInvalidError( f"Error importing {self.model_name}") exception.add_list(exceptions) raise exception
def validate(self) -> None: exceptions: List[ValidationError] = [] # verify that the metadata file is present and valid try: metadata: Optional[Dict[str, str]] = load_metadata(self.contents) except ValidationError as exc: exceptions.append(exc) metadata = None validate_metadata_type(metadata, "assets", exceptions) self._configs = load_configs(self.contents, self.schemas, self.passwords, exceptions) if exceptions: exception = CommandInvalidError("Error importing assets") exception.add_list(exceptions) raise exception
def validate(self) -> None: exceptions: List[ValidationError] = [] # verify that the metadata file is present and valid try: metadata: Optional[Dict[str, str]] = load_metadata(self.contents) except ValidationError as exc: exceptions.append(exc) metadata = None self._validate_metadata_type(metadata, exceptions) self._load__configs(exceptions) self._prevent_overwrite_existing_model(exceptions) if exceptions: exception = CommandInvalidError( f"Error importing {self.model_name}") exception.add_list(exceptions) raise exception
def validate(self) -> None: exceptions: List[ValidationError] = [] # verify that the metadata file is present and valid try: metadata: Optional[Dict[str, str]] = load_metadata(self.contents) except ValidationError as exc: exceptions.append(exc) metadata = None if self.dao.model_cls: validate_metadata_type(metadata, self.dao.model_cls.__name__, exceptions) # load the configs and make sure we have confirmation to overwrite existing models self._configs = load_configs( self.contents, self.schemas, self.passwords, exceptions ) self._prevent_overwrite_existing_model(exceptions) if exceptions: exception = CommandInvalidError(f"Error importing {self.model_name}") exception.add_list(exceptions) raise exception
def run(self) -> None: # iterate over all commands until we find a version that can # handle the contents for version in command_versions: command = version(self.contents, *self.args, **self.kwargs) try: command.run() return except IncorrectVersionError: logger.debug("File not handled by command, skipping") except (CommandInvalidError, ValidationError) as exc: # found right version, but file is invalid logger.exception("Error running import command") raise exc raise CommandInvalidError( "Could not find a valid command to import file")
def run(self) -> None: # iterate over all commands until we find a version that can # handle the contents for version in command_versions: command = version(self.contents) try: command.run() return except IncorrectVersionError: # file is not handled by this command, skip pass except (CommandInvalidError, ValidationError) as exc: # found right version, but file is invalid logger.info("Command failed validation") raise exc except Exception as exc: # validation succeeded but something went wrong logger.exception("Error running import command") raise exc raise CommandInvalidError( "Could not find a valid command to import file")
def validate(self) -> None: exceptions: List[ValidationError] = [] # load existing databases so we can apply the password validation db_passwords = { str(uuid): password for uuid, password in db.session.query(Database.uuid, Database.password).all() } # verify that the metadata file is present and valid try: metadata: Optional[Dict[str, str]] = load_metadata(self.contents) except ValidationError as exc: exceptions.append(exc) metadata = None # validate that the type declared in METADATA_FILE_NAME is correct if metadata and "type" in metadata: type_validator = validate.Equal( self.dao.model_cls.__name__) # type: ignore try: type_validator(metadata["type"]) except ValidationError as exc: exc.messages = {METADATA_FILE_NAME: {"type": exc.messages}} exceptions.append(exc) # validate objects for file_name, content in self.contents.items(): # skip directories if not content: continue prefix = file_name.split("/")[0] schema = self.schemas.get(f"{prefix}/") if schema: try: config = load_yaml(file_name, content) # populate passwords from the request or from existing DBs if file_name in self.passwords: config["password"] = self.passwords[file_name] elif prefix == "databases" and config[ "uuid"] in db_passwords: config["password"] = db_passwords[config["uuid"]] schema.load(config) self._configs[file_name] = config except ValidationError as exc: exc.messages = {file_name: exc.messages} exceptions.append(exc) # check if the object exists and shouldn't be overwritten if not self.overwrite: existing_uuids = self._get_uuids() for file_name, config in self._configs.items(): if (file_name.startswith(self.prefix) and config["uuid"] in existing_uuids): exceptions.append( ValidationError({ file_name: (f"{self.model_name.title()} already exists " "and `overwrite=true` was not passed"), })) if exceptions: exception = CommandInvalidError( f"Error importing {self.model_name}") exception.add_list(exceptions) raise exception
def test_command_invalid_error(self): exception = CommandInvalidError("A test") assert str(exception) == "A test"