-
Notifications
You must be signed in to change notification settings - Fork 140
/
device.py
145 lines (110 loc) · 4.28 KB
/
device.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""Contains the Device base class."""
import abc
from typing import List
from typing import TYPE_CHECKING
from mpf.core.machine import MachineController
from mpf.core.logging import LogMixin
if TYPE_CHECKING:
from mpf.core.config_validator import ConfigDict
from mpf.core.platform import BasePlatform
class Device(LogMixin, metaclass=abc.ABCMeta):
"""Generic parent class of for every hardware device in a pinball machine."""
# String of the config section name
config_section = None # type: str
# String name of the collection
collection = None # type: str
# String of the friendly name of the device class
class_label = None # type: str
# Can a config for this device be empty?
allow_empty_configs = False
def __init__(self, machine: MachineController, name: str) -> None:
"""Set up default attributes of every device.
Args:
machine: The machine controller.
name: Name of the device in config.
"""
super().__init__()
self.machine = machine
self.name = name.lower()
self.tags = [] # type: List[str]
self.label = None # type: str
self.platform = None # type: BasePlatform
self.config = dict() # type: ConfigDict
@classmethod
def get_config_spec(cls):
"""Return config spec for this device."""
return False
def load_platform_section(self, platform_section: str):
"""Can be called in _initialize to load the platform section.
Args:
platform_section: Name of the platform section.
"""
self.platform = self.machine.get_platform_sections(platform_section, self.config['platform'])
@classmethod
def prepare_config(cls, config: dict, is_mode_config: bool) -> dict:
"""Return the config prepared for validation.
Args:
config: Config of device
is_mode_config: Whether this device is loaded in a mode or system-wide
Returns: Prepared config
"""
del is_mode_config
return config
def validate_and_parse_config(self, config: dict, is_mode_config: bool) -> dict:
"""Return the parsed and validated config.
Args:
config: Config of device
is_mode_config: Whether this device is loaded in a mode or system-wide
Returns: Validated config
"""
del is_mode_config
self.machine.config_validator.validate_config(
self.config_section, config, self.name, "device")
self._configure_device_logging(config)
return config
def _configure_device_logging(self, config):
if config['debug']:
config['console_log'] = 'full'
config['file_log'] = 'full'
self.configure_logging(self.class_label + '.' + self.name,
config['console_log'],
config['file_log'])
self.debug_log('Platform Driver: %s', self.platform)
self.debug_log("Configuring device with settings: '%s'", config)
def load_config(self, config: dict):
"""Load config.
Args:
config: Config for device
"""
self.config = config
self.tags = self.config['tags']
self.label = self.config['label']
def __repr__(self):
"""Return string representation."""
return '<{self.class_label}.{self.name}>'.format(self=self)
# def enable_debugging(self):
# """Enable debug logging."""
# self.debug_log("Enabling debug logging")
# self.debug = True
# self._enable_related_device_debugging()
#
# def disable_debugging(self):
# """Disable debug logging."""
# self.debug_log("Disabling debug logging")
# self.debug = False
# self._disable_related_device_debugging()
#
# def _enable_related_device_debugging(self):
# pass
#
# def _disable_related_device_debugging(self):
# pass
@classmethod
def get_config_info(cls):
"""Return config collection and config section.
Returns (str, str): Tuple with (collection, config_section)
"""
return cls.collection, cls.config_section
def _initialize(self):
"""Default initialize method."""
pass