]> Repositories - hackapet/Adafruit_Blinka.git/blobdiff - python/mcp/__init__.py
Top level package now defines only uart and spi ports. pin submodule handles pins
[hackapet/Adafruit_Blinka.git] / python / mcp / __init__.py
index c29025003703c598939431b24f8836b024e5a280..0e52a3cf9a2b10317ca204d582d21b32541c40aa 100644 (file)
@@ -6,6 +6,18 @@ class Enum(object):
         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):
         """
@@ -18,14 +30,25 @@ class Enum(object):
                 yield (key, val)
 
 
-    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)
\ No newline at end of file
+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")