OLD | NEW |
---|---|
(Empty) | |
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 | |
3 # found in the LICENSE file. | |
4 | |
5 from HTMLParser import HTMLParser | |
6 import mimetypes | |
7 import logging | |
8 import os | |
9 | |
10 from compiled_file_system import SingleFile | |
11 from directory_zipper import DirectoryZipper | |
12 from file_system import ToUnicode | |
13 from future import Gettable, Future | |
14 from third_party.handlebar import Handlebar | |
15 | |
16 | |
17 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.
| |
18 '''Return value from ContentProvider.GetContentInfo. | |
19 ''' | |
20 | |
21 def __init__(self, content, content_type): | |
22 self.content = content | |
23 self.content_type = content_type | |
24 | |
25 | |
26 class ContentProvider(object): | |
27 '''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.
| |
28 for images, and so on. | |
29 ''' | |
30 | |
31 def __init__(self, | |
32 name, | |
33 compiled_fs_factory, | |
34 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
| |
35 host_file_system, | |
36 supports_templates=False, | |
37 supports_zip=False): | |
38 # Public. | |
39 self.name = name | |
40 self.file_system = file_system | |
41 # Private. | |
42 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.
| |
43 self._content_cache = compiled_fs_factory.Create(file_system, | |
44 self._CompileContent, | |
45 ContentProvider) | |
46 self._supports_templates = supports_templates | |
47 if supports_zip: | |
48 self._directory_zipper = DirectoryZipper(compiled_fs_factory, file_system) | |
49 else: | |
50 self._directory_zipper = None | |
51 | |
52 @SingleFile | |
53 def _CompileContent(self, path, text): | |
54 assert text is not None, path | |
55 mimetype = mimetypes.guess_type(path)[0] | |
56 if mimetype is None: | |
57 content = text | |
58 mimetype = 'text/plain' | |
59 elif mimetype == 'text/html': | |
60 content = ToUnicode(text) | |
61 if self._supports_templates: | |
62 content = Handlebar(content) | |
63 elif (mimetype.startswith('text/') or | |
64 mimetype in ('application/javascript', 'application/json')): | |
65 content = ToUnicode(text) | |
66 else: | |
67 content = text | |
68 return ContentInfo(content, mimetype) | |
69 | |
70 def GetContentInfo(self, host, path): | |
71 path = path.lstrip('/') | |
72 base, ext = os.path.splitext(path) | |
73 | |
74 # Check for a zip file first, if zip is enabled. | |
75 if self._directory_zipper and ext == '.zip': | |
76 zip_future = self._directory_zipper.Zip(base) | |
77 return Future(delegate=Gettable( | |
78 lambda: ContentInfo(zip_future.Get(), 'application/zip'))) | |
79 | |
80 return self._content_cache.GetFromFile(path, binary=True) | |
81 | |
82 def Cron(self): | |
83 # TODO(kalman): Implement. | |
84 pass | |
OLD | NEW |