App Engine Python SDK version 1.9.2
[gae.git] / python / google / appengine / api / pagespeedinfo.py
blobe7cd2345b930c6a27b4d5c4b2bb0ca4019ef5b90
1 #!/usr/bin/env python
3 # Copyright 2007 Google Inc.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
21 """PageSpeed configuration tools.
23 Library for parsing pagespeed configuration data from app.yaml and working
24 with these in memory.
25 """
33 import google
35 from google.appengine.api import validation
36 from google.appengine.api import yaml_builder
37 from google.appengine.api import yaml_listener
38 from google.appengine.api import yaml_object
40 _URL_BLACKLIST_REGEX = r'http(s)?://\S{0,499}'
41 _REWRITER_NAME_REGEX = r'[a-zA-Z0-9_]+'
42 _DOMAINS_TO_REWRITE_REGEX = r'(http(s)?://)?[-a-zA-Z0-9_.*]+(:\d+)?'
44 URL_BLACKLIST = 'url_blacklist'
45 ENABLED_REWRITERS = 'enabled_rewriters'
46 DISABLED_REWRITERS = 'disabled_rewriters'
47 DOMAINS_TO_REWRITE = 'domains_to_rewrite'
50 class MalformedPagespeedConfiguration(Exception):
51 """Configuration file for PageSpeed API is malformed."""
58 class PagespeedEntry(validation.Validated):
59 """Describes the format of a pagespeed configuration from a yaml file.
61 URL blacklist entries are patterns (with '?' and '*' as wildcards). Any URLs
62 that match a pattern on the blacklist will not be optimized by PageSpeed.
64 Rewriter names are strings (like 'CombineCss' or 'RemoveComments') describing
65 individual PageSpeed rewriters. A full list of valid rewriter names can be
66 found in the PageSpeed documentation.
68 The domains-to-rewrite list is a whitelist of domain name patterns with '*' as
69 a wildcard, optionally starting with 'http://' or 'https://'. If no protocol
70 is given, 'http://' is assumed. A resource will only be rewritten if it is on
71 the same domain as the HTML that references it, or if its domain is on the
72 domains-to-rewrite list.
73 """
74 ATTRIBUTES = {
75 URL_BLACKLIST: validation.Optional(
76 validation.Repeated(validation.Regex(_URL_BLACKLIST_REGEX))),
77 ENABLED_REWRITERS: validation.Optional(
78 validation.Repeated(validation.Regex(_REWRITER_NAME_REGEX))),
79 DISABLED_REWRITERS: validation.Optional(
80 validation.Repeated(validation.Regex(_REWRITER_NAME_REGEX))),
81 DOMAINS_TO_REWRITE: validation.Optional(
82 validation.Repeated(validation.Regex(_DOMAINS_TO_REWRITE_REGEX))),
86 def LoadPagespeedEntry(pagespeed_entry, open_fn=None):
87 """Load a yaml file or string and return a PagespeedEntry.
89 Args:
90 pagespeed_entry: The contents of a pagespeed entry from a yaml file
91 as a string, or an open file object.
92 open_fn: Function for opening files. Unused.
94 Returns:
95 A PagespeedEntry instance which represents the contents of the parsed yaml.
97 Raises:
98 yaml_errors.EventError: An error occured while parsing the yaml.
99 MalformedPagespeedConfiguration: The configuration is parseable but invalid.
101 builder = yaml_object.ObjectBuilder(PagespeedEntry)
102 handler = yaml_builder.BuilderHandler(builder)
103 listener = yaml_listener.EventListener(handler)
104 listener.Parse(pagespeed_entry)
106 parsed_yaml = handler.GetResults()
107 if not parsed_yaml:
108 return PagespeedEntry()
110 if len(parsed_yaml) > 1:
111 raise MalformedPagespeedConfiguration(
112 'Multiple configuration sections in the yaml')
114 return parsed_yaml[0]