]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/__init__.py
Merge pull request #18 from tannewt/rogue_machine
[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(object):
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     def deinit(self):
47         """Free any hardware used by the object."""
48         pass
49
50
51 class Lockable(ContextManaged):
52     """An object that must be locked to prevent collisions on a microcontroller resource."""
53     _locked = False
54
55     def try_lock(self):
56         """Attempt to grab the lock. Return True on success, False if the lock is already taken."""
57         if self._locked:
58             return False
59         self._locked = True
60         return True
61
62     def unlock(self):
63         """Release the lock so others may use the resource."""
64         if self._locked:
65             self._locked = False
66         else:
67             raise ValueError("Not locked")
68
69 def patch_system():
70     """Patch modules that may be different due to the platform."""
71     import sys
72     from adafruit_blinka.agnostic import time
73     sys.modules['time'] = time