summaryrefslogtreecommitdiffstats
path: root/chrome/common/extensions/docs/server2/template_data_source.py
blob: b1064538380af689d5fcb0b258b692975e542b94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Copyright (c) 2012 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.

import logging
import posixpath
import traceback

from data_source import DataSource
from docs_server_utils import FormatKey
from extensions_paths import (
    ARTICLES_TEMPLATES, INTROS_TEMPLATES, PRIVATE_TEMPLATES)
from file_system import FileNotFoundError
from future import Collect
from path_util import AssertIsDirectory


class TemplateDataSource(DataSource):
  '''Provides a DataSource interface for compiled templates.
  '''
  def __init__(self, server_instance, request=None):
    self._dir = type(self)._BASE
    AssertIsDirectory(self._dir)
    self._request = request
    self._template_cache = server_instance.compiled_fs_factory.ForTemplates(
        server_instance.host_file_system_provider.GetTrunk())
    self._file_system = server_instance.host_file_system_provider.GetTrunk()

  def get(self, path):
    try:
      return self._template_cache.GetFromFile('%s%s' %
          (self._dir, FormatKey(path))).Get()
    except FileNotFoundError:
      logging.warning(traceback.format_exc())
      return None

  def Cron(self):
    futures = []
    for root, _, files in self._file_system.Walk(self._dir):
      futures += [self._template_cache.GetFromFile(
                      posixpath.join(self._dir, root, FormatKey(f)))
                  for f in files
                  if posixpath.splitext(f)[1] == '.html']
    return Collect(futures)


class ArticleDataSource(TemplateDataSource):
  '''Serves templates for Articles.
  '''
  _BASE = ARTICLES_TEMPLATES


class IntroDataSource(TemplateDataSource):
  '''Serves templates for Intros.
  '''
  _BASE = INTROS_TEMPLATES


class PartialDataSource(TemplateDataSource):
  '''Serves templates for private templates.
  '''
  _BASE = PRIVATE_TEMPLATES