]> Repositories - hackapet/Adafruit_Blinka.git/blobdiff - src/adafruit_blinka/__init__.py
Agnostic and wrapper-specific classes now under adafruit_blinka
[hackapet/Adafruit_Blinka.git] / src / adafruit_blinka / __init__.py
diff --git a/src/adafruit_blinka/__init__.py b/src/adafruit_blinka/__init__.py
new file mode 100644 (file)
index 0000000..0e52a3c
--- /dev/null
@@ -0,0 +1,54 @@
+"""Module providing runtime utility objects to support the Micro/CircuitPython api"""
+
+class Enum(object):
+    """
+        Object supporting CircuitPython-style of static symbols
+        as seen with Direction.OUTPUT, Pull.UP
+    """
+
+    def __repr__(self):
+        """
+        Assumes instance will be found as attribute of own class.
+        Returns dot-subscripted path to instance
+        (assuming absolute import of containing package)
+        """
+        cls = type(self)
+        for key in dir(cls):
+            if getattr(cls, key) is self:
+                return "{}.{}.{}".format(cls.__module__, cls.__qualname__, key)
+        return repr(self)
+
+    @classmethod
+    def iteritems(cls):
+        """
+            Inspects attributes of the class for instances of the class
+            and returns as key,value pairs mirroring dict#iteritems
+        """
+        for key in dir(cls):
+            val = getattr(cls, key)
+            if type(val) is cls:
+                yield (key, val)
+
+
+class ContextManaged:
+    def __enter__(self):
+        return self
+
+    def __exit__(self, exc_type, exc_value, traceback):
+        self.deinit()
+
+def Lockable(ContextManaged):
+    _locked = False
+
+    def try_lock(self):
+        if self._locked:
+            return False
+        else:
+            self._locked=True
+            return True
+
+    def unlock(self):
+        if self._locked:
+            self._locked = False
+        else:
+            raise ValueError("Not locked")