summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHelmut Grohne <helmut@subdivi.de>2025-08-13 11:11:53 +0200
committerHelmut Grohne <helmut@subdivi.de>2025-08-13 11:11:53 +0200
commit8019ecd16ca30afbc52045643fa0aa092ead0e6f (patch)
tree5206635fc66564bb841f267fac05f82641140c72
parente6d2aa979cea1a4d98f588a66b01d742f685971a (diff)
downloadpython-linuxnamespaces-8019ecd16ca30afbc52045643fa0aa092ead0e6f.tar.gz
support slicing an IDMapping
-rw-r--r--linuxnamespaces/idmap.py15
1 files changed, 15 insertions, 0 deletions
diff --git a/linuxnamespaces/idmap.py b/linuxnamespaces/idmap.py
index f66456d..edd823c 100644
--- a/linuxnamespaces/idmap.py
+++ b/linuxnamespaces/idmap.py
@@ -74,6 +74,21 @@ class IDMapping:
"""Construct an identity mapping for the given identifier."""
return cls(idn, idn, count)
+ def __getitem__(self, key: int | slice) -> typing.Self:
+ """Return an entry or slice of the allocated mapping."""
+ if isinstance(key, int):
+ key = slice(key, None, None)
+ if not isinstance(key, slice):
+ raise TypeError("slicing an IDMapping requires an int or slice")
+ start, stop, step = key.indices(self.count)
+ if step != 1:
+ raise ValueError("IDMapping cannot slice non-trivial steps")
+ if start >= stop:
+ raise ValueError("slicing IDMapping resulted in empty mapping")
+ return self.__class__(
+ self.innerstart + start, self.outerstart + start, stop - start
+ )
+
class IDAllocation:
"""This represents a subset of IDs (user or group). It can be used to