]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/__init__.py
Merge pull request #124 from Groboards/master
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / __init__.py
1 """
2 `adafruit_blinka` - Runtime utility objects for re-implementation of CircuitPython API
3 ======================================================================================
4
5 * Author(s): cefn
6 """
7
8 class Enum():
9     """
10         Object supporting CircuitPython-style of static symbols
11         as seen with Direction.OUTPUT, Pull.UP
12     """
13
14     def __repr__(self):
15         """
16         Assumes instance will be found as attribute of own class.
17         Returns dot-subscripted path to instance
18         (assuming absolute import of containing package)
19         """
20         cls = type(self)
21         for key in dir(cls):
22             if getattr(cls, key) is self:
23                 return "{}.{}.{}".format(cls.__module__, cls.__qualname__, key)
24         return repr(self)
25
26     @classmethod
27     def iteritems(cls):
28         """
29             Inspects attributes of the class for instances of the class
30             and returns as key,value pairs mirroring dict#iteritems
31         """
32         for key in dir(cls):
33             val = getattr(cls, key)
34             if isinstance(cls, val):
35                 yield (key, val)
36
37
38 class ContextManaged:
39     """An object that automatically deinitializes hardware with a context manager."""
40     def __enter__(self):
41         return self
42
43     def __exit__(self, exc_type, exc_value, traceback):
44         self.deinit()
45
46     # pylint: disable=no-self-use
47     def deinit(self):
48         """Free any hardware used by the object."""
49         return
50     # pylint: enable=no-self-use
51
52
53 class Lockable(ContextManaged):
54     """An object that must be locked to prevent collisions on a microcontroller resource."""
55     _locked = False
56
57     def try_lock(self):
58         """Attempt to grab the lock. Return True on success, False if the lock is already taken."""
59         if self._locked:
60             return False
61         self._locked = True
62         return True
63
64     def unlock(self):
65         """Release the lock so others may use the resource."""
66         if self._locked:
67             self._locked = False
68         else:
69             raise ValueError("Not locked")
70
71 def patch_system():
72     """Patch modules that may be different due to the platform."""
73     import sys
74     from adafruit_blinka.agnostic import time
75     sys.modules['time'] = time