Mercurial > wikked
comparison tests/mock.py @ 336:03e3e793fa22
Convert project to Python 3.4.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 19 Apr 2015 20:58:14 -0700 |
parents | ebb12ff21cb2 |
children | 64acfb58e023 |
comparison
equal
deleted
inserted
replaced
335:cc038c636901 | 336:03e3e793fa22 |
---|---|
1 import os | 1 import os |
2 import os.path | 2 import os.path |
3 import types | 3 import types |
4 import codecs | 4 import codecs |
5 import logging | 5 import logging |
6 import StringIO | 6 import io |
7 from collections import deque | 7 from collections import deque |
8 from contextlib import closing | 8 from contextlib import closing |
9 from ConfigParser import SafeConfigParser | 9 from configparser import SafeConfigParser |
10 from wikked.fs import FileSystem | 10 from wikked.fs import FileSystem |
11 from wikked.db.base import Database | 11 from wikked.db.base import Database |
12 from wikked.indexer.base import WikiIndex | 12 from wikked.indexer.base import WikiIndex |
13 from wikked.scm.base import SourceControl | 13 from wikked.scm.base import SourceControl |
14 from wikked.wiki import WikiParameters, passthrough_formatter | 14 from wikked.wiki import WikiParameters, passthrough_formatter |
61 | 61 |
62 config = SafeConfigParser() | 62 config = SafeConfigParser() |
63 config.readfp(open(default_config_path)) | 63 config.readfp(open(default_config_path)) |
64 config.set('wiki', 'root', '/fake/root') | 64 config.set('wiki', 'root', '/fake/root') |
65 if self.config_text: | 65 if self.config_text: |
66 with closing(StringIO.StringIO(self.config_text)) as conf: | 66 with closing(io.StringIO(self.config_text)) as conf: |
67 config.readfp(conf) | 67 config.readfp(conf) |
68 | 68 |
69 return config | 69 return config |
70 | 70 |
71 | 71 |
75 while len(queue) > 0: | 75 while len(queue) > 0: |
76 cur_dir, cur_node = queue.pop() | 76 cur_dir, cur_node = queue.pop() |
77 | 77 |
78 dirnames = [] | 78 dirnames = [] |
79 filenames = [] | 79 filenames = [] |
80 for name, child in cur_node.iteritems(): | 80 for name, child in cur_node.items(): |
81 if isinstance(child, dict): | 81 if isinstance(child, dict): |
82 dirnames.append(name) | 82 dirnames.append(name) |
83 else: | 83 else: |
84 filenames.append(name) | 84 filenames.append(name) |
85 yield cur_dir, dirnames, filenames | 85 yield cur_dir, dirnames, filenames |
145 os.walk = orig_walk | 145 os.walk = orig_walk |
146 | 146 |
147 @staticmethod | 147 @staticmethod |
148 def flat_to_nested(flat): | 148 def flat_to_nested(flat): |
149 nested = {} | 149 nested = {} |
150 for k, v in flat.iteritems(): | 150 for k, v in flat.items(): |
151 bits = k.lstrip('/').split('/') | 151 bits = k.lstrip('/').split('/') |
152 cur = nested | 152 cur = nested |
153 for i, b in enumerate(bits): | 153 for i, b in enumerate(bits): |
154 if i < len(bits) - 1: | 154 if i < len(bits) - 1: |
155 if b not in cur: | 155 if b not in cur: |
164 def save_structure(path, structure): | 164 def save_structure(path, structure): |
165 if not os.path.isdir(path): | 165 if not os.path.isdir(path): |
166 os.makedirs(path) | 166 os.makedirs(path) |
167 for node in structure: | 167 for node in structure: |
168 node_path = os.path.join(path, node) | 168 node_path = os.path.join(path, node) |
169 if isinstance(structure[node], types.StringTypes): | 169 if isinstance(structure[node], str): |
170 with codecs.open(node_path, 'w', encoding='utf-8') as f: | 170 with codecs.open(node_path, 'w', encoding='utf-8') as f: |
171 f.write(structure[node]) | 171 f.write(structure[node]) |
172 else: | 172 else: |
173 MockFileSystem.save_structure(node_path, structure[node]) | 173 MockFileSystem.save_structure(node_path, structure[node]) |
174 | 174 |