summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: c6be9c5)
raw | patch | inline | side by side (parent: c6be9c5)
author | Ralf Ramsauer <ralf.ramsauer@oth-regensburg.de> | |
Thu, 9 May 2019 16:52:29 +0000 (18:52 +0200) | ||
committer | Jan Kiszka <jan.kiszka@siemens.com> | |
Sun, 12 May 2019 08:01:00 +0000 (10:01 +0200) |
Pythons Enums have the restriction that they only allow instances of a
with qualified known values. Unknown values are not supported.
In case of PCI capabilities, there might be IDs that do not have
speaking names. In this case, we should use the raw representation.
This helper class provides similar features to Python's enums, but is
specialised for generating C definiton-like things.
For very easy usage in code, I want this 'Enum'-like type to be directly
accessible via attributes. This is generally no problem, but we need to make a
tiny rain dance in order to support both, python2 and python3. The
with_metaclass decorator can be removed once Python 2 is EOL or we decide to
only support Python3.
Signed-off-by: Ralf Ramsauer <ralf.ramsauer@oth-regensburg.de>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
with qualified known values. Unknown values are not supported.
In case of PCI capabilities, there might be IDs that do not have
speaking names. In this case, we should use the raw representation.
This helper class provides similar features to Python's enums, but is
specialised for generating C definiton-like things.
For very easy usage in code, I want this 'Enum'-like type to be directly
accessible via attributes. This is generally no problem, but we need to make a
tiny rain dance in order to support both, python2 and python3. The
with_metaclass decorator can be removed once Python 2 is EOL or we decide to
only support Python3.
Signed-off-by: Ralf Ramsauer <ralf.ramsauer@oth-regensburg.de>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
pyjailhouse/extendedenum.py | [new file with mode: 0644] | patch | blob |
diff --git a/pyjailhouse/extendedenum.py b/pyjailhouse/extendedenum.py
--- /dev/null
@@ -0,0 +1,46 @@
+#
+# Jailhouse, a Linux-based partitioning hypervisor
+#
+# Copyright (c) OTH Regensburg, 2019
+#
+# Authors:
+# Ralf Ramsauer <ralf.ramsauer@oth-regensburg.de>
+#
+# This work is licensed under the terms of the GNU GPL, version 2. See
+# the COPYING file in the top-level directory.
+
+# Python 2 and 3 have different ways of handling metaclasses. This decorator
+# is a support layer for both and can be removed once Python 2 is no longer
+# supported.
+def with_metaclass(meta):
+ def decorator(cls):
+ body = vars(cls).copy()
+ body.pop('__dict__', None)
+ body.pop('__weakref__', None)
+ return meta(cls.__name__, cls.__bases__, body)
+ return decorator
+
+
+class ExtendedEnumMeta(type):
+ def __getattr__(cls, key):
+ return cls(cls._ids[key])
+
+
+@with_metaclass(ExtendedEnumMeta)
+class ExtendedEnum:
+ def __init__(self, value):
+ self.value = value
+
+ def __str__(self):
+ for key, value in self._ids.items():
+ if value == self.value:
+ return '%s_%s' % (self.__class__.__name__, key)
+
+ return '0x%x' % self.value
+
+ def __eq__(self, other):
+ if isinstance(other, self.__class__):
+ return self.value == other.value
+ elif isinstance(other, int):
+ return self.value == other
+ return False