本文整理汇总了Python中zope.interface.provided_by函数的典型用法代码示例。如果您正苦于以下问题:Python provided_by函数的具体用法?Python provided_by怎么用?Python provided_by使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了provided_by函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: resource_to_url
def resource_to_url(self, resource, **kw):
if ICollectionResource in provided_by(resource):
query = {}
query.update(kw)
if not resource.filter is None:
query['q'] = \
UrlPartsConverter.make_filter_string(resource.filter)
if not resource.order is None:
query['sort'] = \
UrlPartsConverter.make_order_string(resource.order)
if not resource.slice is None:
query['start'], query['size'] = \
UrlPartsConverter.make_slice_strings(resource.slice)
if query != {}:
url = model_url(resource, self.__request, query=query)
else:
url = model_url(resource, self.__request)
elif not IMemberResource in provided_by(resource):
raise ValueError('Can not convert non-resource object "%s to '
'URL".' % resource)
else:
if resource.__parent__ is None:
raise ValueError('Can not generate URL for floating member '
'"%s".' % resource)
url = model_url(resource, self.__request)
return unquote(url)
示例2: url_to_resource
def url_to_resource(self, url):
"""
Returns the resource that is addressed by the given URL.
:param str url: URL to convert
:return: member or collection resource
:note: If the query string in the URL has multiple values for a
query parameter, the last definition in the query string wins.
"""
parsed = urlparse.urlparse(url)
parsed_path = parsed.path # namedtupble problem pylint: disable=E1101
rc = find_resource(self.__request.root, traversal_path(parsed_path))
if ICollectionResource in provided_by(rc):
# In case we found a collection, we have to filter, order, slice.
parsed_query = parsed.query # namedtuple problem pylint: disable=E1101
params = dict(parse_qsl(parsed_query))
filter_string = params.get('q')
if not filter_string is None:
rc.filter = \
UrlPartsConverter.make_filter_specification(filter_string)
order_string = params.get('sort')
if not order_string is None:
rc.order = \
UrlPartsConverter.make_order_specification(order_string)
start_string = params.get('start')
size_string = params.get('size')
if not (start_string is None or size_string is None):
rc.slice = \
UrlPartsConverter.make_slice_key(start_string, size_string)
elif not IMemberResource in provided_by(rc):
raise ValueError('Traversal found non-resource object "%s".' % rc)
return rc
示例3: get_nested
def get_nested(self, attr):
# We only allow *one* child with the given name.
q_tag = self.__get_q_tag(attr)
child_it = self.iterchildren(q_tag)
try:
child = child_it.next()
except StopIteration:
child = None
else:
try:
child_it.next()
except StopIteration:
pass
else:
# This should never happen.
raise ValueError('More than one child for member '
'attribute "%s" found.' % attr) # pragma: no cover
# Link handling: look for wrapper tag with *one* link child.
if child.countchildren() == 1:
grand_child = child.getchildren()[0]
if ILinkedDataElement in provided_by(grand_child):
# We inject the id attribute from the wrapper element.
str_xml = child.get('id')
if not str_xml is None:
grand_child.set('id', str_xml)
child = grand_child
return child
示例4: __init__
def __init__(self, data_element):
if ILinkedDataElement in provided_by(data_element):
raise ValueError('Do not use data element proxies with linked '
'data elements.')
attrs = data_element.mapping.attribute_iterator()
self.__attr_map = dict([(attr.repr_name, attr) for attr in attrs])
self.__data_element = data_element
示例5: create_mapping
def create_mapping(self, mapped_class, configuration=None):
"""
Creates a new mapping for the given mapped class and representer
configuration.
:param configuration: configuration for the new data element class.
:type configuration: :class:`RepresenterConfiguration`
:returns: newly created instance of :class:`Mapping`
"""
cfg = self.__configuration.copy()
if not configuration is None:
cfg.update(configuration)
provided_ifcs = provided_by(object.__new__(mapped_class))
if IMemberResource in provided_ifcs:
base_data_element_class = self.member_data_element_base_class
elif ICollectionResource in provided_ifcs:
base_data_element_class = self.collection_data_element_base_class
elif IResourceLink in provided_ifcs:
base_data_element_class = self.linked_data_element_base_class
else:
raise ValueError('Mapped class for data element class does not '
'implement one of the required interfaces.')
name = "%s%s" % (mapped_class.__name__,
base_data_element_class.__name__)
de_cls = type(name, (base_data_element_class,), {})
mp = self.mapping_class(self, mapped_class, de_cls, cfg)
# Set the data element class' mapping.
# FIXME: This looks like a hack.
de_cls.mapping = mp
return mp
示例6: provides_collection_resource
def provides_collection_resource(obj):
"""
Checks if the given type or instance provides the
:class:`everest.resources.interfaces.ICollectionResource` interface.
"""
if isinstance(obj, type):
obj = object.__new__(obj)
return ICollectionResource in provided_by(obj)
示例7: resource_to_url
def resource_to_url(self, resource, quote=False):
"""
Returns the URL for the given resource.
:param resource: Resource to create a URL for.
:param bool quote: If set, the URL returned will be quoted.
:raises ValueError: If the given resource is floating (i.e., has
the parent attribute set to `None`)
"""
ifc = provided_by(resource)
if not IResource in ifc:
raise TypeError('Can not generate URL for non-resource "%s".'
% resource)
elif resource.__parent__ is None:
raise ValueError('Can not generate URL for floating resource '
'"%s".' % resource)
if ICollectionResource in ifc:
query = {}
if not resource.filter is None:
query['q'] = \
UrlPartsConverter.make_filter_string(resource.filter)
if not resource.order is None:
query['sort'] = \
UrlPartsConverter.make_order_string(resource.order)
if not resource.slice is None:
query['start'], query['size'] = \
UrlPartsConverter.make_slice_strings(resource.slice)
if query != {}:
options = dict(query=query)
else:
options = dict()
if not resource.is_root_collection:
# For nested collections, we check if the referenced root
# collection is exposed (i.e., has the service as parent).
# If yes, we return an absolute URL, else a nested URL.
root_coll = get_root_collection(resource)
if not root_coll.has_parent:
url = self.__request.resource_url(resource, **options)
else:
url = self.__request.resource_url(root_coll, **options)
else:
url = self.__request.resource_url(resource, **options)
else:
if not resource.is_root_member:
# For nested members, we check if the referenced root
# collection is exposed (i.e., has the service as parent).
# If yes, we return an absolute URL, else a nested URL.
root_coll = get_root_collection(resource)
if not root_coll.has_parent:
par_url = self.__request.resource_url(resource)
else:
par_url = self.__request.resource_url(root_coll)
url = "%s%s/" % (par_url, resource.__name__)
else:
url = self.__request.resource_url(resource)
if not quote:
url = url_unquote(url)
return url
示例8: _dispatch
def _dispatch(self, attr_key, attr, node, parent_data, visitor):
ifcs = provided_by(node)
if IMemberResource in ifcs:
self._traverse_member(attr_key, attr, node, parent_data, visitor)
elif ICollectionResource in ifcs:
self._traverse_collection(attr_key, attr, node, parent_data,
visitor)
else:
raise ValueError('Data must be a resource.')
示例9: __getattr__
def __getattr__(self, name):
value = self.__data_element.get_attribute(name)
ifcs = provided_by(value)
if IMemberDataElement in ifcs:
value = DataElementAttributeProxy(value)
elif ICollectionDataElement in ifcs:
value = [DataElementAttributeProxy(mb_el)
for mb_el in value.get_members()]
return value
示例10: __check_for_link
def __check_for_link(self, child):
# Link handling: look for wrapper tag with *one* link child.
if child.countchildren() == 1:
grand_child = child.getchildren()[0]
if ILinkedDataElement in provided_by(grand_child):
# # We inject the id attribute from the wrapper element.
# str_xml = child.get('id')
# if not str_xml is None:
# grand_child.set('id', str_xml)
child = grand_child
return child
示例11: register
def register(cls, value_type, converter_class):
if cls.__converters is None: # Lazy initialization.
cls.__converters = {}
if value_type in cls.__converters:
raise ValueError('For %s, a converter has already been '
'registered (%s).'
% (value_type, cls.__converters[value_type]))
if not IRepresentationConverter in provided_by(converter_class):
raise ValueError('Converter class must provide '
'IRepresenterConverter.')
cls.__converters[value_type] = converter_class
示例12: _dispatch
def _dispatch(self, attr_key, attr, node, parent_data, visitor):
ifcs = provided_by(node)
if IMemberResource in ifcs:
self._traverse_member(attr_key, attr, node, parent_data, visitor)
elif ICollectionResource in ifcs:
self._traverse_collection(attr_key, attr, node, parent_data,
visitor)
else:
raise ValueError('Can only traverse objects that provide'
'IMemberResource or ICollectionResource '
'(key: %s).' % str(attr_key))
示例13: as_repository
def as_repository(resource):
"""
Adapts the given registered resource to its configured repository.
:return: object implementing
:class:`everest.repositories.interfaces.IRepository`.
"""
reg = get_current_registry()
if IInterface in provided_by(resource):
resource = reg.getUtility(resource, name='collection-class')
return reg.getAdapter(resource, IRepository)
示例14: __init__
def __init__(self, attr_type, entity_attr=None,
cardinality=None, is_nested=False):
"""
:param bool is_nested: indicates if the URLs generated for this
relation descriptor should be relative to the parent ("nested")
or absolute.
"""
if not (isinstance(attr_type, type)
or IInterface in provided_by(attr_type)):
raise ValueError('The attribute type of a member or collection '
' attribute must be a class or an interface.')
attribute_base.__init__(self, attr_type, entity_attr, cardinality)
self.is_nested = is_nested
示例15: _data_callback
def _data_callback(cls, value, options):
# Set the default for the report directory.
if options.report_directory is None:
options.report_directory = os.path.dirname(value)
coll_cls = get_collection_class(cls.registration_resource)
rpr = as_representer(object.__new__(coll_cls), JsonMime)
reg_items = rpr.from_stream(open(value, 'rU'))
# FIXME: This should be treated properly in everest.
if IMemberResource in provided_by(reg_items):
ents = [reg_items.get_entity()]
else:
ents = [rc.get_entity() for rc in reg_items]
return ents