rev: 491566fa24ddd88cee7987a361d3a63f466f8c76 Wikked/tests/mock.py -rw-r--r-- 6.7 KiB View raw Log this file
491566fa24ddLudovic Chabant cm: Upgrade dependencies and fix deprecated APIs. 1 year, 6 months ago
                                                                                
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import os
import os.path
import codecs
import logging
import io
from collections import deque
from contextlib import closing
from configparser import ConfigParser
from wikked.fs import FileSystem
from wikked.db.base import Database
from wikked.indexer.base import WikiIndex
from wikked.scm.base import SourceControl
from wikked.wiki import WikiParameters, passthrough_formatter


logger = logging.getLogger(__name__)


class MockWikiParameters(WikiParameters):
    def __init__(self, root=None):
        super(MockWikiParameters, self).__init__(root)
        self.config_text = ""
        self.mock_fs = None
        self.mock_index = None
        self.mock_db = None
        self.mock_scm = None

    def fs_factory(self):
        if self.mock_fs is False:
            return super(MockWikiParameters, self).fs_factory()
        return self.mock_fs or MockFileSystem(self.root, self.config)

    def index_factory(self):
        if self.mock_index is False:
            return super(MockWikiParameters, self).index_factory()
        return self.mock_index or MockWikiIndex()

    def db_factory(self):
        if self.mock_db is False:
            return super(MockWikiParameters, self).db_factory()
        return self.mock_db or MockDatabase()

    def scm_factory(self, for_init=False):
        if self.mock_scm is False:
            return super(MockWikiParameters, self).scm_factory(for_init)
        return self.mock_scm or MockSourceControl()

    def getFormatters(self):
        formatters = {
            passthrough_formatter: ['txt', 'html']
        }
        return formatters

    def getPageUpdater(self):
        return lambda wiki, url: wiki.update(url, cache_ext_data=True)

    def _loadConfig(self):
        default_config_path = os.path.join(
            os.path.dirname(__file__), '..',
            'wikked', 'resources', 'defaults.cfg')

        config = ConfigParser()
        config.read_file(open(default_config_path))
        config.set('wiki', 'root', '/fake/root')
        if self.config_text:
            with closing(io.StringIO(self.config_text)) as conf:
                config.read_file(conf)

        return config


def mock_os_walk(root_dir, root_node):
    queue = deque()
    queue.appendleft((root_dir, root_node))
    while len(queue) > 0:
        cur_dir, cur_node = queue.pop()

        dirnames = []
        filenames = []
        for name, child in cur_node.items():
            if isinstance(child, dict):
                dirnames.append(name)
            else:
                filenames.append(name)
        yield cur_dir, dirnames, filenames
        for name in dirnames:
            fullname = os.path.join(cur_dir, name)
            queue.appendleft((fullname, cur_node[name]))


class MockFileSystem(FileSystem):
    def __init__(self, root, config, structure=None):
        super(MockFileSystem, self).__init__(root, config)
        self.include_builtin_endpoints = False
        if not structure:
            self.structure = {}
        else:
            self.structure = MockFileSystem.flat_to_nested(structure)

    def getPageInfos(self, subdir=None):
        def tmp_walk(path):
            node = self._getNode(path)
            if node is None:
                raise Exception("No node at %s" % path)
            return mock_os_walk(path, node)

        orig_walk = os.walk
        os.walk = tmp_walk
        try:
            gen = super(MockFileSystem, self).getPageInfos(subdir)
            return list(gen)
        finally:
            os.walk = orig_walk

    def setPage(self, url, content):
        raise NotImplementedError()

    def _getPageInfo(self, path):
        pi = super(MockFileSystem, self)._getPageInfo(path)
        node = self._getNode(path)
        if node is not None:
            pi._content = node
        else:
            raise Exception("Can't find node: %s" % path)
        return pi

    def _getNode(self, path):
        node = self.structure
        path = path.lstrip('/')
        if path != '':
            for n in path.split('/'):
                if n not in node:
                    return None
                node = node[n]
        return node

    def _getPhysicalPath(self, url, is_file=True, make_new=False):
        def tmp_walk(path):
            node = self._getNode(path)
            if node is None:
                raise Exception("No node at %s" % path)
            return mock_os_walk(path, node)

        orig_walk = os.walk
        os.walk = tmp_walk
        try:
            return super(MockFileSystem, self)._getPhysicalPath(url, is_file,
                                                                make_new)
        finally:
            os.walk = orig_walk

    @staticmethod
    def flat_to_nested(flat):
        nested = {}
        for k, v in flat.items():
            bits = k.lstrip('/').split('/')
            cur = nested
            for i, b in enumerate(bits):
                if i < len(bits) - 1:
                    if b not in cur:
                        cur[b] = {}
                    cur = cur[b]
                else:
                    cur[b] = v
        return nested

    @staticmethod
    def save_structure(path, structure):
        if not os.path.isdir(path):
            os.makedirs(path)
        for node in structure:
            node_path = os.path.join(path, node)
            if isinstance(structure[node], str):
                with codecs.open(node_path, 'w', encoding='utf-8') as f:
                    f.write(structure[node])
            else:
                MockFileSystem.save_structure(node_path, structure[node])


class MockDatabase(Database):
    def __init__(self, content=None):
        super(MockDatabase, self).__init__()
        self.content = content

    def getPageUrls(self, subdir=None, uncached_only=False):
        return []

    def getPages(self, subdir=None, meta_query=None, uncached_only=False,
                 fields=None):
        return []

    def isCacheValid(self, page):
        return False

    def pageExists(self, url=None, path=None):
        return False

    def getLinksTo(self, url):
        return []

    def _getPageByUrl(self, url, fields):
        return None

    def _getPageByPath(self, path, fields):
        return None


class MockWikiIndex(WikiIndex):
    def __init__(self):
        super(MockWikiIndex, self).__init__()

    def search(self, query):
        # url, title, content_highlights
        return None


class MockSourceControl(SourceControl):
    def __init__(self):
        super(MockSourceControl, self).__init__()

    def getSpecialFilenames(self):
        return []

    def getHistory(self, path=None):
        return []

    def getState(self, path):
        raise NotImplementedError()

    def getRevision(self, path, rev):
        raise NotImplementedError()

    def diff(self, path, rev1, rev2):
        raise NotImplementedError()