// File name does not have an extension associated with Databricks notebooks, return early.
if_,ok:=extensionsToLanguages[ext];!ok{
returnnil,nil
}
// If the file could be a notebook, check if it is and has the correct language.
stat,err:=w.stat(ctx,nameWithoutExt)
iferr!=nil{
// If the file does not exist, return early.
iferrors.As(err,&FileDoesNotExistError{}){
returnnil,nil
}
log.Debugf(ctx,"attempting to determine if %s could be a notebook. Failed to fetch the status of object at %s: %s",name,path.Join(w.root,nameWithoutExt),err)
returnnil,err
}
// Not a notebook. Return early.
ifstat.ObjectType!=workspace.ObjectTypeNotebook{
log.Debugf(ctx,"attempting to determine if %s could be a notebook. Found an object at %s but it is not a notebook. It is a %s.",name,path.Join(w.root,nameWithoutExt),stat.ObjectType)
returnnil,nil
}
// Not the correct language. Return early.
ifstat.Language!=extensionsToLanguages[ext]{
log.Debugf(ctx,"attempting to determine if %s could be a notebook. Found a notebook at %s but it is not of the correct language. Expected %s but found %s.",name,path.Join(w.root,nameWithoutExt),extensionsToLanguages[ext],stat.Language)
returnnil,nil
}
// When the extension is .py we expect the export format to be source.
log.Debugf(ctx,"attempting to determine if %s could be a notebook. Found a notebook at %s but it is not exported as a source notebook. Its export format is %s.",name,path.Join(w.root,nameWithoutExt),stat.ReposExportFormat)
returnnil,nil
}
// When the extension is .ipynb we expect the export format to be Jupyter.
log.Debugf(ctx,"attempting to determine if %s could be a notebook. Found a notebook at %s but it is not exported as a Jupyter notebook. Its export format is %s.",name,path.Join(w.root,nameWithoutExt),stat.ReposExportFormat)
returnnil,nil
}
// Modify the stat object path to include the extension. This stat object will be used
// to return the fs.FileInfo object in the stat method.
// Modify the stat object path to include the extension. This stat object will be used
// to return the fs.DirEntry object in the ReadDir method.
stat.Path=stat.Path+ext
returnstat,nil
}
typeDuplicatePathErrorstruct{
oi1workspace.ObjectInfo
oi2workspace.ObjectInfo
commonNamestring
}
func(eDuplicatePathError)Error()string{
returnfmt.Sprintf("failed to read files from the workspace file system. Duplicate paths encountered. Both %s at %s and %s at %s resolve to the same name %s. Changing the name of one of these objects will resolve this issue",e.oi1.ObjectType,e.oi1.Path,e.oi2.ObjectType,e.oi2.Path,e.commonName)
}
// This is a filer for the workspace file system that allows you to pretend the
// workspace file system is a traditional file system. It allows you to list, read, write,
// delete, and stat notebooks (and files in general) in the workspace, using their paths
// with the extension included.
//
// The ReadDir method returns a DuplicatePathError if this traditional file system view is
// not possible. For example, a Python notebook called foo and a Python file called `foo.py`
// would resolve to the same path `foo.py` in a tradition file system.
//
// Users of this filer should be careful when using the Write and Mkdir methods.
// The underlying import API we use to upload notebooks and files returns opaque internal
// errors for namespace clashes (e.g. a file and a notebook or a directory and a notebook).
// Thus users of these methods should be careful to avoid such clashes.