]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/__init__.py
first pass at a setup.py
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / __init__.py
1 """Module providing runtime utility objects to support the Micro/CircuitPython api"""
2
3
4 class Enum(object):
5     """
6         Object supporting CircuitPython-style of static symbols
7         as seen with Direction.OUTPUT, Pull.UP
8     """
9
10     def __repr__(self):
11         """
12         Assumes instance will be found as attribute of own class.
13         Returns dot-subscripted path to instance
14         (assuming absolute import of containing package)
15         """
16         cls = type(self)
17         for key in dir(cls):
18             if getattr(cls, key) is self:
19                 return "{}.{}.{}".format(cls.__module__, cls.__qualname__, key)
20         return repr(self)
21
22     @classmethod
23     def iteritems(cls):
24         """
25             Inspects attributes of the class for instances of the class
26             and returns as key,value pairs mirroring dict#iteritems
27         """
28         for key in dir(cls):
29             val = getattr(cls, key)
30             if type(val) is cls:
31                 yield (key, val)
32
33
34 class ContextManaged:
35     def __enter__(self):
36         return self
37
38     def __exit__(self, exc_type, exc_value, traceback):
39         self.deinit()
40
41
42 class Lockable(ContextManaged):
43     _locked = False
44
45     def try_lock(self):
46         if self._locked:
47             return False
48         else:
49             self._locked = True
50             return True
51
52     def unlock(self):
53         if self._locked:
54             self._locked = False
55         else:
56             raise ValueError("Not locked")
57
58 def patch_system():
59     import sys
60     from adafruit_blinka.agnostic import time
61     sys.modules['time'] = time