Mercurial > piecrust2
annotate tests/mockutil.py @ 76:fdb08d986384 2.0.0-alpha2
Add SmartyPants formatting.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sat, 30 Aug 2014 17:35:11 -0700 |
parents | 485682a6de50 |
children | 3471ffa059b2 |
rev | line source |
---|---|
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
1 import io |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
2 import time |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
3 import random |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
4 import codecs |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
5 import shutil |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
6 import os.path |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
7 import functools |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 import mock |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
9 import yaml |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
10 from piecrust.app import PieCrust, PieCrustConfiguration |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
11 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
13 resources_path = os.path.abspath( |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
14 os.path.join( |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
15 os.path.dirname(__file__), |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
16 '..', 'piecrust', 'resources')) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
17 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
18 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
19 def get_mock_app(config=None): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
20 app = mock.MagicMock(spec=PieCrust) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
21 app.config = PieCrustConfiguration() |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
22 return app |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
23 |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
24 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
25 def with_mock_fs_app(f): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
26 @functools.wraps(f) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
27 def wrapper(app, *args, **kwargs): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
28 with mock_fs_scope(app): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
29 real_app = app.getApp() |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
30 return f(real_app, *args, **kwargs) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
31 return wrapper |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
32 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
33 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
34 class mock_fs(object): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
35 def __init__(self, default_spec=True): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
36 self._root = 'root_%d' % random.randrange(1000) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
37 self._fs = {self._root: {}} |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
38 if default_spec: |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
39 self.withDir('counter') |
36
485682a6de50
New site layout support.
Ludovic Chabant <ludovic@chabant.com>
parents:
35
diff
changeset
|
40 self.withFile('kitchen/config.yml', |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
41 "site:\n title: Mock Website\n") |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
42 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
43 def path(self, p): |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
44 p = p.replace('\\', '/') |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
45 if p in ['/', '', None]: |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
46 return '/%s' % self._root |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
47 return '/%s/%s' % (self._root, p.lstrip('/')) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
48 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
49 def getApp(self): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
50 root_dir = self.path('/kitchen') |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
51 return PieCrust(root_dir, cache=False) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
52 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
53 def withDir(self, path): |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
54 path = path.replace('\\', '/') |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
55 path = path.lstrip('/') |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
56 path = '/%s/%s' % (self._root, path) |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
57 self._createDir(path) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
58 return self |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
59 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
60 def withFile(self, path, contents): |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
61 path = path.replace('\\', '/') |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
62 path = path.lstrip('/') |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
63 path = '/%s/%s' % (self._root, path) |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
64 self._createFile(path, contents) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
65 return self |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
66 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
67 def withAsset(self, path, contents): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
68 return self.withFile('kitchen/' + path, contents) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
69 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
70 def withAssetDir(self, path): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
71 return self.withDir('kitchen/' + path) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
72 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
73 def withConfig(self, config): |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
74 return self.withFile( |
36
485682a6de50
New site layout support.
Ludovic Chabant <ludovic@chabant.com>
parents:
35
diff
changeset
|
75 'kitchen/config.yml', |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
76 yaml.dump(config)) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
77 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
78 def withThemeConfig(self, config): |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
79 return self.withFile( |
36
485682a6de50
New site layout support.
Ludovic Chabant <ludovic@chabant.com>
parents:
35
diff
changeset
|
80 'kitchen/theme/theme_config.yml', |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
81 yaml.dump(config)) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
82 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
83 def withPage(self, url, config=None, contents=None): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
84 config = config or {} |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
85 contents = contents or "A test page." |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
86 text = "---\n" |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
87 text += yaml.dump(config) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
88 text += "---\n" |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
89 text += contents |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
90 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
91 name, ext = os.path.splitext(url) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
92 if not ext: |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
93 url += '.md' |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
94 url = url.lstrip('/') |
36
485682a6de50
New site layout support.
Ludovic Chabant <ludovic@chabant.com>
parents:
35
diff
changeset
|
95 return self.withAsset(url, text) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
96 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
97 def withPageAsset(self, page_url, name, contents=None): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
98 contents = contents or "A test asset." |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
99 url_base, ext = os.path.splitext(page_url) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
100 dirname = url_base + '-assets' |
36
485682a6de50
New site layout support.
Ludovic Chabant <ludovic@chabant.com>
parents:
35
diff
changeset
|
101 return self.withAsset('%s/%s' % (dirname, name), |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
102 contents) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
103 |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
104 def getStructure(self, path=None): |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
105 root = self._fs[self._root] |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
106 if path: |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
107 root = self._getEntry(self.path(path)) |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
108 |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
109 res = {} |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
110 for k, v in root.items(): |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
111 self._getStructureRecursive(v, res, k) |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
112 return res |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
113 |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
114 def _getStructureRecursive(self, src, target, name): |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
115 if isinstance(src, tuple): |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
116 target[name] = src[0] |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
117 return |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
118 |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
119 e = {} |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
120 for k, v in src.items(): |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
121 self._getStructureRecursive(v, e, k) |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
122 target[name] = e |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
123 |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
124 def _getEntry(self, path): |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
125 cur = self._fs |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
126 path = path.replace('\\', '/').lstrip('/') |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
127 bits = path.split('/') |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
128 for p in bits: |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
129 try: |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
130 cur = cur[p] |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
131 except KeyError: |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
132 return None |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
133 return cur |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
134 |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
135 def _createDir(self, path): |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
136 cur = self._fs |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
137 bits = path.strip('/').split('/') |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
138 for b in bits: |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
139 if b not in cur: |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
140 cur[b] = {} |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
141 cur = cur[b] |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
142 return self |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
143 |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
144 def _createFile(self, path, contents): |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
145 cur = self._fs |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
146 bits = path.strip('/').split('/') |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
147 for b in bits[:-1]: |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
148 if b not in cur: |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
149 cur[b] = {} |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
150 cur = cur[b] |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
151 cur[bits[-1]] = (contents, {'mtime': time.time()}) |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
152 return self |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
153 |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
154 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
155 class mock_fs_scope(object): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
156 def __init__(self, fs): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
157 self._fs = fs |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
158 self._patchers = [] |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
159 self._originals = {} |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
160 |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
161 @property |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
162 def root(self): |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
163 return self._fs._root |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
164 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
165 def __enter__(self): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
166 self._startMock() |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
167 return self |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
168 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
169 def __exit__(self, type, value, traceback): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
170 self._endMock() |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
171 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
172 def _startMock(self): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
173 self._createMock('__main__.open', open, self._open, create=True) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
174 self._createMock('codecs.open', codecs.open, self._codecsOpen) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
175 self._createMock('os.listdir', os.listdir, self._listdir) |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
176 self._createMock('os.makedirs', os.makedirs, self._makedirs) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
177 self._createMock('os.path.isdir', os.path.isdir, self._isdir) |
24
644869022b6e
Mock `os.path.isfile`, and fix a few other test utilities.
Ludovic Chabant <ludovic@chabant.com>
parents:
11
diff
changeset
|
178 self._createMock('os.path.isfile', os.path.isfile, self._isfile) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
179 self._createMock('os.path.islink', os.path.islink, self._islink) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
180 self._createMock('os.path.getmtime', os.path.getmtime, self._getmtime) |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
181 self._createMock('shutil.copyfile', shutil.copyfile, self._copyfile) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
182 for p in self._patchers: |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
183 p.start() |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
184 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
185 def _endMock(self): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
186 for p in self._patchers: |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
187 p.stop() |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
188 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
189 def _createMock(self, name, orig, func, **kwargs): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
190 self._originals[name] = orig |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
191 self._patchers.append(mock.patch(name, func, **kwargs)) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
192 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
193 def _open(self, path, *args, **kwargs): |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
194 path = os.path.normpath(path) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
195 if path.startswith(resources_path): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
196 return self._originals['__main__.open'](path, **kwargs) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
197 e = self._getFsEntry(path) |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
198 if e is None: |
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
199 raise OSError("No such file: %s" % path) |
24
644869022b6e
Mock `os.path.isfile`, and fix a few other test utilities.
Ludovic Chabant <ludovic@chabant.com>
parents:
11
diff
changeset
|
200 if not isinstance(e, tuple): |
644869022b6e
Mock `os.path.isfile`, and fix a few other test utilities.
Ludovic Chabant <ludovic@chabant.com>
parents:
11
diff
changeset
|
201 raise OSError("'%s' is not a file" % path) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
202 return io.StringIO(e[0]) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
203 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
204 def _codecsOpen(self, path, *args, **kwargs): |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
205 path = os.path.normpath(path) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
206 if path.startswith(resources_path): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
207 return self._originals['codecs.open'](path, *args, **kwargs) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
208 e = self._getFsEntry(path) |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
209 if e is None: |
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
210 raise OSError("No such file: %s" % path) |
24
644869022b6e
Mock `os.path.isfile`, and fix a few other test utilities.
Ludovic Chabant <ludovic@chabant.com>
parents:
11
diff
changeset
|
211 if not isinstance(e, tuple): |
644869022b6e
Mock `os.path.isfile`, and fix a few other test utilities.
Ludovic Chabant <ludovic@chabant.com>
parents:
11
diff
changeset
|
212 raise OSError("'%s' is not a file" % path) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
213 return io.StringIO(e[0]) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
214 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
215 def _listdir(self, path): |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
216 if not path.startswith('/' + self.root): |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
217 return self._originals['os.listdir'](path) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
218 e = self._getFsEntry(path) |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
219 if e is None: |
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
220 raise OSError("No such directory: %s" % path) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
221 if not isinstance(e, dict): |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
222 raise OSError("'%s' is not a directory." % path) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
223 return list(e.keys()) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
224 |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
225 def _makedirs(self, path, mode): |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
226 if not path.startswith('/' + self.root): |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
227 raise Exception("Shouldn't create directory: %s" % path) |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
228 self._fs._createDir(path) |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
229 |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
230 def _isdir(self, path): |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
231 if not path.startswith('/' + self.root): |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
232 return self._originals['os.path.isdir'](path) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
233 e = self._getFsEntry(path) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
234 return e is not None and isinstance(e, dict) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
235 |
24
644869022b6e
Mock `os.path.isfile`, and fix a few other test utilities.
Ludovic Chabant <ludovic@chabant.com>
parents:
11
diff
changeset
|
236 def _isfile(self, path): |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
237 if not path.startswith('/' + self.root): |
24
644869022b6e
Mock `os.path.isfile`, and fix a few other test utilities.
Ludovic Chabant <ludovic@chabant.com>
parents:
11
diff
changeset
|
238 return self._originals['os.path.isfile'](path) |
644869022b6e
Mock `os.path.isfile`, and fix a few other test utilities.
Ludovic Chabant <ludovic@chabant.com>
parents:
11
diff
changeset
|
239 e = self._getFsEntry(path) |
644869022b6e
Mock `os.path.isfile`, and fix a few other test utilities.
Ludovic Chabant <ludovic@chabant.com>
parents:
11
diff
changeset
|
240 return e is not None and isinstance(e, tuple) |
644869022b6e
Mock `os.path.isfile`, and fix a few other test utilities.
Ludovic Chabant <ludovic@chabant.com>
parents:
11
diff
changeset
|
241 |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
242 def _islink(self, path): |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
243 if not path.startswith('/' + self.root): |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
244 return self._originals['os.path.islink'](path) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
245 return False |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
246 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
247 def _getmtime(self, path): |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
248 if not path.startswith('/' + self.root): |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
249 return self._originals['os.path.getmtime'](path) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
250 e = self._getFsEntry(path) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
251 if e is None: |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
252 raise OSError("No such file: %s" % path) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
253 return e[1]['mtime'] |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
254 |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
255 def _copyfile(self, src, dst): |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
256 if not src.startswith('/' + self.root): |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
257 with open(src, 'r') as fp: |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
258 src_text = fp.read() |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
259 else: |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
260 e = self._getFsEntry(src) |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
261 src_text = e[0] |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
262 if not dst.startswith('/' + self.root): |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
263 raise Exception("Shouldn't copy to: %s" % dst) |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
264 self._fs._createFile(dst, src_text) |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
265 |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
266 def _getFsEntry(self, path): |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
267 return self._fs._getEntry(path) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
268 |