def getPage(requestPath, lang):
    response = None
    fileLocations = [
        os.path.join(SOURCE_PATH, lang, requestPath, '_index.yaml'),
        os.path.join(SOURCE_PATH, 'en', requestPath, '_index.yaml'),
        os.path.join(SOURCE_PATH, lang, requestPath, 'index.md'),
        os.path.join(SOURCE_PATH, 'en', requestPath, 'index.md')
    ]
    for fileLocation in fileLocations:
        if os.path.isfile(fileLocation):
            fileContent = open(fileLocation, 'r').read()
            fileContent = fileContent.decode('utf8')

            if fileLocation.endswith('_index.yaml'):
                response = generateYaml(lang, requestPath, fileContent)
                break

            if fileLocation.endswith('index.md'):
                # index.md are treated like other devsite pages, so just use the
                # devsitePage rendering template.
                requestPath = os.path.join(requestPath, 'index')
                response = devsitePage.getPage(requestPath, lang)
                break

    if response is None and os.environ['SERVER_SOFTWARE'].startswith('Dev'):
        if (requestPath.startswith('showcase/')
                or requestPath.startswith('shows/')
                or requestPath.startswith('updates/')):
            content = ''
            content += '<h1>Generated Listing Page</h1>\n'
            content += '<aside class="warning" markdown="1"><strong>Oops</strong> '
            content += '<span>Looks like you forgot to build the index files. Try '
            content += 'running <code>gulp build</code> from the command line.'
            content += '</span></aside>'
            fileList = os.listdir(os.path.join(SOURCE_PATH, 'en', requestPath))
            for f in fileList:
                if not f.startswith('_'):
                    p = '/web/' + requestPath + f.replace('.md', '')
                    line = '<li>'
                    line += '<a href="' + p + '">' + p + '</a>'
                    line += '</li>'
                    content += line

            response = render(
                'gae/article.tpl', {
                    'title': 'Whoops! Missing index page!',
                    'requestPath': requestPath,
                    'leftNav': '',
                    'content': content,
                    'toc': '',
                    'dateUpdated': 'now',
                    'lang': 'en'
                })
    return response
def getPage(requestPath, lang):
  response = None
  fileLocations = [
    os.path.join(SOURCE_PATH, lang, requestPath, '_index.yaml'),
    os.path.join(SOURCE_PATH, 'en', requestPath, '_index.yaml'),
    os.path.join(SOURCE_PATH, lang, requestPath, 'index.md'),
    os.path.join(SOURCE_PATH, 'en', requestPath, 'index.md')
  ]
  for fileLocation in fileLocations:
    if os.path.isfile(fileLocation):
      fileContent = open(fileLocation, 'r').read()
      fileContent = fileContent.decode('utf8')

      if fileLocation.endswith('_index.yaml'):
        response = generateYaml(lang, requestPath, fileContent)
        break

      if fileLocation.endswith('index.md'):
        # index.md are treated like other devsite pages, so just use the
        # devsitePage rendering template.
        requestPath = os.path.join(requestPath, 'index')
        response = devsitePage.getPage(requestPath, lang)
        break

  if response is None and os.environ['SERVER_SOFTWARE'].startswith('Dev'):
    if (requestPath.startswith('showcase/') or
        requestPath.startswith('shows/') or requestPath.startswith('updates/')):
      content = ''
      content += '<h1>Generated Listing Page</h1>\n'
      content += '<aside class="warning" markdown="1"><strong>Oops</strong> '
      content += '<span>Looks like you forgot to build the index files. Try '
      content += 'running <code>gulp build</code> from the command line.'
      content += '</span></aside>'
      fileList = os.listdir(os.path.join(SOURCE_PATH, 'en', requestPath))
      for f in fileList:
        if not f.startswith('_'):
          p = '/web/' + requestPath + f.replace('.md', '')
          line = '<li>'
          line += '<a href="' + p + '">' + p + '</a>'
          line += '</li>'
          content += line

      response = render('gae/article.tpl', {
        'title': 'Whoops! Missing index page!',
        'requestPath': requestPath,
        'leftNav': '',
        'content': content,
        'toc': '',
        'dateUpdated': 'now',
        'lang': 'en'}
      )
  return response
