annotate piecrust/osutil.py @ 1145:e94737572542

serve: Fix an issue where false positive matches were rendered as the requested page. Now we try to render the page, but also try to detect for the most common "empty" pages.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 05 Jun 2018 22:08:51 -0700
parents ea6cbd6d2af5
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
526
9b8b47fb1068 bug: Forgot to add a new file like a big n00b.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import os
9b8b47fb1068 bug: Forgot to add a new file like a big n00b.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import sys
527
fa9eb8f866cd bug: Fix file-system wrappers for non-Mac systems.
Ludovic Chabant <ludovic@chabant.com>
parents: 526
diff changeset
3 import glob as _system_glob
526
9b8b47fb1068 bug: Forgot to add a new file like a big n00b.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 import unicodedata
9b8b47fb1068 bug: Forgot to add a new file like a big n00b.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5
9b8b47fb1068 bug: Forgot to add a new file like a big n00b.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6
527
fa9eb8f866cd bug: Fix file-system wrappers for non-Mac systems.
Ludovic Chabant <ludovic@chabant.com>
parents: 526
diff changeset
7 walk = os.walk
fa9eb8f866cd bug: Fix file-system wrappers for non-Mac systems.
Ludovic Chabant <ludovic@chabant.com>
parents: 526
diff changeset
8 listdir = os.listdir
fa9eb8f866cd bug: Fix file-system wrappers for non-Mac systems.
Ludovic Chabant <ludovic@chabant.com>
parents: 526
diff changeset
9 glob = _system_glob.glob
fa9eb8f866cd bug: Fix file-system wrappers for non-Mac systems.
Ludovic Chabant <ludovic@chabant.com>
parents: 526
diff changeset
10
fa9eb8f866cd bug: Fix file-system wrappers for non-Mac systems.
Ludovic Chabant <ludovic@chabant.com>
parents: 526
diff changeset
11
638
a4ac464a45b3 internal: Remove SyntaxWarning from MacOS wrappers.
Ludovic Chabant <ludovic@chabant.com>
parents: 546
diff changeset
12 def _wrap_fs_funcs():
a4ac464a45b3 internal: Remove SyntaxWarning from MacOS wrappers.
Ludovic Chabant <ludovic@chabant.com>
parents: 546
diff changeset
13 global walk
a4ac464a45b3 internal: Remove SyntaxWarning from MacOS wrappers.
Ludovic Chabant <ludovic@chabant.com>
parents: 546
diff changeset
14 global listdir
a4ac464a45b3 internal: Remove SyntaxWarning from MacOS wrappers.
Ludovic Chabant <ludovic@chabant.com>
parents: 546
diff changeset
15 global glob
a4ac464a45b3 internal: Remove SyntaxWarning from MacOS wrappers.
Ludovic Chabant <ludovic@chabant.com>
parents: 546
diff changeset
16
527
fa9eb8f866cd bug: Fix file-system wrappers for non-Mac systems.
Ludovic Chabant <ludovic@chabant.com>
parents: 526
diff changeset
17 def _walk(top, **kwargs):
526
9b8b47fb1068 bug: Forgot to add a new file like a big n00b.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 for dirpath, dirnames, filenames in os.walk(top, **kwargs):
9b8b47fb1068 bug: Forgot to add a new file like a big n00b.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19 dirpath = _from_osx_fs(dirpath)
546
6ef89b31ddda internal: Fix a severe bug with the file-system wrappers on OSX.
Ludovic Chabant <ludovic@chabant.com>
parents: 529
diff changeset
20 dirnames[:] = list(map(_from_osx_fs, dirnames))
6ef89b31ddda internal: Fix a severe bug with the file-system wrappers on OSX.
Ludovic Chabant <ludovic@chabant.com>
parents: 529
diff changeset
21 filenames[:] = list(map(_from_osx_fs, filenames))
526
9b8b47fb1068 bug: Forgot to add a new file like a big n00b.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 yield dirpath, dirnames, filenames
9b8b47fb1068 bug: Forgot to add a new file like a big n00b.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23
527
fa9eb8f866cd bug: Fix file-system wrappers for non-Mac systems.
Ludovic Chabant <ludovic@chabant.com>
parents: 526
diff changeset
24 def _listdir(path='.'):
526
9b8b47fb1068 bug: Forgot to add a new file like a big n00b.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25 for name in os.listdir(path):
9b8b47fb1068 bug: Forgot to add a new file like a big n00b.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 name = _from_osx_fs(name)
9b8b47fb1068 bug: Forgot to add a new file like a big n00b.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 yield name
9b8b47fb1068 bug: Forgot to add a new file like a big n00b.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28
527
fa9eb8f866cd bug: Fix file-system wrappers for non-Mac systems.
Ludovic Chabant <ludovic@chabant.com>
parents: 526
diff changeset
29 def _glob(pathname):
526
9b8b47fb1068 bug: Forgot to add a new file like a big n00b.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 pathname = _to_osx_fs(pathname)
529
6f1f45fb7790 cm: Re-fix Mac file-system wrappers.
Ludovic Chabant <ludovic@chabant.com>
parents: 527
diff changeset
31 matches = _system_glob.glob(pathname)
526
9b8b47fb1068 bug: Forgot to add a new file like a big n00b.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 return list(map(_from_osx_fs, matches))
9b8b47fb1068 bug: Forgot to add a new file like a big n00b.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33
9b8b47fb1068 bug: Forgot to add a new file like a big n00b.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 def _from_osx_fs(s):
9b8b47fb1068 bug: Forgot to add a new file like a big n00b.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35 return unicodedata.normalize('NFC', s)
9b8b47fb1068 bug: Forgot to add a new file like a big n00b.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36
9b8b47fb1068 bug: Forgot to add a new file like a big n00b.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37 def _to_osx_fs(s):
9b8b47fb1068 bug: Forgot to add a new file like a big n00b.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 return unicodedata.ucd_3_2_0.normalize('NFD', s)
9b8b47fb1068 bug: Forgot to add a new file like a big n00b.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39
527
fa9eb8f866cd bug: Fix file-system wrappers for non-Mac systems.
Ludovic Chabant <ludovic@chabant.com>
parents: 526
diff changeset
40 walk = _walk
fa9eb8f866cd bug: Fix file-system wrappers for non-Mac systems.
Ludovic Chabant <ludovic@chabant.com>
parents: 526
diff changeset
41 listdir = _listdir
fa9eb8f866cd bug: Fix file-system wrappers for non-Mac systems.
Ludovic Chabant <ludovic@chabant.com>
parents: 526
diff changeset
42 glob = _glob
526
9b8b47fb1068 bug: Forgot to add a new file like a big n00b.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
43
638
a4ac464a45b3 internal: Remove SyntaxWarning from MacOS wrappers.
Ludovic Chabant <ludovic@chabant.com>
parents: 546
diff changeset
44
1094
ea6cbd6d2af5 internal: Disable macOS file-system wrappers.
Ludovic Chabant <ludovic@chabant.com>
parents: 638
diff changeset
45 _do_wrap_mac_fs = False
ea6cbd6d2af5 internal: Disable macOS file-system wrappers.
Ludovic Chabant <ludovic@chabant.com>
parents: 638
diff changeset
46
ea6cbd6d2af5 internal: Disable macOS file-system wrappers.
Ludovic Chabant <ludovic@chabant.com>
parents: 638
diff changeset
47 if _do_wrap_mac_fs and sys.platform == 'darwin':
638
a4ac464a45b3 internal: Remove SyntaxWarning from MacOS wrappers.
Ludovic Chabant <ludovic@chabant.com>
parents: 546
diff changeset
48 _wrap_fs_funcs()
a4ac464a45b3 internal: Remove SyntaxWarning from MacOS wrappers.
Ludovic Chabant <ludovic@chabant.com>
parents: 546
diff changeset
49