Mercurial > piecrust2
annotate piecrust/osutil.py @ 661:2f780b191541
internal: Fix a bug with registering taxonomy terms that are not strings.
Some objects, like the blog data provider's taxnonomy entries, can render as
strings, but are objects themselves. When registering them as "used terms", we
need to use their string representation.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Tue, 01 Mar 2016 22:26:09 -0800 |
parents | a4ac464a45b3 |
children | ea6cbd6d2af5 |
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 |
a4ac464a45b3
internal: Remove SyntaxWarning from MacOS wrappers.
Ludovic Chabant <ludovic@chabant.com>
parents:
546
diff
changeset
|
45 if sys.platform == 'darwin': |
a4ac464a45b3
internal: Remove SyntaxWarning from MacOS wrappers.
Ludovic Chabant <ludovic@chabant.com>
parents:
546
diff
changeset
|
46 _wrap_fs_funcs() |
a4ac464a45b3
internal: Remove SyntaxWarning from MacOS wrappers.
Ludovic Chabant <ludovic@chabant.com>
parents:
546
diff
changeset
|
47 |