/usr/share/pyshared/lazr/restful/metazcml.py is in python-lazr.restful 0.19.3-0ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 | # Copyright 2008 Canonical Ltd. All rights reserved.
"""ZCML registration directives for the LAZR webservice framework."""
__metaclass__ = type
__all__ = []
import inspect
import itertools
from zope.component import (
adapter,
getGlobalSiteManager,
getUtility,
)
from zope.component.zcml import handler
from zope.configuration.fields import GlobalObject
from zope.interface import Interface
from zope.interface.interfaces import IInterface
from zope.processlifetime import IProcessStarting
from lazr.restful.declarations import (
COLLECTION_TYPE,
ENTRY_TYPE,
LAZR_WEBSERVICE_EXPORTED,
OPERATION_TYPES,
REMOVED_OPERATION_TYPE,
generate_collection_adapter,
generate_entry_adapters,
generate_entry_interfaces,
generate_operation_adapter,
)
from lazr.restful.error import WebServiceExceptionView
from lazr.restful.interfaces import (
ICollection,
ICollectionField,
IEntry,
IReference,
IResourceDELETEOperation,
IResourceGETOperation,
IResourcePOSTOperation,
IWebServiceClientRequest,
IWebServiceConfiguration,
IWebServiceVersion,
)
from lazr.restful.utils import VersionedObject
# Keep track of entry and operation registrations so we can
# sanity-check them later.
REGISTERED_ENTRIES = []
REGISTERED_OPERATIONS = []
class IRegisterDirective(Interface):
"""Directive to hook up webservice based on the declarations in a module.
"""
module = GlobalObject(
title=u'Module which will be inspected for webservice declarations')
def generate_and_register_entry_adapters(interface, info, contributors,
repository=None):
"""Generate an entry adapter for every version of the web service.
This code generates an IEntry subinterface for every version, each
subinterface based on the annotations in `interface` for that
version. It then generates a set of factory classes for each
subinterface, and registers each as an adapter for the appropriate
version and version-specific interface.
"""
# Get the list of versions.
config = getUtility(IWebServiceConfiguration)
versions = list(config.active_versions)
# Generate an interface and an adapter for every version.
web_interfaces = generate_entry_interfaces(
interface, contributors, *versions)
web_factories = generate_entry_adapters(
interface, contributors, web_interfaces)
for index, (interface_version, web_interface) in (
enumerate(web_interfaces)):
factory_version, factory = web_factories[index]
assert factory_version==interface_version, (
"Generated interface and factory versions don't match up! "
'%s vs. %s' % (factory_version, interface_version))
# If this is the earliest version, register against a generic
# request interface rather than a version-specific one. This
# will make certain tests require less setup.
if interface_version == versions[0]:
interface_version = None
register_adapter_for_version(factory, interface, interface_version,
IEntry, '', info)
# If we were given a repository, add the interface and version
# to it.
if repository is not None:
repository.append(VersionedObject(interface_version, web_interface))
def ensure_correct_version_ordering(name, version_list):
"""Make sure that a list mentions versions from earliest to latest.
If an earlier version shows up after a later version, this is a
sign that a developer was confused about version names when
annotating the web service.
:param name: The name of the object annotated with the
possibly mis-ordered versions.
:param version_list: The list of versions found in the interface.
:raise AssertionError: If the given version list is not a
earlier-to-later ordering of a subset of the web service's
versions.
"""
configuration = getUtility(IWebServiceConfiguration)
actual_versions = configuration.active_versions
# Replace None with the actual version number of the earliest
# version.
try:
earliest_version_pos = version_list.index(None)
version_list = list(version_list)
version_list[earliest_version_pos] = actual_versions[0]
except ValueError:
# The earliest version was not mentioned in the version list.
# Do nothing.
pass
# Sort version_list according to the list of actual versions.
# If the sorted list is different from the unsorted list, at
# least one version is out of place.
def compare(x, y):
return cmp (actual_versions.index(x), actual_versions.index(y))
sorted_version_list = sorted(version_list, cmp=compare)
if sorted_version_list != version_list:
bad_versions = '", "'.join(version_list)
good_versions = '", "'.join(sorted_version_list)
msg = ('Annotations on "%s" put an earlier version on top of a '
'later version: "%s". The correct order is: "%s".')
raise AssertionError, msg % (name, bad_versions, good_versions)
def register_adapter_for_version(factory, interface, version_name,
provides, name, info):
"""A version-aware wrapper for the registerAdapter operation.
During web service generation we often need to register an adapter
for a particular version of the web service. The way to do this is
to register a multi-adapter using the interface being adapted to,
plus the marker interface for the web service version.
These marker interfaces are not available when the web service is
being generated, but the version strings are available. So methods
like register_webservice_operations use this function as a handler
for the second stage of ZCML processing.
This function simply looks up the appropriate marker interface and
calls Zope's handler('registerAdapter').
"""
if version_name is None:
# This adapter is for the earliest supported version. Register
# it against the generic IWebServiceClientRequest interface,
# which is the superclass of the marker interfaces for every
# specific version.
marker = IWebServiceClientRequest
else:
# Look up the marker interface for the given version. This
# will also ensure the given version string has an
# IWebServiceVersion utility registered for it, and is not
# just a random string.
marker = getUtility(IWebServiceVersion, name=version_name)
handler('registerAdapter', factory, (interface, marker),
provides, name, info)
def _is_exported_interface(member):
"""Helper for find_exported_interfaces; a predicate to inspect.getmembers.
Returns True if member is a webservice-aware Exception or interface.
"""
if (inspect.isclass(member) and
issubclass(member, Exception) and
getattr(member, '__lazr_webservice_error__', None) is not None):
# This is a webservice-aware Exception.
return True
if IInterface.providedBy(member):
tag = member.queryTaggedValue(LAZR_WEBSERVICE_EXPORTED)
if tag is not None and tag['type'] in [ENTRY_TYPE, COLLECTION_TYPE]:
# This is a webservice-aware interface.
return True
return False
def find_exported_interfaces(module):
"""Find all the interfaces in a module marked for export.
It also includes exceptions that represents errors on the webservice.
:return: iterator of interfaces.
"""
return (interface for name, interface
in inspect.getmembers(module, _is_exported_interface))
def find_interfaces_and_contributors(module):
"""Find the interfaces and its contributors marked for export.
Return a dictionary with interfaces as keys and their contributors as
values.
"""
interfaces_with_contributors = {}
for interface in find_exported_interfaces(module):
if issubclass(interface, Exception):
# Exceptions can't have interfaces, so just store it in
# interfaces_with_contributors and move on.
interfaces_with_contributors.setdefault(interface, [])
continue
tag = interface.getTaggedValue(LAZR_WEBSERVICE_EXPORTED)
if tag.get('contributes_to'):
# Append this interface (which is a contributing interface) to the
# list of contributors of every interface it contributes to.
for iface in tag['contributes_to']:
if iface.queryTaggedValue(LAZR_WEBSERVICE_EXPORTED) is None:
raise AttemptToContributeToNonExportedInterface(
"Interface %s contributes to %s, which is not "
"exported." % (interface.__name__, iface.__name__))
raise AssertionError('foo')
contributors = interfaces_with_contributors.setdefault(
iface, [])
contributors.append(interface)
else:
# This is a regular interface, but one of its contributing
# interfaces may have been processed previously and in that case a
# key for it would already exist in interfaces_with_contributors;
# that's why we use setdefault.
interfaces_with_contributors.setdefault(interface, [])
# For every exported interface, check that none of its names are exported
# in more than one contributing interface.
for interface, contributors in interfaces_with_contributors.items():
if len(contributors) == 0:
continue
names = {}
for iface in itertools.chain([interface], contributors):
for name, f in iface.namesAndDescriptions(all=True):
if f.queryTaggedValue(LAZR_WEBSERVICE_EXPORTED) is not None:
L = names.setdefault(name, [])
L.append(iface)
for name, interfaces in names.items():
if len(interfaces) > 1:
raise ConflictInContributingInterfaces(name, interfaces)
return interfaces_with_contributors
class ConflictInContributingInterfaces(Exception):
"""More than one interface tried to contribute a given attribute/method to
another interface.
"""
def __init__(self, name, interfaces):
self.msg = (
"'%s' is exported in more than one contributing interface: %s"
% (name, ", ".join(i.__name__ for i in interfaces)))
def __str__(self):
return self.msg
class AttemptToContributeToNonExportedInterface(Exception):
"""An interface contributes to another one which is not exported."""
def register_webservice(context, module):
"""Generate and register web service adapters.
All interfaces in the module are inspected, and appropriate interfaces and
adapters are generated and registered for the ones marked for export on
the web service.
"""
if not inspect.ismodule(module):
raise TypeError("module attribute must be a module: %s, %s" %
module, type(module))
interfaces_with_contributors = find_interfaces_and_contributors(module)
for interface, contributors in interfaces_with_contributors.items():
if issubclass(interface, Exception):
register_exception_view(context, interface)
continue
tag = interface.getTaggedValue(LAZR_WEBSERVICE_EXPORTED)
if tag['type'] == ENTRY_TYPE:
context.action(
discriminator=('webservice entry interface', interface),
callable=generate_and_register_entry_adapters,
args=(interface, context.info, contributors,
REGISTERED_ENTRIES),
)
elif tag['type'] == COLLECTION_TYPE:
for version in tag['collection_default_content'].keys():
factory = generate_collection_adapter(interface, version)
provides = ICollection
context.action(
discriminator=(
'webservice versioned adapter', interface, provides,
'', version),
callable=register_adapter_for_version,
args=(factory, interface, version, provides, '',
context.info),
)
else:
raise AssertionError('Unknown export type: %s' % tag['type'])
context.action(
discriminator=('webservice versioned operations', interface),
args=(context, interface, contributors, REGISTERED_OPERATIONS),
callable=generate_and_register_webservice_operations)
@adapter(IProcessStarting)
def webservice_sanity_checks(registration):
"""Ensure the web service contains no references to unpublished objects.
We are worried about fields that link to unpublished objects, and
operations that have arguments or return values that are
unpublished. An unpublished object may not be published at all, or
it may not be published in the same version in which it's
referenced.
"""
global REGISTERED_ENTRIES
global REGISTERED_OPERATIONS
# Create a mapping of marker interfaces to version names.
versions = getUtility(IWebServiceConfiguration).active_versions
version_for_marker = { IWebServiceClientRequest: versions[0] }
for version in versions:
marker_interface = getUtility(IWebServiceVersion, name=version)
version_for_marker[marker_interface] = version
# For each version, build a list of all of the IEntries published
# in that version's web service. This works because every IEntry
# is explicitly registered for every version, even if there's been
# no change since the last version. For the sake of performance,
# the list is stored as a set of VersionedObject 2-tuples.
available_registrations = set()
registrations = getGlobalSiteManager().registeredAdapters()
for registration in registrations:
if (not IInterface.providedBy(registration.provided)
or not registration.provided.isOrExtends(IEntry)):
continue
interface, version_marker = registration.required
available_registrations.add(VersionedObject(
version_for_marker[version_marker], interface))
# Check every Reference and CollectionField field in every IEntry
# interface, making sure that they only reference entries that are
# published in that version.
for version, interface in REGISTERED_ENTRIES:
for name, field in interface.namesAndDescriptions():
referenced_interface, what = _extract_reference_type(field)
if referenced_interface is not None:
_assert_interface_registered_for_version(
version, referenced_interface, available_registrations,
("%(interface)s.%(field)s is %(what)s"
% dict(interface=interface.__name__,
field=field.__name__, what=what)))
# For every version, check the return value and arguments to every
# named operation published in that version, making sure that
# there are no references to IEntries not published in that
# version.
for version, method in REGISTERED_OPERATIONS:
tags = method.getTaggedValue(LAZR_WEBSERVICE_EXPORTED)
return_type = tags.get('return_type')
referenced_interface, what = _extract_reference_type(return_type)
if referenced_interface is not None:
_assert_interface_registered_for_version(
version, referenced_interface, available_registrations,
"named operation %s returns %s" % (tags['as'], what))
if not 'params' in tags:
continue
for name, field in tags['params'].items():
referenced_interface, what = _extract_reference_type(field)
if referenced_interface is not None:
_assert_interface_registered_for_version(
version, referenced_interface, available_registrations,
"named operation %s accepts %s" % (tags['as'], what))
del REGISTERED_OPERATIONS[:]
del REGISTERED_ENTRIES[:]
def _extract_reference_type(field):
"""Determine what kind of object the given field is a reference to.
This is a helper method used by the sanity checker.
:return: A 2-tuple (reference_type, human_readable): If
`field` is a reference to a scalar entry, human_readable is
the name of the interface. If `field` is a collection of
entries, human_readable is the string "a collection of
[interface name]". If `field` is not a reference to an
entry, both values are None.
"""
if IReference.providedBy(field):
schema = field.schema
return (schema, schema.__name__)
elif ICollectionField.providedBy(field):
schema = field.value_type.schema
return (schema, "a collection of %s" % schema.__name__)
else:
return (None, None)
def _assert_interface_registered_for_version(
version, interface, available_registrations, error_message_insert):
"""A helper method for the sanity checker.
See if the given entry interface is published in the given version
(as determined by the contents of `available_registrations`), and
if it's not, raise an exception.
:param version: The version in which `interface` should be published.
:param interface: The interface that ought to be published.
:param available_registrations: A set of (version, interface) pairs
to check.
:param error_message_insert: A snippet of text explaining the significance
if `interface`, eg. "IMyInterface.myfield is IOughtToBePublished" or
"named operation my_named_operation returns IOughtToBePublished".
"""
if version is None:
version = getUtility(IWebServiceConfiguration).active_versions[0]
to_check = VersionedObject(version, interface)
if to_check not in available_registrations:
raise ValueError(
"In version %(version)s, %(problem)s, but version %(version)s "
"of the web service does not publish %(reference)s as "
"an entry. (It may not be published at all.)" % dict(
version=version, problem=error_message_insert,
reference=interface.__name__))
def generate_and_register_webservice_operations(
context, interface, contributors, repository=None):
"""Create and register adapters for all exported methods.
Different versions of the web service may publish the same
operation differently or under different names.
"""
# First of all, figure out when to stop publishing field mutators
# as named operations.
config = getUtility(IWebServiceConfiguration)
if config.last_version_with_mutator_named_operations is None:
no_mutator_operations_after = None
block_mutator_operations_as_of_version = None
else:
no_mutator_operations_after = config.active_versions.index(
config.last_version_with_mutator_named_operations)
if len(config.active_versions) > no_mutator_operations_after+1:
block_mutator_operations_as_of_version = config.active_versions[
no_mutator_operations_after+1]
else:
block_mutator_operations_as_of_version = None
methods = interface.namesAndDescriptions(True)
for iface in contributors:
methods.extend(iface.namesAndDescriptions(True))
for name, method in methods:
tag = method.queryTaggedValue(LAZR_WEBSERVICE_EXPORTED)
if tag is None or tag['type'] not in OPERATION_TYPES:
# This method is not published as a named operation.
continue
human_readable_name = interface.__name__ + '.' + method.__name__
if (config.require_explicit_versions and tag.stack[0][0] is None):
# We have annotations for an implicitly defined version, which
# is prohibited. But don't panic--these might just be the
# default annotations set by the system. We only need to raise
# an exception if the method is actually *published*.
version, annotations = tag.stack[0]
if annotations.get('type') != REMOVED_OPERATION_TYPE:
version = config.active_versions[0]
raise ValueError(
("%s: Implicitly tagged for export to web service "
'version "%s", but the service configuration requires '
"all version declarations to be explicit. You should "
'add @operation_for_version("%s") to the bottom of the '
'annotation stack.') % (
human_readable_name, version, version))
# Make sure that this method was annotated with the
# versions in the right order.
ensure_correct_version_ordering(
human_readable_name, tag.dict_names)
operation_name = None
# If an operation's name does not change between version n and
# version n+1, we want lookups for requests that come in for
# version n+1 to find the registered adapter for version n. This
# happens automatically. But if the operation name changes
# (because the operation is now published under a new name, or
# because the operation has been removed), we need to register a
# masking adapter: something that will stop version n+1's lookups
# from finding the adapter registered for version n. To this end
# we keep track of what the operation looked like in the previous
# version.
previous_operation_name = None
previous_operation_provides = None
mutator_operation_needs_to_be_blocked = False
master_stack = tag
for version, tag in tag.stack:
if version is None:
this_version_index = 0
else:
this_version_index = config.active_versions.index(
version)
if tag['type'] == REMOVED_OPERATION_TYPE:
# This operation is not present in this version.
# We'll represent this by setting the operation_name
# to None. If the operation was not present in the
# previous version either (or there is no previous
# version), previous_operation_name will also be None
# and nothing will happen. If the operation was
# present in the previous version,
# previous_operation_name will not be None, and the
# code that handles name changes will install a
# masking adapter.
operation_name = None
operation_provides = None
factory = None
# If there are any other tags besides 'type', it means
# that the developer tried to annotate a method for a
# version where the method isn't published. Let's warn
# them about it.
#
# (We can't make this check when the annotation
# happens, because it will reject a method that uses
# an annotation like @export_operation_as before using
# an annotation like @export_read_operation. That's a
# little sloppy, but it's not bad enough to warrant an
# exception.)
tag_names = list(tag.keys())
if tag_names != ['type']:
tag_names.remove('type')
raise AssertionError(
'Method "%s" contains annotations for version "%s", '
'even though it\'s not published in that version. '
'The bad annotations are: "%s".' % (
method.__name__, version, '", "'.join(tag_names)))
else:
if tag['type'] == 'read_operation':
operation_provides = IResourceGETOperation
elif tag['type']in ['factory', 'write_operation']:
operation_provides = IResourcePOSTOperation
elif tag['type'] in ['destructor']:
operation_provides = IResourceDELETEOperation
else:
# We know it's not REMOVED_OPERATION_TYPE, because
# that case is handled above.
raise AssertionError(
'Unknown operation type: %s' % tag['type'])
operation_name = tag.get('as')
if tag['type'] in ['destructor']:
operation_name = ''
if (tag.get('is_mutator', False)
and (no_mutator_operations_after is None
or no_mutator_operations_after < this_version_index)):
# This is a mutator method, and in this version,
# mutator methods are not published as named
# operations at all. Block any lookup of the named
# operation from succeeding.
#
# This will save us from having to do another
# de-registration later.
factory = _mask_adapter_registration
mutator_operation_needs_to_be_blocked = False
else:
factory = generate_operation_adapter(method, version)
# Operations are looked up by name. If the operation's
# name has changed from the previous version to this
# version, or if the operation was removed in this
# version, we need to block lookups of the previous name
# from working.
if (operation_name != previous_operation_name
and previous_operation_name is not None):
register_adapter_for_version(
_mask_adapter_registration, interface, version,
previous_operation_provides, previous_operation_name,
context.info)
# If the operation exists in this version (ie. its name is
# not None), register it using this version's name.
if operation_name is not None:
register_adapter_for_version(
factory, interface, version, operation_provides,
operation_name, context.info)
if (tag.get('is_mutator')
and block_mutator_operations_as_of_version != None):
defined_in = this_version_index
blocked_as_of = config.active_versions.index(
block_mutator_operations_as_of_version)
if defined_in < blocked_as_of:
# In this version, this mutator is also a
# named operation. But in a later version
# mutators stop being named operations, and
# the operation registration for this mutator
# will need to be blocked.
mutator_operation_needs_to_be_blocked = True
if repository is not None:
repository.append(VersionedObject(version, method))
previous_operation_name = operation_name
previous_operation_provides = operation_provides
if mutator_operation_needs_to_be_blocked:
# The operation was registered as a mutator, back in the
# days when mutator operations were also named operations,
# and it never got de-registered. De-register it now.
register_adapter_for_version(
_mask_adapter_registration, interface,
block_mutator_operations_as_of_version,
previous_operation_provides, previous_operation_name,
context.info)
def _mask_adapter_registration(*args):
"""A factory function that stops an adapter lookup from succeeding.
This function is registered when it's necessary to explicitly stop
some part of web service version n from being visible to version n+1.
"""
return None
def register_exception_view(context, exception):
"""Register WebServiceExceptionView to handle exception on the webservice.
"""
context.action(
discriminator=(
'view', exception, 'index.html', IWebServiceClientRequest,
IWebServiceClientRequest),
callable=handler,
args=('registerAdapter',
WebServiceExceptionView, (exception, IWebServiceClientRequest),
Interface, 'index.html', context.info),
)
|