comparison mercurial/thirdparty/zope/interface/common/sequence.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 """Sequence Interfaces
15 """
16 __docformat__ = 'restructuredtext'
17 from zope.interface import Interface
18
19 class IMinimalSequence(Interface):
20 """Most basic sequence interface.
21
22 All sequences are iterable. This requires at least one of the
23 following:
24
25 - a `__getitem__()` method that takes a single argument; interger
26 values starting at 0 must be supported, and `IndexError` should
27 be raised for the first index for which there is no value, or
28
29 - an `__iter__()` method that returns an iterator as defined in
30 the Python documentation (http://docs.python.org/lib/typeiter.html).
31
32 """
33
34 def __getitem__(index):
35 """`x.__getitem__(index)` <==> `x[index]`
36
37 Declaring this interface does not specify whether `__getitem__`
38 supports slice objects."""
39
40 class IFiniteSequence(IMinimalSequence):
41
42 def __len__():
43 """`x.__len__()` <==> `len(x)`"""
44
45 class IReadSequence(IFiniteSequence):
46 """read interface shared by tuple and list"""
47
48 def __contains__(item):
49 """`x.__contains__(item)` <==> `item in x`"""
50
51 def __lt__(other):
52 """`x.__lt__(other)` <==> `x < other`"""
53
54 def __le__(other):
55 """`x.__le__(other)` <==> `x <= other`"""
56
57 def __eq__(other):
58 """`x.__eq__(other)` <==> `x == other`"""
59
60 def __ne__(other):
61 """`x.__ne__(other)` <==> `x != other`"""
62
63 def __gt__(other):
64 """`x.__gt__(other)` <==> `x > other`"""
65
66 def __ge__(other):
67 """`x.__ge__(other)` <==> `x >= other`"""
68
69 def __add__(other):
70 """`x.__add__(other)` <==> `x + other`"""
71
72 def __mul__(n):
73 """`x.__mul__(n)` <==> `x * n`"""
74
75 def __rmul__(n):
76 """`x.__rmul__(n)` <==> `n * x`"""
77
78 def __getslice__(i, j):
79 """`x.__getslice__(i, j)` <==> `x[i:j]`
80
81 Use of negative indices is not supported.
82
83 Deprecated since Python 2.0 but still a part of `UserList`.
84 """
85
86 class IExtendedReadSequence(IReadSequence):
87 """Full read interface for lists"""
88
89 def count(item):
90 """Return number of occurrences of value"""
91
92 def index(item, *args):
93 """Return first index of value
94
95 `L.index(value, [start, [stop]])` -> integer"""
96
97 class IUniqueMemberWriteSequence(Interface):
98 """The write contract for a sequence that may enforce unique members"""
99
100 def __setitem__(index, item):
101 """`x.__setitem__(index, item)` <==> `x[index] = item`
102
103 Declaring this interface does not specify whether `__setitem__`
104 supports slice objects.
105 """
106
107 def __delitem__(index):
108 """`x.__delitem__(index)` <==> `del x[index]`
109
110 Declaring this interface does not specify whether `__delitem__`
111 supports slice objects.
112 """
113
114 def __setslice__(i, j, other):
115 """`x.__setslice__(i, j, other)` <==> `x[i:j]=other`
116
117 Use of negative indices is not supported.
118
119 Deprecated since Python 2.0 but still a part of `UserList`.
120 """
121
122 def __delslice__(i, j):
123 """`x.__delslice__(i, j)` <==> `del x[i:j]`
124
125 Use of negative indices is not supported.
126
127 Deprecated since Python 2.0 but still a part of `UserList`.
128 """
129 def __iadd__(y):
130 """`x.__iadd__(y)` <==> `x += y`"""
131
132 def append(item):
133 """Append item to end"""
134
135 def insert(index, item):
136 """Insert item before index"""
137
138 def pop(index=-1):
139 """Remove and return item at index (default last)"""
140
141 def remove(item):
142 """Remove first occurrence of value"""
143
144 def reverse():
145 """Reverse *IN PLACE*"""
146
147 def sort(cmpfunc=None):
148 """Stable sort *IN PLACE*; `cmpfunc(x, y)` -> -1, 0, 1"""
149
150 def extend(iterable):
151 """Extend list by appending elements from the iterable"""
152
153 class IWriteSequence(IUniqueMemberWriteSequence):
154 """Full write contract for sequences"""
155
156 def __imul__(n):
157 """`x.__imul__(n)` <==> `x *= n`"""
158
159 class ISequence(IReadSequence, IWriteSequence):
160 """Full sequence contract"""