/usr/lib/python2.7/dist-packages/zope/configuration/fields.py is in python-zope.configuration 4.0.3-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 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 | ##############################################################################
#
# Copyright (c) 2003 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.
#
##############################################################################
"""Configuration-specific schema fields
"""
import os
import re
import warnings
from zope.interface import implementer
from zope.schema import Bool as schema_Bool
from zope.schema import Field
from zope.schema import InterfaceField
from zope.schema import List
from zope.schema import Text
from zope.schema import TextLine
from zope.schema import ValidationError
from zope.schema.interfaces import IFromUnicode
from zope.configuration.exceptions import ConfigurationError
from zope.configuration.interfaces import InvalidToken
from zope.configuration._compat import u
PYIDENTIFIER_REGEX = u('\\A[a-zA-Z_]+[a-zA-Z0-9_]*\\Z')
pyidentifierPattern = re.compile(PYIDENTIFIER_REGEX)
@implementer(IFromUnicode)
class PythonIdentifier(TextLine):
"""This field describes a python identifier, i.e. a variable name.
"""
def fromUnicode(self, u):
return u.strip()
def _validate(self, value):
super(PythonIdentifier, self)._validate(value)
if pyidentifierPattern.match(value) is None:
raise ValidationError(value)
@implementer(IFromUnicode)
class GlobalObject(Field):
"""An object that can be accessed as a module global.
"""
def __init__(self, value_type=None, **kw):
self.value_type = value_type
super(GlobalObject, self).__init__(**kw)
def _validate(self, value):
super(GlobalObject, self)._validate(value)
if self.value_type is not None:
self.value_type.validate(value)
def fromUnicode(self, u):
name = str(u.strip())
# special case, mostly for interfaces
if name == '*':
return None
try:
value = self.context.resolve(name)
except ConfigurationError as v:
raise ValidationError(v)
self.validate(value)
return value
@implementer(IFromUnicode)
class GlobalInterface(GlobalObject):
"""An interface that can be accessed from a module.
"""
def __init__(self, **kw):
super(GlobalInterface, self).__init__(InterfaceField(), **kw)
@implementer(IFromUnicode)
class Tokens(List):
"""A list that can be read from a space-separated string.
"""
def fromUnicode(self, u):
u = u.strip()
if u:
vt = self.value_type.bind(self.context)
values = []
for s in u.split():
try:
v = vt.fromUnicode(s)
except ValidationError as v:
raise InvalidToken("%s in %s" % (v, u))
else:
values.append(v)
else:
values = []
self.validate(values)
return values
@implementer(IFromUnicode)
class Path(Text):
"""A file path name, which may be input as a relative path
Input paths are converted to absolute paths and normalized.
"""
def fromUnicode(self, u):
u = u.strip()
if os.path.isabs(u):
return os.path.normpath(u)
return self.context.path(u)
@implementer(IFromUnicode)
class Bool(schema_Bool):
"""A boolean value
Values may be input (in upper or lower case) as any of:
yes, no, y, n, true, false, t, or f.
"""
def fromUnicode(self, u):
u = u.lower()
if u in ('1', 'true', 'yes', 't', 'y'):
return True
if u in ('0', 'false', 'no', 'f', 'n'):
return False
raise ValidationError
@implementer(IFromUnicode)
class MessageID(Text):
"""Text string that should be translated.
When a string is converted to a message ID, it is also
recorded in the context.
"""
__factories = {}
def fromUnicode(self, u):
context = self.context
domain = getattr(context, 'i18n_domain', '')
if not domain:
domain = 'untranslated'
warnings.warn(
"You did not specify an i18n translation domain for the "\
"'%s' field in %s" % (self.getName(), context.info.file )
)
v = super(MessageID, self).fromUnicode(u)
# Check whether there is an explicit message is specified
default = None
if v.startswith('[]'):
v = v[2:].lstrip()
elif v.startswith('['):
end = v.find(']')
default = v[end+2:]
v = v[1:end]
# Convert to a message id, importing the factory, if necessary
factory = self.__factories.get(domain)
if factory is None:
import zope.i18nmessageid
factory = zope.i18nmessageid.MessageFactory(domain)
self.__factories[domain] = factory
msgid = factory(v, default)
# Record the string we got for the domain
i18n_strings = context.i18n_strings
strings = i18n_strings.get(domain)
if strings is None:
strings = i18n_strings[domain] = {}
locations = strings.setdefault(msgid, [])
locations.append((context.info.file, context.info.line))
return msgid
|