1.9.30 sync.
[gae.git] / python / google / appengine / api / pagespeedinfo.py
blob73b0f82cb0f15dfd5dd36c5f8ec002aa924d2a16
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 """
34 import google
36 from google.appengine.api import validation
37 from google.appengine.api import yaml_builder
38 from google.appengine.api import yaml_listener
39 from google.appengine.api import yaml_object
41 _URL_BLACKLIST_REGEX = r'http(s)?://\S{0,499}'
42 _REWRITER_NAME_REGEX = r'[a-zA-Z0-9_]+'
43 _DOMAINS_TO_REWRITE_REGEX = r'(http(s)?://)?[-a-zA-Z0-9_.*]+(:\d+)?'
45 URL_BLACKLIST = 'url_blacklist'
46 ENABLED_REWRITERS = 'enabled_rewriters'
47 DISABLED_REWRITERS = 'disabled_rewriters'
48 DOMAINS_TO_REWRITE = 'domains_to_rewrite'
51 class MalformedPagespeedConfiguration(Exception):
52 """Configuration file for PageSpeed API is malformed."""
59 class PagespeedEntry(validation.Validated):
60 """Describes the format of a pagespeed configuration from a yaml file.
62 URL blacklist entries are patterns (with '?' and '*' as wildcards). Any URLs
63 that match a pattern on the blacklist will not be optimized by PageSpeed.
65 Rewriter names are strings (like 'CombineCss' or 'RemoveComments') describing
66 individual PageSpeed rewriters. A full list of valid rewriter names can be
67 found in the PageSpeed documentation.
69 The domains-to-rewrite list is a whitelist of domain name patterns with '*' as
70 a wildcard, optionally starting with 'http://' or 'https://'. If no protocol
71 is given, 'http://' is assumed. A resource will only be rewritten if it is on
72 the same domain as the HTML that references it, or if its domain is on the
73 domains-to-rewrite list.
74 """
75 ATTRIBUTES = {
76 URL_BLACKLIST: validation.Optional(
77 validation.Repeated(validation.Regex(_URL_BLACKLIST_REGEX))),
78 ENABLED_REWRITERS: validation.Optional(
79 validation.Repeated(validation.Regex(_REWRITER_NAME_REGEX))),
80 DISABLED_REWRITERS: validation.Optional(
81 validation.Repeated(validation.Regex(_REWRITER_NAME_REGEX))),
82 DOMAINS_TO_REWRITE: validation.Optional(
83 validation.Repeated(validation.Regex(_DOMAINS_TO_REWRITE_REGEX))),
87 def LoadPagespeedEntry(pagespeed_entry, open_fn=None):
88 """Load a yaml file or string and return a PagespeedEntry.
90 Args:
91 pagespeed_entry: The contents of a pagespeed entry from a yaml file
92 as a string, or an open file object.
93 open_fn: Function for opening files. Unused.
95 Returns:
96 A PagespeedEntry instance which represents the contents of the parsed yaml.
98 Raises:
99 yaml_errors.EventError: An error occured while parsing the yaml.
100 MalformedPagespeedConfiguration: The configuration is parseable but invalid.
102 builder = yaml_object.ObjectBuilder(PagespeedEntry)
103 handler = yaml_builder.BuilderHandler(builder)
104 listener = yaml_listener.EventListener(handler)
105 listener.Parse(pagespeed_entry)
107 parsed_yaml = handler.GetResults()
108 if not parsed_yaml:
109 return PagespeedEntry()
111 if len(parsed_yaml) > 1:
112 raise MalformedPagespeedConfiguration(
113 'Multiple configuration sections in the yaml')
115 return parsed_yaml[0]