Stay organized with collections
Save and categorize content based on your preferences.
Source code for google.appengine.api.capabilities
#!/usr/bin/env python## Copyright 2007 Google Inc.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.#"""Allows applications to identify API outages and scheduled downtime.Example:: def StoreUploadedProfileImage(self): uploaded_image = self.request.get('img') # If the images API is unavailable, we'll just skip the resize. if CapabilitySet('images').is_enabled(): uploaded_image = images.resize(uploaded_image, 64, 64) store(uploaded_image) def RenderHTMLForm(self): datastore_readonly = CapabilitySet('datastore_v3', capabilities=['write']) if datastore_readonly.is_enabled(): # ...render form normally... else: # self.response.out('<p>Not accepting submissions right now: %s</p>' % datastore_readonly.admin_message()) # ...render form with form elements disabled...Individual API wrapper modules should expose `CapabilitySet` objectsfor users rather than relying on users to create them. They canalso create convenience methods that delegate to the relevant `CapabilitySet`;for example, `db.IsReadOnly()`."""importwarningsfromgoogle.appengine.api.capabilitiesimportcapability_service_pbfromgoogle.appengine.baseimportcapabilities_pbfromgoogle.appengine.apiimportapiproxy_stub_mapIsEnabledRequest=capability_service_pb.IsEnabledRequestIsEnabledResponse=capability_service_pb.IsEnabledResponseCapabilityConfig=capabilities_pb.CapabilityConfig
[docs]classUnknownCapabilityError(Exception):"""An unknown capability was requested."""
[docs]classCapabilitySet(object):"""Encapsulates one or more capabilities. Capabilities can either be named explicitly, or inferred from the list of methods provided. If no capabilities or methods are provided, this will check whether the entire package is enabled. """def__init__(self,package,capabilities=None,methods=None,stub_map=apiproxy_stub_map):"""Constructor. Args: capabilities: List of strings methods: List of strings """ifcapabilitiesisNone:capabilities=[]ifmethodsisNone:methods=[]self._package=packageself._capabilities=['*']+capabilitiesself._methods=methodsself._stub_map=stub_map
[docs]defis_enabled(self):"""Tests whether the capabilities are currently enabled. Returns: `True` if API calls that require these capabillities will succeed. Raises: UnknownCapabilityError: If a specified capability was not recognized. """config=self._get_status()returnconfig.summary_status()in(IsEnabledResponse.DEFAULT,IsEnabledResponse.ENABLED,IsEnabledResponse.SCHEDULED_FUTURE,IsEnabledResponse.SCHEDULED_NOW)
[docs]defwill_remain_enabled_for(self,time=60):"""Returns whether a capability will remain enabled. DEPRECATED: this method was never fully implemented and is considered deprecated. Use `is_enabled()` instead. Args: time: Number of seconds in the future to look when checking for scheduled downtime. Returns: `True` if there is no scheduled downtime for the specified capability within the amount of time specified. Raises: UnknownCapabilityError: If a specified capability was not recognized. """warnings.warn('will_remain_enabled_for() is deprecated: ''use is_enabled instead.',DeprecationWarning,stacklevel=2)config=self._get_status()status=config.summary_status()ifstatusin(IsEnabledResponse.DEFAULT,IsEnabledResponse.ENABLED):returnTrueelifstatus==IsEnabledResponse.SCHEDULED_NOW:returnFalseelifstatus==IsEnabledResponse.SCHEDULED_FUTURE:ifconfig.has_time_until_scheduled():returnconfig.time_until_scheduled()>=timeelse:returnTrueelifstatus==IsEnabledResponse.DISABLED:returnFalseelse:returnFalse
[docs]defadmin_message(self):"""Retrieves any administrator notice messages for these capabilities. Returns: A string containing one or more administrator messages, or an empty string. Raises: UnknownCapabilityError: If a specified capability was not recognized. """message_list=[]forconfiginself._get_status().config_list():message=config.admin_message()ifmessageandmessagenotinmessage_list:message_list.append(message)return' '.join(message_list)
def_get_status(self):"""Gets the `IsEnabledResponse` for the capabilities listed. Returns: `IsEnabledResponse` for the specified capabilities. Raises: UnknownCapabilityError: If an unknown capability was requested. """req=IsEnabledRequest()req.set_package(self._package)forcapabilityinself._capabilities:req.add_capability(capability)formethodinself._methods:req.add_call(method)resp=capability_service_pb.IsEnabledResponse()self._stub_map.MakeSyncCall('capability_service','IsEnabled',req,resp)ifresp.summary_status()==IsEnabledResponse.UNKNOWN:raiseUnknownCapabilityError()returnresp