pub async fn kernels_push(
        &self,
        folder: impl AsRef<Path>,
    ) -> anyhow::Result<KernelPushResponse> {
        let folder = folder.as_ref();
        let metadata = Self::read_kernel_metadata_file(folder).await?;

        if metadata.title.len() < 5 {
            return Err(KaggleError::meta("Title must be at least five characters").into());
        }

        metadata.is_dataset_sources_valid()?;
        metadata.is_kernel_sources_valid()?;

        let code_path = metadata
            .code_file
            .ok_or_else(|| KaggleError::meta("A source file must be specified in the metadata"))?;

        let code_file = folder.join(code_path);
        if !code_file.is_file() && !code_file.exists() {
            return Err(KaggleError::meta(format!(
                "Source file not found:{}",
                code_file.display()
            ))
            .into());
        }

        let (_owner_slug, kernel_slug) = self
            .get_user_and_identifier_slug(&metadata.id)
            .map(|(s1, s2)| (s1.to_string(), s2.to_string()))?;

        if kernel_slug.to_lowercase() != slug::slugify(&metadata.title) {
            return Err(
                KaggleError::meta("kernel title does not resolve to the specified id").into(),
            );
        }

        let script_body = tokio::fs::read(&code_file).await?;

        let text = if Some(PushKernelType::Notebook) == metadata.kernel_type {
            let mut json_body = serde_json::from_slice::<serde_json::Value>(&script_body)?;

            // clean outputs
            let obj = json_body
                .as_object_mut()
                .context("Expected json object in code file")?;
            if let Some(cells) = obj.get_mut("cells").and_then(|x| x.as_array_mut()) {
                for cell in cells {
                    if let Some(cell_obj) = cell.as_object_mut() {
                        if cell_obj.contains_key("outputs")
                            && Some("code") == cell_obj.get("cell_type").and_then(|x| x.as_str())
                        {
                            cell_obj
                                .insert("outputs".to_string(), serde_json::Value::Array(vec![]));
                        }
                    }
                }
            }
            serde_json::to_string(&json_body)?
        } else {
            String::from_utf8_lossy(&script_body).to_string()
        };

        let language = if Some(PushKernelType::Notebook) == metadata.kernel_type
            && Some(PushLanguageType::Rmarkdown) == metadata.language
        {
            Some(PushLanguageType::R)
        } else {
            metadata.language
        };

        let mut req = KernelPushRequest::new(text)
            .with_new_title(metadata.title)
            .with_slug(metadata.id)
            .with_dataset_data_sources(metadata.dataset_sources)
            .with_competition_data_sources(metadata.competition_sources)
            .with_kernel_data_sources(metadata.kernel_sources)
            .with_category_ids(metadata.keywords);

        if let Some(id_no) = metadata.id_no {
            req.set_id(id_no);
        }
        if let Some(language) = language {
            req.set_language(language);
        }
        if let Some(kernel) = metadata.kernel_type {
            req.set_kernel_type(kernel);
        }
        if let Some(enable_gpu) = metadata.enable_gpu {
            req.set_enable_gpu(enable_gpu);
        }
        if let Some(enable_internet) = metadata.enable_internet {
            req.set_enable_internet(enable_internet);
        }
        if let Some(is_private) = metadata.is_private {
            req.set_is_private(is_private);
        }

        Ok(self.kernel_push(&req).await?)
    }
/// read the metadata file and kernel files from a notebook, validate both,
/// and use Kernel API to push to Kaggle if all is valid.
read the metadata file and kernel files from a notebook, validate both,
and use Kernel API to push to Kaggle if all is valid.
read the metadata file and kernel files from a notebook validate both and use Kernel API to push to Kaggle if all is valid .
pub async fn kernels_push(
        &self,
        folder: impl AsRef<Path>,
    ) -> anyhow::Result<KernelPushResponse> {
        let folder = folder.as_ref();
        let metadata = Self::read_kernel_metadata_file(folder).await?;
        if metadata.title.len() < 5 {
            return Err(KaggleError::meta("Title must be at least five characters").into());
        }
        metadata.is_dataset_sources_valid()?;
        metadata.is_kernel_sources_valid()?;
        let code_path = metadata
            .code_file
            .ok_or_else(|| KaggleError::meta("A source file must be specified in the metadata"))?;
        let code_file = folder.join(code_path);
        if !code_file.is_file() && !code_file.exists() {
            return Err(KaggleError::meta(format!(
                "Source file not found:{}",
                code_file.display()
            ))
            .into());
        }
        let (_owner_slug, kernel_slug) = self
            .get_user_and_identifier_slug(&metadata.id)
            .map(|(s1, s2)| (s1.to_string(), s2.to_string()))?;
        if kernel_slug.to_lowercase() != slug::slugify(&metadata.title) {
            return Err(
                KaggleError::meta("kernel title does not resolve to the specified id").into(),
            );
        }
        let script_body = tokio::fs::read(&code_file).await?;
        let text = if Some(PushKernelType::Notebook) == metadata.kernel_type {
            let mut json_body = serde_json::from_slice::<serde_json::Value>(&script_body)?;
            // clean outputs
            let obj = json_body
                .as_object_mut()
                .context("Expected json object in code file")?;
            if let Some(cells) = obj.get_mut("cells").and_then(|x| x.as_array_mut()) {
                for cell in cells {
                    if let Some(cell_obj) = cell.as_object_mut() {
                        if cell_obj.contains_key("outputs")
                            && Some("code") == cell_obj.get("cell_type").and_then(|x| x.as_str())
                        {
                            cell_obj
                                .insert("outputs".to_string(), serde_json::Value::Array(vec![]));
                        }
                    }
                }
            }
            serde_json::to_string(&json_body)?
        } else {
            String::from_utf8_lossy(&script_body).to_string()
        };
        let language = if Some(PushKernelType::Notebook) == metadata.kernel_type
            && Some(PushLanguageType::Rmarkdown) == metadata.language
        {
            Some(PushLanguageType::R)
        } else {
            metadata.language
        };
        let mut req = KernelPushRequest::new(text)
            .with_new_title(metadata.title)
            .with_slug(metadata.id)
            .with_dataset_data_sources(metadata.dataset_sources)
            .with_competition_data_sources(metadata.competition_sources)
            .with_kernel_data_sources(metadata.kernel_sources)
            .with_category_ids(metadata.keywords);
        if let Some(id_no) = metadata.id_no {
            req.set_id(id_no);
        }
        if let Some(language) = language {
            req.set_language(language);
        }
        if let Some(kernel) = metadata.kernel_type {
            req.set_kernel_type(kernel);
        }
        if let Some(enable_gpu) = metadata.enable_gpu {
            req.set_enable_gpu(enable_gpu);
        }
        if let Some(enable_internet) = metadata.enable_internet {
            req.set_enable_internet(enable_internet);
        }
        if let Some(is_private) = metadata.is_private {
            req.set_is_private(is_private);
        }
        Ok(self.kernel_push(&req).await?)
    }
