annotate tests/mock.py @ 168:a71822a4beed

Refactoring to not better use Flask, only when needed: - command-line utility is using plain `argparse`. - no Flask app created unless we want to run a server. - split all commands into sub-modules. - do more initialization stuff in the `WikiParameters`. - make `Wiki` init as cheap as possible. - one `Wiki` instance now created per request.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 22 Jan 2014 21:39:02 -0800
parents fd6eccb24882
children ebb12ff21cb2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import re
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import os.path
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 import types
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 import codecs
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 import logging
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6 import StringIO
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 from wikked.page import Page
82
9afe4a1dbd1e Refactoring of core wiki classes:
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
8 from wikked.fs import PageInfo, PageNotFoundError
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 from wikked.db import Database
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 from wikked.indexer import WikiIndex
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11 from wikked.scm import SourceControl
100
fd6eccb24882 Refactoring to get rid of `slugify` callbacks.
Ludovic Chabant <ludovic@chabant.com>
parents: 82
diff changeset
12 from wikked.utils import title_to_url
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 class MockWikiParameters(object):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 def __init__(self):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 self.formatters = {
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 self._passthrough: ['txt', 'html']
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19 }
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 self.config_text = ""
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 self.special_filenames = []
51
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
23 self.use_db = False
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25 self.logger_factory = lambda: logging.getLogger('wikked.tests')
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 self.page_factory = lambda wiki, url: MockPage(wiki, url)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 self.config_factory = lambda: StringIO.StringIO(self.config_text)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28 self.fs_factory = lambda cfg: MockFileSystem()
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29 self.index_factory = lambda cfg: MockWikiIndex()
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 self.db_factory = lambda cfg: MockDatabase()
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 self.scm_factory = lambda cfg: MockSourceControl()
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 def getSpecialFilenames(self):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 return self.special_filenames
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36 def _passthrough(self, text):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37 return text
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40 class MockPage(Page):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 def __init__(self, wiki, url):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42 Page.__init__(self, wiki, url)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
43
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45 class MockDatabase(Database):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
46 def __init__(self, content=None, logger=None):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
47 Database.__init__(self, logger)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
48 self.content = content
51
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
49 self.conn = None
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
50 self._open_count = 0
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
51
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
52 def initDb(self):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
53 pass
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
55 def open(self):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
56 self._open_count += 1
51
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
57 self.conn = 'MOCK_CONNECTION'
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59 def close(self):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60 self._open_count -= 1
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61 if self._open_count < 0:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
62 raise Exception(
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
63 "The database was closed more times than it was open.")
51
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
64 elif self._open_count == 0:
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
65 self.conn = None
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
66
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
67 def reset(self, pages):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
68 pass
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
69
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
70 def update(self, pages):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
71 pass
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
72
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
73 def getPageUrls(self, subdir=None):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
74 return []
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
75
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
76 def getPages(self, subdir=None):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
77 return []
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
78
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
79 def getPage(self, url):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
80 return None
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
81
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
82 def pageExists(self, url):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
83 return False
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
84
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
85 def getLinksTo(self, url):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
86 return []
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
87
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
88
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
89 class MockFileSystem():
100
fd6eccb24882 Refactoring to get rid of `slugify` callbacks.
Ludovic Chabant <ludovic@chabant.com>
parents: 82
diff changeset
90 def __init__(self, structure=None, logger=None):
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
91 if not structure:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
92 structure = []
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
93 self.structure = structure
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
94 self.logger = logger
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
95 self.excluded = []
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
96
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
97 def getPageInfos(self, subdir=None):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
98 node = self._getNode(subdir)
51
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
99 if node is None:
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
100 raise PageNotFoundError()
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
101 for n in self._getChildren(node):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
102 yield self._getPageInfo(n)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
103
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
104 def getPageInfo(self, path):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
105 node = self._getNode(path)
51
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
106 if node is None:
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
107 raise PageNotFoundError()
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
108 return self._getPageInfo(node)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
109
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
110 def getPage(self, url):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
111 path = self._getPath(url, True)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
112 node = self._getNode(path)
51
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
113 if node is None:
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
114 raise PageNotFoundError()
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
115 return self._getPageInfo(node, True)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
116
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
117 def setPage(self, path, content):
51
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
118 raise NotImplementedError()
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
119
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
120 def pageExists(self, url):
51
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
121 try:
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
122 self._getPath(url, True)
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
123 return True
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
124 except PageNotFoundError:
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
125 return False
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
126
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
127 def getPhysicalNamespacePath(self, url):
51
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
128 raise NotImplementedError()
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
129
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
130 def _getPageInfo(self, node, with_content=False):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
131 path_split = os.path.splitext(node['path'])
100
fd6eccb24882 Refactoring to get rid of `slugify` callbacks.
Ludovic Chabant <ludovic@chabant.com>
parents: 82
diff changeset
132 url = title_to_url(path_split[0])
82
9afe4a1dbd1e Refactoring of core wiki classes:
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
133 info = PageInfo(url, node['path'])
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
134 if with_content:
82
9afe4a1dbd1e Refactoring of core wiki classes:
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
135 info.content = node['content']
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
136 return info
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
137
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
138 def _getNode(self, path):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
139 node = self.structure
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
140 if path:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
141 for n in path.split('/'):
51
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
142 if n not in node:
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
143 return None
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
144 node = node[n]
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
145 else:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
146 path = ''
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
147 if isinstance(node, types.StringTypes):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
148 return {'type': 'file', 'path': path, 'content': node}
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
149 return {'type': 'dir', 'path': path, 'content': node}
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
150
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
151 def _getChildren(self, node):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
152 if node['type'] != 'dir':
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
153 raise Exception("'%s' is not a directory." % node['path'])
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
154 for name in node['content']:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
155 child_path = os.path.join(node['path'], name)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
156 child = node['content'][name]
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
157 if isinstance(child, types.StringTypes):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
158 yield {
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
159 'type': 'file',
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
160 'path': child_path,
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
161 'content': child
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
162 }
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
163 else:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
164 for c in self._getChildren({
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
165 'type': 'dir',
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
166 'path': child_path,
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
167 'content': child
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
168 }):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
169 yield c
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
170
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
171 def _getPath(self, url, is_file):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
172 path = ''
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
173 current = self.structure
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
174 parts = unicode(url).lower().split('/')
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
175 for i, part in enumerate(parts):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
176 for name in current:
100
fd6eccb24882 Refactoring to get rid of `slugify` callbacks.
Ludovic Chabant <ludovic@chabant.com>
parents: 82
diff changeset
177 name_slug = title_to_url(name)
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
178 if is_file and i == len(parts) - 1:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
179 if re.match(r"%s\.[a-z]+" % re.escape(part), name_slug):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
180 current = current[name]
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
181 path = os.path.join(path, name)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
182 break
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
183 else:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
184 if name_slug == part:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
185 current = current[name]
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
186 path = os.path.join(path, name)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
187 break
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
188 else:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
189 # Failed to find a part of the URL.
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
190 raise PageNotFoundError("No such page: " + url)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
191 return path
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
192
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
193 @staticmethod
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
194 def save_structure(path, structure):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
195 if not os.path.isdir(path):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
196 os.makedirs(path)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
197 for node in structure:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
198 node_path = os.path.join(path, node)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
199 if isinstance(structure[node], types.StringTypes):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
200 with codecs.open(node_path, 'w', encoding='utf-8') as f:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
201 f.write(structure[node])
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
202 else:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
203 MockFileSystem.save_structure(node_path, structure[node])
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
204
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
205
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
206 class MockWikiIndex(WikiIndex):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
207 def __init__(self, logger=None):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
208 WikiIndex.__init__(self, logger)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
209
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
210 def initIndex(self):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
211 pass
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
212
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
213 def reset(self, pages):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
214 pass
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
215
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
216 def update(self, pages):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
217 pass
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
218
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
219 def search(self, query):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
220 # url, title, content_highlights
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
221 return None
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
222
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
223
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
224 class MockSourceControl(SourceControl):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
225 def __init__(self, logger=None):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
226 SourceControl.__init__(self, logger)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
227
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
228 def initRepo(self):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
229 pass
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
230
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
231 def getSpecialFilenames(self):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
232 return []
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
233
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
234 def getHistory(self, path=None):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
235 return []
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
236
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
237 def getState(self, path):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
238 raise NotImplementedError()
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
239
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
240 def getRevision(self, path, rev):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
241 raise NotImplementedError()
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
242
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
243 def diff(self, path, rev1, rev2):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
244 raise NotImplementedError()
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
245
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
246 def commit(self, paths, op_meta):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
247 raise NotImplementedError()
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
248
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
249 def revert(self, paths=None):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
250 raise NotImplementedError()