Skip to content

Commit

Permalink
Adds support for DevSite HTML (google#5071)
Browse files Browse the repository at this point in the history
  • Loading branch information
petele authored Sep 27, 2017
1 parent 8d41c04 commit fed0ba7
Show file tree
Hide file tree
Showing 9 changed files with 494 additions and 320 deletions.
19 changes: 16 additions & 3 deletions appengine_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import os
import webapp2
import logging
import traceback
import devsitePage
import devsiteIndex
import devsiteHelper
Expand All @@ -28,20 +27,24 @@
DEFAULT_LANG = 'en'
DEVENV = os.environ['SERVER_SOFTWARE'].startswith('Dev')
USE_MEMCACHE = not DEVENV
SOURCE_PATH = os.path.join(os.path.dirname(__file__), 'src/content/')

class FlushMemCache(webapp2.RequestHandler):
def get(self):
memcache.flush_all()
self.response.out.write('Flushed')


class HomePage(webapp2.RequestHandler):
def get(self):
self.redirect('/web/', permanent=True)


class DevSiteRedirect(webapp2.RequestHandler):
def get(self, path):
self.redirect('https://developers.google.com/' + path, permanent=True)


class Framebox(webapp2.RequestHandler):
def get(self, path):
response = None
Expand All @@ -58,8 +61,15 @@ def get(self, path):
logging.info('200 ' + memcacheKey)
self.response.out.write(response)


class DevSitePages(webapp2.RequestHandler):
def get(self, path):

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')
Expand All @@ -82,8 +92,11 @@ def get(self, path):

if response is None:
try:
if path.endswith('/') or path == '':
if os.path.isdir(os.path.join(SOURCE_PATH, 'en', path)):
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)

Expand All @@ -108,7 +121,7 @@ def get(self, path):
response = render('gae/500.tpl', context)
logging.exception('500 ' + fullPath)
self.response.set_status(500)

self.response.out.write(response)


Expand Down
49 changes: 8 additions & 41 deletions devsiteHelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,10 @@
import logging
import textwrap
import urllib2

from datetime import date, datetime
from google.appengine.api import memcache
from google.appengine.ext.webapp.template import render

SOURCE_PATH = os.path.join(os.path.dirname(__file__), 'src/content/')
DEVENV = os.environ['SERVER_SOFTWARE'].startswith('Dev')
USE_MEMCACHE = not DEVENV


def slugify(str):
# Very simply slugify
Expand All @@ -34,8 +29,7 @@ def getFromMemCache(memcacheKey):

def setMemCache(memcacheKey, value, length=3600):
try:
if USE_MEMCACHE:
memcache.set(memcacheKey, value, length)
memcache.set(memcacheKey, value, length)
except Exception as e:
logging.exception('Unable to cache to MemCache')

Expand Down Expand Up @@ -149,18 +143,13 @@ def parseBookYaml(pathToBook, lang='en'):
Returns:
A dictionary with the parsed book.
"""
memcacheKey = 'bookYAML-' + pathToBook
result = getFromMemCache(memcacheKey)
if result:
return result
try:
result = {}
upperTabs = []
result['upper_tabs'] = upperTabs
bookYaml = yaml.load(readFile(pathToBook, lang))
for upperTab in bookYaml['upper_tabs']:
upperTabs.append(expandBook(upperTab))
setMemCache(memcacheKey, result, 60)
return result
except Exception as e:
logging.exception('Error in parseBookYaml')
Expand Down Expand Up @@ -219,13 +208,8 @@ def getLowerTabs(bookYaml):


def getLeftNav(requestPath, bookYaml, lang='en'):
# Returns the left nav. If it's already been generated and stored in
# memcache, return that, otherwise, read the file then recursively
# build the tree using buildLeftNav.
memcacheKey = 'leftNav-' + requestPath
result = getFromMemCache(memcacheKey)
if result:
return result
# Returns the left nav. Read the file then recursively, then build the
# tree using buildLeftNav.
whoops = '<h2>Whoops!</h2>'
whoops += '<p>An error occured while trying to parse and build the'
whoops += ' left hand navigation. Check the error logs.'
Expand All @@ -241,10 +225,9 @@ def getLeftNav(requestPath, bookYaml, lang='en'):
result = '<ul class="devsite-nav-list devsite-nav-expandable">\n'
result += buildLeftNav(lowerTab['contents'])
result += '</ul>\n'
setMemCache(memcacheKey, result)
return result
except Exception as e:
msg = ' - Unable to read or parse primary book.yaml: ' + pathToBook
msg = ' - Unable to read or parse primary book.yaml'
logging.exception(msg)
whoops += '<p>Exception occured.</p>'
return whoops
Expand All @@ -254,18 +237,6 @@ def buildLeftNav(bookYaml, lang='en'):
# Recursively reads the book.yaml file and generates the navigation tree
result = ''
for item in bookYaml:
if 'include' in item:
## TODO(petele): Remove this
## leaving this in for a few weeks while I ensure it doesn't break
## anything.
logging.error('***** INCLUDE - this should NOT happen.')
# try:
# include = readFile(item['include'], lang)
# include = yaml.load(include)
# result += buildLeftNav(include['toc'])
# except Exception as e:
# msg = ' - Unable to parsing embedded toc file: ' + item['include']
# logging.exception(msg)
if 'path' in item:
result += '<li class="devsite-nav-item">\n'
result += '<a href="' + item['path'] + '" class="devsite-nav-title">\n'
Expand Down Expand Up @@ -442,10 +413,7 @@ def getIncludeCode(include_tag, lang='en'):


def getAnnouncementBanner(pathToProject, lang='en'):
memcacheKey = 'projectYAML-' + pathToProject
result = getFromMemCache(memcacheKey)
if result:
return result
result = ''
try:
project = yaml.load(readFile(pathToProject, lang))
if 'announcement' in project:
Expand All @@ -461,10 +429,9 @@ def getAnnouncementBanner(pathToProject, lang='en'):
result += '</div>'
else:
logging.warn('Announcement in _project.yaml expired: not shown')
setMemCache(memcacheKey, result, 60)
except Exception as e:
logging.exception('Unable to get announcement from project.yaml')
return ''
result = ''
return result


Expand Down Expand Up @@ -497,7 +464,7 @@ def getFooterPromo(lang='en'):
result += '</a><div class="devsite-footer-promo-description">'
result += promo['description']
result += '</div></li>\n'
setMemCache(memcacheKey, result)
setMemCache(memcacheKey, result, 60)
return result


Expand Down Expand Up @@ -527,5 +494,5 @@ def getFooterLinkBox(lang='en'):
result += linkItem['label'] + '</a></li>'
result += '</ul>'
result += '</li>'
setMemCache(memcacheKey, result)
setMemCache(memcacheKey, result, 60)
return result
Loading

0 comments on commit fed0ba7

Please sign in to comment.