Chromium Code Reviews| Index: chrome/common/extensions/docs/server2/content_provider.py |
| diff --git a/chrome/common/extensions/docs/server2/content_provider.py b/chrome/common/extensions/docs/server2/content_provider.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..965ffa080d8f5ce06191bb5efe53ecfca65ca0ec |
| --- /dev/null |
| +++ b/chrome/common/extensions/docs/server2/content_provider.py |
| @@ -0,0 +1,84 @@ |
| +# Copyright 2013 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +from HTMLParser import HTMLParser |
| +import mimetypes |
| +import logging |
| +import os |
| + |
| +from compiled_file_system import SingleFile |
| +from directory_zipper import DirectoryZipper |
| +from file_system import ToUnicode |
| +from future import Gettable, Future |
| +from third_party.handlebar import Handlebar |
| + |
| + |
| +class ContentInfo(object): |
|
Jeffrey Yasskin
2013/11/04 21:21:18
"TypedContent"? This isn't "Info" because it also
not at google - send to devlin
2013/11/04 23:34:49
Info can't include the content? I'd need to rename
Jeffrey Yasskin
2013/11/04 23:45:21
ContentAndType would be fine with me.
|
| + '''Return value from ContentProvider.GetContentInfo. |
| + ''' |
| + |
| + def __init__(self, content, content_type): |
| + self.content = content |
| + self.content_type = content_type |
| + |
| + |
| +class ContentProvider(object): |
| + '''Provides correctly typed content: templates for HTML, text for CSS, binary |
|
Jeffrey Yasskin
2013/11/04 21:21:18
"Provides" is too vague: through what interface do
not at google - send to devlin
2013/11/04 23:34:49
Done.
|
| + for images, and so on. |
| + ''' |
| + |
| + def __init__(self, |
| + name, |
| + compiled_fs_factory, |
| + file_system, |
|
Jeffrey Yasskin
2013/11/04 21:21:18
Document the difference between file_system and ho
not at google - send to devlin
2013/11/04 23:34:49
Good point. It's only used by the template cache t
|
| + host_file_system, |
| + supports_templates=False, |
| + supports_zip=False): |
| + # Public. |
| + self.name = name |
| + self.file_system = file_system |
| + # Private. |
| + self._template_cache = compiled_fs_factory.ForTemplates(host_file_system) |
|
Jeffrey Yasskin
2013/11/04 21:21:18
This isn't used.
not at google - send to devlin
2013/11/04 23:34:49
Done.
|
| + self._content_cache = compiled_fs_factory.Create(file_system, |
| + self._CompileContent, |
| + ContentProvider) |
| + self._supports_templates = supports_templates |
| + if supports_zip: |
| + self._directory_zipper = DirectoryZipper(compiled_fs_factory, file_system) |
| + else: |
| + self._directory_zipper = None |
| + |
| + @SingleFile |
| + def _CompileContent(self, path, text): |
| + assert text is not None, path |
| + mimetype = mimetypes.guess_type(path)[0] |
| + if mimetype is None: |
| + content = text |
| + mimetype = 'text/plain' |
| + elif mimetype == 'text/html': |
| + content = ToUnicode(text) |
| + if self._supports_templates: |
| + content = Handlebar(content) |
| + elif (mimetype.startswith('text/') or |
| + mimetype in ('application/javascript', 'application/json')): |
| + content = ToUnicode(text) |
| + else: |
| + content = text |
| + return ContentInfo(content, mimetype) |
| + |
| + def GetContentInfo(self, host, path): |
| + path = path.lstrip('/') |
| + base, ext = os.path.splitext(path) |
| + |
| + # Check for a zip file first, if zip is enabled. |
| + if self._directory_zipper and ext == '.zip': |
| + zip_future = self._directory_zipper.Zip(base) |
| + return Future(delegate=Gettable( |
| + lambda: ContentInfo(zip_future.Get(), 'application/zip'))) |
| + |
| + return self._content_cache.GetFromFile(path, binary=True) |
| + |
| + def Cron(self): |
| + # TODO(kalman): Implement. |
| + pass |