File size: 973 Bytes
6ffeb66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import fsspec
from luigi.target import FileSystem, FileSystemTarget


class FSSpecFileSystem(FileSystem):
    def __init__(self, fs: fsspec.AbstractFileSystem):
        self.fs = fs

    def exists(self, path):
        return self.fs.exists()

    def remove(self, path, recursive=True, skip_trash=True):
        raise NotImplementedError()

    def isdir(self, path):
        return self.fs.isdir(path)

    def listdir(self, path):
        return self.fs.ls(path)


class FSSpecTarget(FileSystemTarget):
    def __init__(self, path, fs: fsspec.AbstractFileSystem | None = None):
        self.path = path
        if fs is None:
            self.fsspec_fs = fsspec.filesystem("file")
        else:
            self.fsspec_fs = fs
        self._fs = None

    @property
    def fs(self):
        if self._fs is None:
            self._fs = FSSpecFileSystem(self.fsspec_fs)
        return self._fs

    def open(self, mode):
        return self.fs.open(self.path, mode=mode)