##// END OF EJS Templates
extras: re-use Projection from jaraco.collections
Jason R. Coombs -
r51261:82e5a9b1 default
parent child Browse files
Show More
1 NO CONTENT: new file 100644
NO CONTENT: new file 100644
@@ -0,0 +1,56 b''
1 # adapted from jaraco.collections 3.9
2
3 import collections
4
5
6 class Projection(collections.abc.Mapping):
7 """
8 Project a set of keys over a mapping
9
10 >>> sample = {'a': 1, 'b': 2, 'c': 3}
11 >>> prj = Projection(['a', 'c', 'd'], sample)
12 >>> prj == {'a': 1, 'c': 3}
13 True
14
15 Keys should only appear if they were specified and exist in the space.
16
17 >>> sorted(list(prj.keys()))
18 ['a', 'c']
19
20 Attempting to access a key not in the projection
21 results in a KeyError.
22
23 >>> prj['b']
24 Traceback (most recent call last):
25 ...
26 KeyError: 'b'
27
28 Use the projection to update another dict.
29
30 >>> target = {'a': 2, 'b': 2}
31 >>> target.update(prj)
32 >>> target == {'a': 1, 'b': 2, 'c': 3}
33 True
34
35 Also note that Projection keeps a reference to the original dict, so
36 if you modify the original dict, that could modify the Projection.
37
38 >>> del sample['a']
39 >>> dict(prj)
40 {'c': 3}
41 """
42
43 def __init__(self, keys, space):
44 self._keys = tuple(keys)
45 self._space = space
46
47 def __getitem__(self, key):
48 if key not in self._keys:
49 raise KeyError(key)
50 return self._space[key]
51
52 def __iter__(self):
53 return iter(set(self._keys).intersection(self._space))
54
55 def __len__(self):
56 return len(tuple(iter(self)))
@@ -44,6 +44,7 b' allowsymbolimports = ('
44 # third-party imports should be directly imported
44 # third-party imports should be directly imported
45 'mercurial.thirdparty',
45 'mercurial.thirdparty',
46 'mercurial.thirdparty.attr',
46 'mercurial.thirdparty.attr',
47 'mercurial.thirdparty.jaraco.collections',
47 'mercurial.thirdparty.zope',
48 'mercurial.thirdparty.zope',
48 'mercurial.thirdparty.zope.interface',
49 'mercurial.thirdparty.zope.interface',
49 'typing',
50 'typing',
@@ -24,6 +24,7 b' from mercurial.node import ('
24 wdirrev,
24 wdirrev,
25 )
25 )
26 from mercurial.pycompat import open
26 from mercurial.pycompat import open
27 from mercurial.thirdparty.jaraco.collections import Projection
27 from mercurial import (
28 from mercurial import (
28 bookmarks,
29 bookmarks,
29 cmdutil,
30 cmdutil,
@@ -52,6 +53,7 b' from mercurial import ('
52 util,
53 util,
53 )
54 )
54
55
56
55 # The following constants are used throughout the rebase module. The ordering of
57 # The following constants are used throughout the rebase module. The ordering of
56 # their values must be maintained.
58 # their values must be maintained.
57
59
@@ -93,19 +95,8 b' def retained_extras():'
93 yield b'intermediate-source'
95 yield b'intermediate-source'
94
96
95
97
96 def _project(orig, names):
97 """Project a subset of names from orig."""
98 names_saved = tuple(names)
99 values = (orig.get(name, None) for name in names_saved)
100 return {
101 name: value
102 for name, value in zip(names_saved, values)
103 if value is not None
104 }
105
106
107 def _save_extras(ctx, extra):
98 def _save_extras(ctx, extra):
108 extra.update(_project(ctx.extra(), retained_extras()))
99 extra.update(Projection(retained_extras(), ctx.extra()))
109
100
110
101
111 def _savebranch(ctx, extra):
102 def _savebranch(ctx, extra):
@@ -1302,6 +1302,7 b' packages = ['
1302 'mercurial.templates',
1302 'mercurial.templates',
1303 'mercurial.thirdparty',
1303 'mercurial.thirdparty',
1304 'mercurial.thirdparty.attr',
1304 'mercurial.thirdparty.attr',
1305 'mercurial.thirdparty.jaraco',
1305 'mercurial.thirdparty.zope',
1306 'mercurial.thirdparty.zope',
1306 'mercurial.thirdparty.zope.interface',
1307 'mercurial.thirdparty.zope.interface',
1307 'mercurial.upgrade_utils',
1308 'mercurial.upgrade_utils',
General Comments 0
You need to be logged in to leave comments. Login now