pub async fn kernels_push ( & self , folder : impl AsRef < Path > , ) -> anyhow :: Result < KernelPushResponse > { let folder = folder . as_ref ( ) ; let metadata = Self :: read_kernel_metadata_file ( folder ) . await ? ; if metadata . title . len ( ) < 5 { return Err ( KaggleError :: meta ( " " ) . into ( ) ) ; } metadata . is_dataset_sources_valid ( ) ? ; metadata . is_kernel_sources_valid ( ) ? ; let code_path = metadata . code_file . ok_or_else ( | | KaggleError :: meta ( " " ) ) ? ; let code_file = folder . join ( code_path ) ; if ! code_file . is_file ( ) && ! code_file . exists ( ) { return Err ( KaggleError :: meta ( format ! ( " " code_file display ( ) ) ) . into ( ) ) ; } let ( _owner_slug , kernel_slug ) = self . get_user_and_identifier_slug ( & metadata . id ) . map ( | ( s1 , s2 ) | ( s1 . to_string ( ) , s2 . to_string ( ) ) ) ? ; if kernel_slug . to_lowercase ( ) != slug :: slugify ( & metadata . title ) { return Err ( KaggleError :: meta ( " " ) . into ( ) , ) ; } let script_body = tokio :: fs :: read ( & code_file ) . await ? ; let text = if Some ( PushKernelType :: Notebook ) == metadata . kernel_type { let mut json_body = serde_json :: from_slice :: < serde_json :: Value > ( & script_body ) ? ; // clean outputs let obj = json_body . as_object_mut ( ) . context ( " " ) ? ; if let Some ( cells ) = obj . get_mut ( " " ) . and_then ( | x | x . as_array_mut ( ) ) { for cell in cells { if let Some ( cell_obj ) = cell . as_object_mut ( ) { if cell_obj . contains_key ( " " ) && Some ( " " ) == cell_obj . get ( " " ) . and_then ( | x | x . as_str ( ) ) { cell_obj . insert ( " " . to_string ( ) , serde_json :: Value :: Array ( vec ! [ ] ) ) ; } } } } serde_json :: to_string ( & json_body ) ? } else { String :: from_utf8_lossy ( & script_body ) . to_string ( ) } ; let language = if Some ( PushKernelType :: Notebook ) == metadata . kernel_type && Some ( PushLanguageType :: Rmarkdown ) == metadata . language { Some ( PushLanguageType :: R ) } else { metadata . language } ; let mut req = KernelPushRequest :: new ( text ) . with_new_title ( metadata . title ) . with_slug ( metadata . id ) . with_dataset_data_sources ( metadata . dataset_sources ) . with_competition_data_sources ( metadata . competition_sources ) . with_kernel_data_sources ( metadata . kernel_sources ) . with_category_ids ( metadata . keywords ) ; if let Some ( id_no ) = metadata . id_no { req . set_id ( id_no ) ; } if let Some ( language ) = language { req . set_language ( language ) ; } if let Some ( kernel ) = metadata . kernel_type { req . set_kernel_type ( kernel ) ; } if let Some ( enable_gpu ) = metadata . enable_gpu { req . set_enable_gpu ( enable_gpu ) ; } if let Some ( enable_internet ) = metadata . enable_internet { req . set_enable_internet ( enable_internet ) ; } if let Some ( is_private ) = metadata . is_private { req . set_is_private ( is_private ) ; } Ok ( self . kernel_push ( & req ) . await ? ) }
read the metadata file and kernel files from a notebook, validate both,
and use Kernel API to push to Kaggle if all is valid.
read the metadata file and kernel files from a notebook validate both and use Kernel API to push to Kaggle if all is valid .
&self
folder
impl AsRef<Path>
&self
folder
impl AsRef<Path>