-
Notifications
You must be signed in to change notification settings - Fork 17
/
configurator.py
157 lines (128 loc) · 5.93 KB
/
configurator.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
146
147
148
149
150
151
152
153
154
155
156
157
import argparse
import logging
from preferences_editor import PreferencesEditor
import sys
DEBUG = "debug"
DETECTION_RATE = "detectionrate" #ms
LASER_INTENSITY = "laserintensity"
MARKER_RADIUS = "markerradius"
IGNORE_LASER_COLOR = "ignorelasercolor"
USE_VIRTUAL_MAGAZINE = "usevirtualmagazine"
VIRTUAL_MAGAZINE = "virtualmagazine"
USE_MALFUNCTIONS = "usemalfunctions"
MALFUNCTION_PROBABILITY = "malfunctionprobability"
VIDCAM = "vidcam" # first detected == 0
class Configurator():
def _check_rate(self, rate):
value = int(rate)
if value < 1:
raise argparse.ArgumentTypeError("DETECTION_RATE must be a number " +
"greater than 0")
return value
def _check_intensity(self, intensity):
value = int(intensity)
if value < 1 or value > 255:
raise argparse.ArgumentTypeError("LASER_INTENSITY must be a number " +
"between 1 and 255")
return value
def _check_radius(self, radius):
value = int(radius)
if value < 1 or value > 20:
raise argparse.ArgumentTypeError("MARKER_RADIUS must be a number " +
"between 1 and 20")
return value
def _check_vidcam(self, vidcam):
value = int(vidcam)
if value < 0 or value > 2:
raise argparse.ArgumentTypeError("VIDCAM must be a number " +
"between 0 and 2")
return value
def _check_ignore_laser_color(self, ignore_laser_color):
ignore_laser_color = ignore_laser_color.lower()
if ignore_laser_color != "red" and ignore_laser_color != "green":
raise argparse.ArgumentTypeError("IGNORE_LASER_COLOR must be a string " +
"equal to either \"green\" or \"red\" without quotes")
return ignore_laser_color
def _check_virtual_magazine(self, virtual_magazine):
value = int(virtual_magazine)
if value < 1 or value > 45:
raise argparse.ArgumentTypeError("VIRTUAL_MAGAZINE must be a number " +
"between 1 and 45")
return value
def _check_malfunctions(self, malfunctions):
value = float(malfunctions)
if value < .1 or value > 99.9:
raise argparse.ArgumentTypeError("MALFUNCTIONS_PROBABILITY must be a number " +
"between .1 and 99.9")
return value
def __init__(self):
self._logger = None
# Load configuration information from the config file, which will
# be over-ridden if settings are set on the command line
config, preferences = PreferencesEditor.map_configuration()
# Parse command line arguments
parser = argparse.ArgumentParser(prog="shootoff.py")
parser.add_argument("-d", "--debug", action="store_true",
help="turn on debug log messages")
parser.add_argument("-r", "--detection-rate", type=self._check_rate,
help="sets the rate at which shots are detected in milliseconds. " +
"this should be set to about the length of time your laser trainer " +
"stays on for each shot, typically about 100 ms")
parser.add_argument("-i", "--laser-intensity", type=self._check_intensity,
help="sets the intensity threshold for detecting the laser [1,255]. " +
"this should be as high as you can set it while still detecting " +
"shots")
parser.add_argument("-m", "--marker-radius", type=self._check_radius,
help="sets the radius of shot markers in pixels [1,20]")
parser.add_argument("-v", "--vidcam", type=self._check_vidcam,
help="sets video camera to use [0,2]")
parser.add_argument("-c", "--ignore-laser-color",
type=self._check_ignore_laser_color,
help="sets the color of laser that should be ignored by ShootOFF (green " +
"or red). No color is ignored by default")
parser.add_argument("-u", "--use-virtual-magazine",
type=self._check_virtual_magazine,
help="turns on the virtual magazine and sets the number rounds it holds")
parser.add_argument("-f", "--use-malfunctions",
type=self._check_malfunctions,
help="turns on malfunctions and sets the probability of them happening")
args = parser.parse_args()
preferences[DEBUG] = args.debug
if args.detection_rate:
preferences[DETECTION_RATE] = int(args.detection_rate)
if args.laser_intensity:
preferences[LASER_INTENSITY] = int(args.laser_intensity)
if args.marker_radius:
preferences[MARKER_RADIUS] = int(args.marker_radius)
if args.vidcam >= 0:
preferences[VIDCAM] = int(args.vidcam)
if args.ignore_laser_color:
preferences[IGNORE_LASER_COLOR] = args.ignore_laser_color
if args.use_virtual_magazine:
preferences[USE_VIRTUAL_MAGAZINE] = True
preferences[VIRTUAL_MAGAZINE] = int(args.use_virtual_magazine)
if args.use_malfunctions:
preferences[USE_MALFUNCTIONS] = True
preferences[MALFUNCTION_PROBABILITY] = float(args.use_malfunctions)
self._preferences = preferences
self._config_parser = config
def get_preferences(self):
return self._preferences
def get_config_parser(self):
return self._config_parser
def get_logger(self):
if self._logger is None:
self._logger = self._make_logger()
return self._logger
def _make_logger(self):
logger = logging.getLogger('shootoff')
stdhandler = logging.StreamHandler(sys.stdout)
if self._preferences[DEBUG]:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.INFO)
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
stdhandler.setFormatter(formatter)
logger.addHandler(stdhandler)
return logger