Ejemplo n.º 1
0
    def _GetSamplesJSON(self, request, key, update=False):
        templates = self.template_data_source_factory.Create(
            request, key + '/samples.html')
        if key == 'apps' or key == 'extensions':
            cache_url = None
            if key == 'apps':
                cache_url = '/trunk/' + key
            else:
                cache_url = '/' + templates._branch_info['current'] + '/' + key
            logging.info('Serving API request %s' % cache_url)

            data = ResponseCache.Get(cache_url)
            age = ResponseCache.GetAge(cache_url)
            need_update = (age > datetime.timedelta(1))
            logging.info('Cache age: %s' % str(age))
            if data is not None and not (update == True
                                         and need_update == True):
                return data
            else:
                content = []
                try:
                    logging.info('Trying to generate samples.json for %s' %
                                 cache_url)
                    content = templates._samples_data_source.GetAsJSON(key)
                except:
                    logging.getLogger('slave-samples-api').exception(
                        'Error generating samples!')
                    pass
                if len(content) > 0:
                    logging.info('samples.json saved for %s' % cache_url)
                    ResponseCache.Set(cache_url, content)
                return content
Ejemplo n.º 2
0
 def _HandleSamplesAPI(self, channel_name, real_path):
   if real_path.startswith(url_constants.SLAVE_SAMPLES_API_BASE_URL):
     key = real_path[len(url_constants.SLAVE_SAMPLES_API_BASE_URL):]
     if key == 'apps' or key == 'extensions':
       self.response.headers['content-type'] = 'text/plain'
       cache_url = None
       if key == 'apps':
         cache_url = '/trunk/' + key
       else:
         cache_url = '/' + channel_name + '/' + key
       logging.info('Serving API request %s' % cache_url)
       data = ResponseCache.Get(cache_url)
       if data is None:
         self.response.out.write('[]')
         logging.error('No samples data. Please regenerate it manually.')
       else:
         self.response.out.write(data)
     else:
       self.response.set_status(404)
   else:
     self.response.set_status(404)
Ejemplo n.º 3
0
    def _GetDocsHTML(self, request, path, update=False):
        templates = self.template_data_source_factory.Create(request, path)
        cache_url = '/' + templates._branch_info['current'] + '/' + path

        logging.info('Serving API request %s' % cache_url)
        data = ResponseCache.Get(cache_url)
        age = ResponseCache.GetAge(cache_url)
        need_update = (age > datetime.timedelta(1))
        logging.info('Cache age: %s' % str(age))
        if data is not None and not (update == True and need_update == True):
            return data
        else:
            content = ''
            try:
                logging.info('Trying to render %s' % cache_url)
                content = templates.Render(path)
            except:
                logging.getLogger('slave-docs-api').exception(
                    'Error rendering HTML!')
                pass
            if len(content) > 0:
                logging.info('HTML saved for %s' % cache_url)
                ResponseCache.Set(cache_url, content.encode('utf-8'))
            return content