/usr/lib/python2.7/dist-packages/zope/schema/_schema.py is in python-zope.schema 4.4.2-3.
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 | ##############################################################################
#
# Copyright (c) 2002 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Schema convenience functions
"""
import zope.interface.verify
def getFieldNames(schema):
"""Return a list of all the Field names in a schema.
"""
from zope.schema.interfaces import IField
return [name for name in schema if IField.providedBy(schema[name])]
def getFields(schema):
"""Return a dictionary containing all the Fields in a schema.
"""
from zope.schema.interfaces import IField
fields = {}
for name in schema:
attr = schema[name]
if IField.providedBy(attr):
fields[name] = attr
return fields
def getFieldsInOrder(schema, _field_key=lambda x: x[1].order):
"""Return a list of (name, value) tuples in native schema order.
"""
return sorted(getFields(schema).items(), key=_field_key)
def getFieldNamesInOrder(schema):
"""Return a list of all the Field names in a schema in schema order.
"""
return [name for name, field in getFieldsInOrder(schema)]
def getValidationErrors(schema, object):
"""Return a list of all validation errors.
"""
errors = getSchemaValidationErrors(schema, object)
if errors:
return errors
# Only validate invariants if there were no previous errors. Previous
# errors could be missing attributes which would most likely make an
# invariant raise an AttributeError.
invariant_errors = []
try:
schema.validateInvariants(object, invariant_errors)
except zope.interface.exceptions.Invalid:
# Just collect errors
pass
errors = [(None, e) for e in invariant_errors]
return errors
def getSchemaValidationErrors(schema, object):
errors = []
for name in schema.names(all=True):
if zope.interface.interfaces.IMethod.providedBy(schema[name]):
continue
attribute = schema[name]
if not zope.schema.interfaces.IField.providedBy(attribute):
continue
try:
value = getattr(object, name)
except AttributeError as error:
# property for the given name is not implemented
errors.append((
name, zope.schema.interfaces.SchemaNotFullyImplemented(error)))
else:
try:
attribute.bind(object).validate(value)
except zope.schema.ValidationError as e:
errors.append((name, e))
return errors
|