示例#3
0
    def get(self, path):
        response = None
        langQS = self.request.get('hl', None)
        langCookie = self.request.cookies.get('hl')
        if langQS:
            lang = langQS
        elif langCookie:
            lang = langCookie
        else:
            lang = DEFAULT_LANG
        self.response.set_cookie('hl', lang, max_age=3600, path='/')

        fullPath = self.request.path
        memcacheKey = fullPath + '?hl=' + lang
        logging.info('GET ' + memcacheKey)

        if USE_MEMCACHE:
            response = memcache.get(memcacheKey)
            if response:
                logging.info('304 ' + fullPath)

        if response is None:
            try:
                if path.endswith('/') or path == '':
                    response = devsiteIndex.getPage(path, lang)
                else:
                    response = devsitePage.getPage(path, lang)

                if response is None:
                    # No file found, check for redirect
                    redirectTo = devsiteHelper.checkForRedirect(
                        fullPath, lang, USE_MEMCACHE)
                    if redirectTo:
                        logging.info('301 ' + redirectTo)
                        self.redirect(redirectTo, permanent=True)
                        return

                    # No redirect found, send the 404 page.
                    response = render('gae/404.tpl', {'requestPath': fullPath})
                    logging.error('404 ' + fullPath)
                    self.response.set_status(404)
                else:
                    logging.info('200 ' + fullPath)
                    if USE_MEMCACHE:
                        memcache.set(memcacheKey, response)
            except Exception as ex:
                context = {'content': ex, 'requestPath': fullPath}
                response = render('gae/500.tpl', context)
                logging.exception('500 ' + fullPath)
                self.response.set_status(500)

        self.response.out.write(response)
    def get(self, path):
        response = None
        langQS = self.request.get('hl', None)
        langCookie = self.request.cookies.get('hl')
        if langQS:
          lang = langQS
        elif langCookie:
          lang = langCookie
        else:
          lang = DEFAULT_LANG
        self.response.set_cookie('hl', lang, max_age=3600, path='/')

        fullPath = self.request.path
        memcacheKey = fullPath + '?hl=' + lang
        logging.info('GET ' + memcacheKey)

        if USE_MEMCACHE:
          response = memcache.get(memcacheKey)
          if response:
            logging.info('304 ' + fullPath)

        if response is None:
          try:
            if path.endswith('/') or path == '':
              response = devsiteIndex.getPage(path, lang)
            else:
              response = devsitePage.getPage(path, lang)
          
            if response is None:
              # No file found, check for redirect
              redirectTo = devsiteHelper.checkForRedirect(fullPath, lang, USE_MEMCACHE)
              if redirectTo:
                logging.info('301 ' + redirectTo)
                self.redirect(redirectTo, permanent=True)
                return

              # No redirect found, send the 404 page.
              response = render('gae/404.tpl', {'requestPath': fullPath})
              logging.error('404 ' + fullPath)
              self.response.set_status(404)
            else:
              logging.info('200 ' + fullPath)
              if USE_MEMCACHE:
                memcache.set(memcacheKey, response)
          except Exception as ex:
            context = {'content': ex, 'requestPath': fullPath}
            response = render('gae/500.tpl', context)
            logging.exception('500 ' + fullPath)
            self.response.set_status(500)

        self.response.out.write(response)
    def get(self, path):

        self.response.headers.add('x-frame-options', 'SAMEORIGIN')

        if path.endswith('.html') or path.endswith('.md'):
          redirectTo = '/web/' + os.path.splitext(path)[0]
          self.redirect(redirectTo, permanent=True)
          return

        response = None
        langQS = self.request.get('hl', None)
        langCookie = self.request.cookies.get('hl')
        if langQS:
          lang = langQS
        elif langCookie:
          lang = langCookie
        else:
          lang = DEFAULT_LANG
        self.response.set_cookie('hl', lang, max_age=3600, path='/')

        fullPath = self.request.path
        memcacheKey = self.request.host + fullPath + '?hl=' + lang
        logging.info('GET ' + memcacheKey)

        if USE_MEMCACHE:
          response = memcache.get(memcacheKey)
          if response:
            logging.info('304 ' + fullPath)

        if response is None:
          try:
            if os.path.isdir(os.path.join(SOURCE_PATH, 'en', path)):
              # Make sure the directory ends with a /, as required by devsite
              if len(path) > 0 and not path.endswith('/'):
                redirectTo = '/web/' +  path + '/'
                logging.info('301 ' + redirectTo)
                self.redirect(redirectTo, permanent=True)
                return
              response = devsiteIndex.getPage(path, lang)
              if (response is None) and (path.startswith('showcase') or
                  path.startswith('shows') or path.startswith('updates')):
                response = devsiteIndex.getDirIndex(path)
            else:
              response = devsitePage.getPage(path, lang)

            if response is None:
              # No file found, check for redirect
              redirectTo = devsiteHelper.checkForRedirect(fullPath, lang, USE_MEMCACHE)
              if redirectTo:
                logging.info('301 ' + redirectTo)
                self.redirect(redirectTo, permanent=True)
                return

              # No redirect found, send the 404 page.
              response = render('gae/404.tpl', {'requestPath': fullPath})
              logging.error('404 ' + fullPath)
              self.response.set_status(404)
            else:
              logging.info('200 ' + fullPath)
              if USE_MEMCACHE:
                memcache.set(memcacheKey, response)
          except Exception as ex:
            context = {'content': ex, 'requestPath': fullPath}
            response = render('gae/500.tpl', context)
            logging.exception('500 ' + fullPath)
            self.response.set_status(500)

        self.response.out.write(response)
    first_forward = source_file.index('/')
    lang = source_file[0:first_forward]
    kind = 'md'
    if source_file.endswith('_index.yaml'):
        kind = 'yaml'
    source_file = source_file[first_forward + 1:]
    source_file = source_file[0:source_file.rindex('.')]
    dest_file = os.path.join(build_dir, lang, source_file + '.html')

    response = None
    if (kind == 'yaml'):
        response = devsiteIndex.getPage(source_file.replace('_index', ''),
                                        lang)
        dest_file = dest_file.replace('_index', 'index')
    else:
        response = devsitePage.getPage(source_file, lang)

    if response:
        try:
            dest_dir = os.path.dirname(dest_file)
            if not os.path.exists(dest_dir):
                os.makedirs(dest_dir)
            with io.open(dest_file, 'w', encoding='utf8') as rendered_file:
                rendered_file.write(response)
                rendered_file.close()
            msg = 'OK ' + dest_file + ' (' + str(file_count_total) + ' of '
            msg += str(file_count_expected) + ')'
            print(msg)
            sys.stdout.flush()
            file_count_good += 1
        except:
