OLD | NEW |
---|---|
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import logging | |
5 import mimetypes | 6 import mimetypes |
6 import posixpath | 7 import posixpath |
7 import traceback | 8 import traceback |
8 | 9 |
9 from compiled_file_system import SingleFile | 10 from compiled_file_system import SingleFile |
10 from directory_zipper import DirectoryZipper | 11 from directory_zipper import DirectoryZipper |
11 from docs_server_utils import ToUnicode | 12 from docs_server_utils import ToUnicode |
12 from file_system import FileNotFoundError | 13 from file_system import FileNotFoundError |
13 from future import All, Future | 14 from future import All, Future |
14 from path_canonicalizer import PathCanonicalizer | 15 from path_canonicalizer import PathCanonicalizer |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
73 self._default_extensions = default_extensions | 74 self._default_extensions = default_extensions |
74 self._supports_templates = supports_templates | 75 self._supports_templates = supports_templates |
75 if supports_zip: | 76 if supports_zip: |
76 self._directory_zipper = DirectoryZipper(compiled_fs_factory, file_system) | 77 self._directory_zipper = DirectoryZipper(compiled_fs_factory, file_system) |
77 else: | 78 else: |
78 self._directory_zipper = None | 79 self._directory_zipper = None |
79 | 80 |
80 @SingleFile | 81 @SingleFile |
81 def _CompileContent(self, path, text): | 82 def _CompileContent(self, path, text): |
82 assert text is not None, path | 83 assert text is not None, path |
83 _, ext = posixpath.splitext(path) | 84 try: |
Ken Rockot(use gerrit already)
2015/05/26 00:26:23
No logic changes here, just wrapping in a try bloc
| |
84 mimetype = _MIMETYPE_OVERRIDES.get(ext, mimetypes.guess_type(path)[0]) | 85 _, ext = posixpath.splitext(path) |
85 if ext == '.md': | 86 mimetype = _MIMETYPE_OVERRIDES.get(ext, mimetypes.guess_type(path)[0]) |
86 # See http://pythonhosted.org/Markdown/extensions | 87 if ext == '.md': |
87 # for details on "extensions=". | 88 # See http://pythonhosted.org/Markdown/extensions |
88 content = markdown(ToUnicode(text), | 89 # for details on "extensions=". |
89 extensions=('extra', 'headerid', 'sane_lists')) | 90 content = markdown(ToUnicode(text), |
90 if self._supports_templates: | 91 extensions=('extra', 'headerid', 'sane_lists')) |
91 content = Motemplate(content, name=path) | 92 mimetype = 'text/html' |
92 mimetype = 'text/html' | 93 if self._supports_templates: |
93 elif mimetype is None: | 94 content = Motemplate(content, name=path) |
94 content = text | 95 elif mimetype is None: |
95 mimetype = 'text/plain' | 96 content = text |
96 elif mimetype == 'text/html': | 97 mimetype = 'text/plain' |
97 content = ToUnicode(text) | 98 elif mimetype == 'text/html': |
98 if self._supports_templates: | 99 content = ToUnicode(text) |
99 content = Motemplate(content, name=path) | 100 if self._supports_templates: |
100 elif (mimetype.startswith('text/') or | 101 content = Motemplate(content, name=path) |
101 mimetype in ('application/javascript', 'application/json')): | 102 elif (mimetype.startswith('text/') or |
102 content = ToUnicode(text) | 103 mimetype in ('application/javascript', 'application/json')): |
103 else: | 104 content = ToUnicode(text) |
104 content = text | 105 else: |
105 return ContentAndType(content, | 106 content = text |
106 mimetype, | 107 return ContentAndType(content, |
107 self.file_system.Stat(path).version) | 108 mimetype, |
109 self.file_system.Stat(path).version) | |
110 except Exception as e: | |
111 logging.warn('In file %s: %s' % (path, e.message)) | |
112 return ContentAndType('', mimetype, self.file_system.Stat(path).version) | |
108 | 113 |
109 def GetCanonicalPath(self, path): | 114 def GetCanonicalPath(self, path): |
110 '''Gets the canonical location of |path|. This class is tolerant of | 115 '''Gets the canonical location of |path|. This class is tolerant of |
111 spelling errors and missing files that are in other directories, and this | 116 spelling errors and missing files that are in other directories, and this |
112 returns the correct/canonical path for those. | 117 returns the correct/canonical path for those. |
113 | 118 |
114 For example, the canonical path of "browseraction" is probably | 119 For example, the canonical path of "browseraction" is probably |
115 "extensions/browserAction.html". | 120 "extensions/browserAction.html". |
116 | 121 |
117 Note that the canonical path is relative to this content provider i.e. | 122 Note that the canonical path is relative to this content provider i.e. |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
202 futures.append(self.GetContentAndType(Join(root, f))) | 207 futures.append(self.GetContentAndType(Join(root, f))) |
203 # Also cache the extension-less version of the file if needed. | 208 # Also cache the extension-less version of the file if needed. |
204 base, ext = posixpath.splitext(f) | 209 base, ext = posixpath.splitext(f) |
205 if f != SITE_VERIFICATION_FILE and ext in self._default_extensions: | 210 if f != SITE_VERIFICATION_FILE and ext in self._default_extensions: |
206 futures.append(self.GetContentAndType(Join(root, base))) | 211 futures.append(self.GetContentAndType(Join(root, base))) |
207 # TODO(kalman): Cache .zip files for each directory (if supported). | 212 # TODO(kalman): Cache .zip files for each directory (if supported). |
208 return All(futures, except_pass=Exception, except_pass_log=True) | 213 return All(futures, except_pass=Exception, except_pass_log=True) |
209 | 214 |
210 def __repr__(self): | 215 def __repr__(self): |
211 return 'ContentProvider of <%s>' % repr(self.file_system) | 216 return 'ContentProvider of <%s>' % repr(self.file_system) |
OLD | NEW |