data.py 2.71 KB
Newer Older
Gerion Entrup's avatar
Gerion Entrup committed
1
import llfuse
2
from sqlfuse.model import FuseDir, Special
3
import logging
4

Gerion Entrup's avatar
Gerion Entrup committed
5
6

class Data:
7
    def __init__(self, session, fs):
8
        self.logger = logging.getLogger('sqlfuse.data')
Gerion Entrup's avatar
Gerion Entrup committed
9
        self._cache = llfuse.ROOT_INODE * [None]
10
        self._session = session
11
12
13
14
        self._tree = FuseDir(None, llfuse.ROOT_INODE, None, "/", self,
                             root=True)
        self._cache.append(self._tree)
        self._create_tree(fs)
15
16
        self.logger.debug("created tree")
        self.logger.debug(self.print_tree())
Gerion Entrup's avatar
Gerion Entrup committed
17

18
    def _create_tree(self, fs):
19
        dtree = [{}, None]
20
        # build tree as dict
21
22
23
24
25
26
        for cpath in fs:
            path = cpath.path.split('/')
            t = dtree
            if path[0] == '':
                path = path[1:]
            for f in path:
27
28
                if f not in t[0]:
                    t[0][f] = [{}, None]
29
                t = t[0][f]
30
            t[1] = cpath
Gerion Entrup's avatar
Gerion Entrup committed
31

32
33
34
35
        # build real objects
        queue = [(dtree[0], self._tree)]
        for (dnode, node) in queue:
            for filename in dnode:
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
                (subdnode, qpath) = dnode[filename]
                nnode = self._create_tree_node(node, qpath, filename)
                if len(subdnode) > 0:
                    queue.append((subdnode, nnode))

    def _create_tree_node(self, node, qpath, filename):
        # if self defined
        if qpath is not None:
            if qpath.fuse_io:
                ndir = node.add_fuseio(
                    qpath.get(node, self._session, filename))
            else:
                ndir = node.add_special(
                    Special(qpath.query, qpath.functions, qpath.where,
                            self._session, node, filename, self,
                            qpath.fileattr))
        else:
            ndir = FuseDir(None, -1, node, filename, self)
            if not node.is_special():
                self.pin_inode(ndir)
            node.add_fuseio(ndir, filename)
        return ndir
Gerion Entrup's avatar
Gerion Entrup committed
58

59
    def get_file(result):
Gerion Entrup's avatar
Gerion Entrup committed
60
61
        pass

62
63
64
65
66
67
    def get(self, inode):
        try:
            return self._cache[inode]
        except KeyError:
            return None

68
69
70
71
    def pin_inode(self, fuseio):
        ino = len(self._cache)
        fuseio.set_inode(ino)
        self._cache.append(fuseio)
Gerion Entrup's avatar
Gerion Entrup committed
72

73
    def print_tree(self):
74
        res = ''
75
76
77
        queue = [('/', self._tree, 0)]
        while len(queue) > 0:
            (name, elem, indent) = queue.pop()
78
            res += ''.join([' ' * indent, name, ': ', elem.__repr__(), '\n'])
79
80
81
82
83
            for (name, child) in elem.get_children().items():
                queue.append((name, child, indent + 2))
            for special in elem.get_specials():
                queue.append(
                    ('special', special, indent + 2))
84
        return res