示例#7
0
    def get(self, path):

        self.response.headers.add('x-frame-options', 'SAMEORIGIN')

        if path.endswith('.html') or path.endswith('.md'):
          redirectTo = '/web/' + os.path.splitext(path)[0]
          self.redirect(redirectTo, permanent=True)
          return

        response = None
        langQS = self.request.get('hl', None)
        langCookie = self.request.cookies.get('hl')
        if langQS:
          lang = langQS
        elif langCookie:
          lang = langCookie
        else:
          lang = DEFAULT_LANG
        self.response.set_cookie('hl', lang, max_age=3600, path='/')

        fullPath = self.request.path
        memcacheKey = self.request.host + fullPath + '?hl=' + lang
        logging.info('GET ' + memcacheKey)

        if USE_MEMCACHE:
          response = memcache.get(memcacheKey)
          if response:
            logging.info('304 ' + fullPath)

        if response is None:
          try:
            if os.path.isdir(os.path.join(SOURCE_PATH, 'en', path)):
              # Make sure the directory ends with a /, as required by devsite
              if len(path) > 0 and not path.endswith('/'):
                redirectTo = '/web/' +  path + '/'
                logging.info('301 ' + redirectTo)
                self.redirect(redirectTo, permanent=True)
                return
              response = devsiteIndex.getPage(path, lang)
              if (response is None) and (path.startswith('showcase') or
                  path.startswith('shows') or path.startswith('updates')):
                response = devsiteIndex.getDirIndex(path)
            else:
              response = devsitePage.getPage(path, lang)

            if response is None:
              # No file found, check for redirect
              redirectTo = devsiteHelper.checkForRedirect(fullPath, lang, USE_MEMCACHE)
              if redirectTo:
                logging.info('301 ' + redirectTo)
                self.redirect(redirectTo, permanent=True)
                return

              # No redirect found, send the 404 page.
              response = render('gae/404.tpl', {'requestPath': fullPath})
              logging.error('404 ' + fullPath)
              self.response.set_status(404)
            else:
              logging.info('200 ' + fullPath)
              if USE_MEMCACHE:
                memcache.set(memcacheKey, response)
          except Exception as ex:
            context = {'content': ex, 'requestPath': fullPath}
            response = render('gae/500.tpl', context)
            logging.exception('500 ' + fullPath)
            self.response.set_status(500)

        self.response.out.write(response)
  file_count_total += 1
  first_forward = source_file.index('/')
  lang = source_file[0:first_forward]
  kind = 'md'
  if source_file.endswith('_index.yaml'):
    kind = 'yaml'
  source_file = source_file[first_forward + 1:]
  source_file = source_file[0:source_file.rindex('.')]
  dest_file = os.path.join(build_dir, lang, source_file + '.html')

  response = None
  if (kind == 'yaml'):
    response = devsiteIndex.getPage(source_file.replace('_index', ''), lang)
    dest_file = dest_file.replace('_index', 'index')
  else:
    response = devsitePage.getPage(source_file, lang)

  if response:
    try:
      dest_dir = os.path.dirname(dest_file)
      if not os.path.exists(dest_dir):
        os.makedirs(dest_dir)
      with io.open(dest_file, 'w', encoding='utf8') as rendered_file:
        rendered_file.write(response)
        rendered_file.close()
      msg = 'OK ' + dest_file + ' (' + str(file_count_total) + ' of '
      msg += str(file_count_expected) + ')'
      print(msg)
      sys.stdout.flush()
      file_count_good += 1
    except: