comparison mercurial/thirdparty/zope/interface/common/mapping.py @ 37176:943d77fc07a3

thirdparty: vendor zope.interface 4.4.3 I've been trying to formalize interfaces for various components of Mercurial. So far, we've been using the "abc" package. This package is "good enough" for a lot of tasks. But it quickly falls over. For example, if you declare an @abc.abstractproperty, you must implement that attribute with a @property or the class compile time checking performed by abc will complain. This often forces you to implement dumb @property wrappers to return a _ prefixed attribute of the sane name. That's ugly. I've also wanted to implement automated checking that classes conform to various interfaces and don't expose other "public" attributes. After doing a bit of research and asking around, the general consensus seems to be that zope.interface is the best package for doing interface-based programming in Python. It has built-in support for verifying classes and objects conform to interfaces. It allows an interface's properties to be defined during __init__. There's even an "adapter registry" that allow you to register interfaces and look up which classes implement them. That could potentially be useful for places where our custom registry.py modules currently facilitates central registrations, but at a type level. Imagine extensions providing alternate implementations of things like the local repository interface to allow opening repositories with custom requirements. Anyway, this commit vendors zope.interface 4.4.3. The contents of the source tarball have been copied into mercurial/thirdparty/zope/ without modifications. Test modules have been removed because they are not interesting to us. The LICENSE.txt file has been copied so it lives next to the source. The Python modules don't use relative imports. zope/__init__.py defines a namespace package. So we'll need to modify the source code before this package is usable inside Mercurial. This will be done in subsequent commits. # no-check-commit for various style failures Differential Revision: https://phab.mercurial-scm.org/D2928
author Gregory Szorc <gregory.szorc@gmail.com>
date Wed, 21 Mar 2018 19:48:50 -0700
parents
children 68ee61822182
comparison
equal deleted inserted replaced
37175:fbe34945220d 37176:943d77fc07a3
1 ##############################################################################
2 #
3 # Copyright (c) 2001, 2002 Zope Foundation and Contributors.
4 # All Rights Reserved.
5 #
6 # This software is subject to the provisions of the Zope Public License,
7 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
8 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11 # FOR A PARTICULAR PURPOSE.
12 #
13 ##############################################################################
14 """Mapping Interfaces
15 """
16 from zope.interface import Interface
17
18 class IItemMapping(Interface):
19 """Simplest readable mapping object
20 """
21
22 def __getitem__(key):
23 """Get a value for a key
24
25 A KeyError is raised if there is no value for the key.
26 """
27
28
29 class IReadMapping(IItemMapping):
30 """Basic mapping interface
31 """
32
33 def get(key, default=None):
34 """Get a value for a key
35
36 The default is returned if there is no value for the key.
37 """
38
39 def __contains__(key):
40 """Tell if a key exists in the mapping."""
41
42
43 class IWriteMapping(Interface):
44 """Mapping methods for changing data"""
45
46 def __delitem__(key):
47 """Delete a value from the mapping using the key."""
48
49 def __setitem__(key, value):
50 """Set a new item in the mapping."""
51
52
53 class IEnumerableMapping(IReadMapping):
54 """Mapping objects whose items can be enumerated.
55 """
56
57 def keys():
58 """Return the keys of the mapping object.
59 """
60
61 def __iter__():
62 """Return an iterator for the keys of the mapping object.
63 """
64
65 def values():
66 """Return the values of the mapping object.
67 """
68
69 def items():
70 """Return the items of the mapping object.
71 """
72
73 def __len__():
74 """Return the number of items.
75 """
76
77 class IMapping(IWriteMapping, IEnumerableMapping):
78 ''' Simple mapping interface '''
79
80 class IIterableMapping(IEnumerableMapping):
81
82 def iterkeys():
83 "iterate over keys; equivalent to __iter__"
84
85 def itervalues():
86 "iterate over values"
87
88 def iteritems():
89 "iterate over items"
90
91 class IClonableMapping(Interface):
92
93 def copy():
94 "return copy of dict"
95
96 class IExtendedReadMapping(IIterableMapping):
97
98 def has_key(key):
99 """Tell if a key exists in the mapping; equivalent to __contains__"""
100
101 class IExtendedWriteMapping(IWriteMapping):
102
103 def clear():
104 "delete all items"
105
106 def update(d):
107 " Update D from E: for k in E.keys(): D[k] = E[k]"
108
109 def setdefault(key, default=None):
110 "D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D"
111
112 def pop(k, *args):
113 """remove specified key and return the corresponding value
114 *args may contain a single default value, or may not be supplied.
115 If key is not found, default is returned if given, otherwise
116 KeyError is raised"""
117
118 def popitem():
119 """remove and return some (key, value) pair as a
120 2-tuple; but raise KeyError if mapping is empty"""
121
122 class IFullMapping(
123 IExtendedReadMapping, IExtendedWriteMapping, IClonableMapping, IMapping):
124 ''' Full mapping interface ''' # IMapping included so tests for IMapping
125 # succeed with IFullMapping