X-Git-Url: https://git.ayoreis.com/hackapet/Adafruit_Blinka.git/blobdiff_plain/48abd1d4f506138aaae9e514bd063d870f5e1904..a8a160c837be23b17fd159a270edf7c4a6304b88:/python/mcp/__init__.py diff --git a/python/mcp/__init__.py b/python/mcp/__init__.py index a8c4a84..0e52a3c 100644 --- a/python/mcp/__init__.py +++ b/python/mcp/__init__.py @@ -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,20 +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) +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 -class Pin(Enum): - def __init__(self, id): - """ id type is likely platform-specific""" - self.id = id - pass + def unlock(self): + if self._locked: + self._locked = False + else: + raise ValueError("Not locked")