Skip to content

Commit 76769dc

Browse files
committed
Ran black, updated to pylint 2.x
1 parent 22140c0 commit 76769dc

File tree

4 files changed

+196
-137
lines changed

4 files changed

+196
-137
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
source actions-ci/install.sh
4141
- name: Pip install pylint, black, & Sphinx
4242
run: |
43-
pip install --force-reinstall pylint==1.9.2 black==19.10b0 Sphinx sphinx-rtd-theme
43+
pip install pylint black==19.10b0 Sphinx sphinx-rtd-theme
4444
- name: Library version
4545
run: git describe --dirty --always --tags
4646
- name: PyLint

adafruit_pyoa.py

Lines changed: 107 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
https://github.com/adafruit/circuitpython/releases
4343
"""
4444

45-
#pylint: disable=too-many-instance-attributes,no-self-use,line-too-long
45+
# pylint: disable=too-many-instance-attributes,no-self-use,line-too-long
4646

4747
# imports
4848
import time
@@ -51,7 +51,8 @@
5151
from digitalio import DigitalInOut
5252
import displayio
5353
import adafruit_touchscreen
54-
try: # No need for Cursor Control on the PyPortal
54+
55+
try: # No need for Cursor Control on the PyPortal
5556
from adafruit_cursorcontrol.cursorcontrol import Cursor
5657
from adafruit_cursorcontrol.cursorcontrol_cursormanager import CursorManager
5758
except ImportError:
@@ -64,7 +65,8 @@
6465
__version__ = "0.0.0-auto.0"
6566
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PYOA.git"
6667

67-
class PYOA_Graphics():
68+
69+
class PYOA_Graphics:
6870
"""A choose your own adventure game framework."""
6971

7072
def __init__(self):
@@ -83,12 +85,12 @@ def __init__(self):
8385

8486
self._speaker_enable = DigitalInOut(board.SPEAKER_ENABLE)
8587
self._speaker_enable.switch_to_output(False)
86-
if hasattr(board, 'AUDIO_OUT'):
88+
if hasattr(board, "AUDIO_OUT"):
8789
self.audio = audioio.AudioOut(board.AUDIO_OUT)
88-
elif hasattr(board, 'SPEAKER'):
90+
elif hasattr(board, "SPEAKER"):
8991
self.audio = audioio.AudioOut(board.SPEAKER)
9092
else:
91-
raise AttributeError('Board does not have an audio output!')
93+
raise AttributeError("Board does not have an audio output!")
9294

9395
self._background_file = None
9496
self._wavfile = None
@@ -98,18 +100,22 @@ def __init__(self):
98100
self._display.show(self.root_group)
99101
self.touchscreen = None
100102
self.mouse_cursor = None
101-
if hasattr(board, 'TOUCH_XL'):
102-
self.touchscreen = adafruit_touchscreen.Touchscreen(board.TOUCH_XL, board.TOUCH_XR,
103-
board.TOUCH_YD, board.TOUCH_YU,
104-
calibration=((5200, 59000),
105-
(5800, 57000)),
106-
size=(self._display.width,
107-
self._display.height))
108-
elif hasattr(board, 'BUTTON_CLOCK'):
109-
self.mouse_cursor = Cursor(self._display, display_group=self.root_group, cursor_speed=8)
103+
if hasattr(board, "TOUCH_XL"):
104+
self.touchscreen = adafruit_touchscreen.Touchscreen(
105+
board.TOUCH_XL,
106+
board.TOUCH_XR,
107+
board.TOUCH_YD,
108+
board.TOUCH_YU,
109+
calibration=((5200, 59000), (5800, 57000)),
110+
size=(self._display.width, self._display.height),
111+
)
112+
elif hasattr(board, "BUTTON_CLOCK"):
113+
self.mouse_cursor = Cursor(
114+
self._display, display_group=self.root_group, cursor_speed=8
115+
)
110116
self.cursor = CursorManager(self.mouse_cursor)
111117
else:
112-
raise AttributeError('PYOA requires a touchscreen or cursor.')
118+
raise AttributeError("PYOA requires a touchscreen or cursor.")
113119
self._gamedirectory = None
114120
self._gamefilename = None
115121
self._game = None
@@ -120,7 +126,6 @@ def __init__(self):
120126
self._right_button = None
121127
self._middle_button = None
122128

123-
124129
def load_game(self, game_directory):
125130
"""Load a game.
126131
@@ -136,8 +141,8 @@ def load_game(self, game_directory):
136141

137142
# Button Attributes
138143
btn_left = 10
139-
btn_right = btn_left+180
140-
btn_mid = btn_left+90
144+
btn_right = btn_left + 180
145+
btn_mid = btn_left + 90
141146
button_y = 195
142147
button_width = 120
143148
button_height = 40
@@ -149,26 +154,44 @@ def load_game(self, game_directory):
149154
btn_right /= 2
150155
btn_mid /= 2
151156
elif self._display.height > 250:
152-
button_y *= .75
157+
button_y *= 0.75
153158
button_y -= 20
154-
button_width *= .75
155-
button_height *= .75
156-
btn_right *= .75
157-
btn_mid *= .75
158-
self._left_button = Button(x=int(btn_left), y=int(button_y), width=int(button_width), height=int(button_height),
159-
label="Left", label_font=self._text_font,
160-
style=Button.SHADOWROUNDRECT)
161-
self._right_button = Button(x=int(btn_right), y=int(button_y), width=int(button_width), height=int(button_height),
162-
label="Right", label_font=self._text_font,
163-
style=Button.SHADOWROUNDRECT)
164-
self._middle_button = Button(x=int(btn_mid), y=int(button_y), width=int(button_width), height=int(button_height),
165-
label="Middle", label_font=self._text_font,
166-
style=Button.SHADOWROUNDRECT)
167-
self._gamefilename = game_directory+"/cyoa.json"
159+
button_width *= 0.75
160+
button_height *= 0.75
161+
btn_right *= 0.75
162+
btn_mid *= 0.75
163+
self._left_button = Button(
164+
x=int(btn_left),
165+
y=int(button_y),
166+
width=int(button_width),
167+
height=int(button_height),
168+
label="Left",
169+
label_font=self._text_font,
170+
style=Button.SHADOWROUNDRECT,
171+
)
172+
self._right_button = Button(
173+
x=int(btn_right),
174+
y=int(button_y),
175+
width=int(button_width),
176+
height=int(button_height),
177+
label="Right",
178+
label_font=self._text_font,
179+
style=Button.SHADOWROUNDRECT,
180+
)
181+
self._middle_button = Button(
182+
x=int(btn_mid),
183+
y=int(button_y),
184+
width=int(button_width),
185+
height=int(button_height),
186+
label="Middle",
187+
label_font=self._text_font,
188+
style=Button.SHADOWROUNDRECT,
189+
)
190+
self._gamefilename = game_directory + "/cyoa.json"
168191
try:
169192
game_file = open(self._gamefilename, "r")
170193
except OSError:
171-
raise OSError("Could not open game file "+self._gamefilename)
194+
raise OSError("Could not open game file " + self._gamefilename)
172195
self._game = json.load(game_file)
173196
game_file.close()
174197

@@ -190,8 +213,8 @@ def _display_buttons(self, card):
190213
191214
:param card: The active card
192215
"""
193-
button01_text = card.get('button01_text', None)
194-
button02_text = card.get('button02_text', None)
216+
button01_text = card.get("button01_text", None)
217+
button02_text = card.get("button02_text", None)
195218
self._left_button.label = button01_text
196219
self._middle_button.label = button01_text
197220
self._right_button.label = button02_text
@@ -207,18 +230,18 @@ def _display_background_for(self, card):
207230
208231
:param card: The active card
209232
"""
210-
self.set_background(card.get('background_image', None), with_fade=False)
233+
self.set_background(card.get("background_image", None), with_fade=False)
211234

212235
def _display_text_for(self, card):
213236
"""Display the main text of a card.
214237
215238
:param card: The active card
216239
"""
217-
text = card.get('text', None)
218-
text_color = card.get('text_color', 0x0) # default to black
240+
text = card.get("text", None)
241+
text_color = card.get("text_color", 0x0) # default to black
219242
if text:
220243
try:
221-
text_color = int(text_color) # parse the JSON string to hex int
244+
text_color = int(text_color) # parse the JSON string to hex int
222245
except ValueError:
223246
text_color = 0x0
224247
self.set_text(text, text_color)
@@ -228,8 +251,8 @@ def _play_sound_for(self, card):
228251
229252
:param card: The active card
230253
"""
231-
sound = card.get('sound', None)
232-
loop = card.get('sound_repeat', False)
254+
sound = card.get("sound", None)
255+
loop = card.get("sound_repeat", False)
233256
if sound:
234257
loop = loop == "True"
235258
print("Loop:", loop)
@@ -242,8 +265,8 @@ def _wait_for_press(self, card):
242265
243266
Return the id of the destination card.
244267
"""
245-
button01_text = card.get('button01_text', None)
246-
button02_text = card.get('button02_text', None)
268+
button01_text = card.get("button01_text", None)
269+
button02_text = card.get("button02_text", None)
247270
point_touched = None
248271
while True:
249272
if self.touchscreen is not None:
@@ -253,21 +276,23 @@ def _wait_for_press(self, card):
253276
if self.cursor.is_clicked is True:
254277
point_touched = self.mouse_cursor.x, self.mouse_cursor.y
255278
if point_touched is not None:
256-
point_touched = (point_touched[0] // self._button_group.scale,
257-
point_touched[1] // self._button_group.scale)
279+
point_touched = (
280+
point_touched[0] // self._button_group.scale,
281+
point_touched[1] // self._button_group.scale,
282+
)
258283
print("touch: ", point_touched)
259284
if button01_text and not button02_text:
260285
# showing only middle button
261286
if self._middle_button.contains(point_touched):
262287
print("Middle button")
263-
return card.get('button01_goto_card_id', None)
288+
return card.get("button01_goto_card_id", None)
264289
if button01_text and button02_text:
265290
if self._left_button.contains(point_touched):
266291
print("Left button")
267-
return card.get('button01_goto_card_id', None)
292+
return card.get("button01_goto_card_id", None)
268293
if self._right_button.contains(point_touched):
269294
print("Right button")
270-
return card.get('button02_goto_card_id', None)
295+
return card.get("button02_goto_card_id", None)
271296
time.sleep(0.1)
272297

273298
def display_card(self, card_num):
@@ -277,9 +302,9 @@ def display_card(self, card_num):
277302
"""
278303
card = self._game[card_num]
279304
print(card)
280-
print("*"*32)
281-
print('****{:^24s}****'.format(card['card_id']))
282-
print("*"*32)
305+
print("*" * 32)
306+
print("****{:^24s}****".format(card["card_id"]))
307+
print("*" * 32)
283308

284309
self._fade_to_black()
285310
self._display_buttons(card)
@@ -294,21 +319,23 @@ def display_card(self, card_num):
294319

295320
self._play_sound_for(card)
296321

297-
auto_adv = card.get('auto_advance', None)
322+
auto_adv = card.get("auto_advance", None)
298323
if auto_adv is not None:
299324
auto_adv = float(auto_adv)
300325
print("Auto advancing after %0.1f seconds" % auto_adv)
301326
time.sleep(auto_adv)
302-
return card_num+1
327+
return card_num + 1
303328

304329
destination_card_id = self._wait_for_press(card)
305330

306331
self.play_sound(None) # stop playing any sounds
307332
for card_number, card_struct in enumerate(self._game):
308-
if card_struct.get('card_id', None) == destination_card_id:
309-
return card_number # found the matching card!
333+
if card_struct.get("card_id", None) == destination_card_id:
334+
return card_number # found the matching card!
310335
# eep if we got here something went wrong
311-
raise RuntimeError("Could not find card with matching 'card_id': ", destination_card_id)
336+
raise RuntimeError(
337+
"Could not find card with matching 'card_id': ", destination_card_id
338+
)
312339

313340
def play_sound(self, filename, *, wait_to_finish=True, loop=False):
314341
"""Play a sound
@@ -324,8 +351,8 @@ def play_sound(self, filename, *, wait_to_finish=True, loop=False):
324351
self._wavfile = None
325352

326353
if not filename:
327-
return # nothing more to do, just stopped
328-
filename = self._gamedirectory+"/"+filename
354+
return # nothing more to do, just stopped
355+
filename = self._gamedirectory + "/" + filename
329356
print("Playing sound", filename)
330357
try:
331358
self._display.refresh(target_frames_per_second=60)
@@ -357,12 +384,12 @@ def set_text(self, text, color):
357384
if self._text_group:
358385
self._text_group.pop()
359386
if not text or not color:
360-
return # nothing to do!
387+
return # nothing to do!
361388
text_wrap = 37
362389
if self._display.height < 130:
363390
text_wrap = 25
364391
text = self.wrap_nicely(text, text_wrap)
365-
text = '\n'.join(text)
392+
text = "\n".join(text)
366393
print("Set text to", text, "with color", hex(color))
367394
text_x = 8
368395
text_y = 95
@@ -392,11 +419,11 @@ def set_background(self, filename, *, with_fade=True):
392419
if filename:
393420
if self._background_file:
394421
self._background_file.close()
395-
self._background_file = open(self._gamedirectory+"/"+filename, "rb")
422+
self._background_file = open(self._gamedirectory + "/" + filename, "rb")
396423
background = displayio.OnDiskBitmap(self._background_file)
397-
self._background_sprite = displayio.TileGrid(background,
398-
pixel_shader=displayio.ColorConverter(),
399-
x=0, y=0)
424+
self._background_sprite = displayio.TileGrid(
425+
background, pixel_shader=displayio.ColorConverter(), x=0, y=0
426+
)
400427
self._background_group.append(self._background_sprite)
401428
if with_fade:
402429
try:
@@ -410,20 +437,19 @@ def backlight_fade(self, to_light):
410437
"""Adjust the TFT backlight. Fade from one value to another
411438
"""
412439
from_light = self._display.brightness
413-
from_light = int(from_light*100)
440+
from_light = int(from_light * 100)
414441
to_light = max(0, min(1.0, to_light))
415-
to_light = int(to_light*100)
442+
to_light = int(to_light * 100)
416443
delta = 1
417444
if from_light > to_light:
418445
delta = -1
419446
for val in range(from_light, to_light, delta):
420-
self._display.brightness = val/100
447+
self._display.brightness = val / 100
421448
time.sleep(0.003)
422-
self._display.brightness = to_light/100
423-
449+
self._display.brightness = to_light / 100
424450

425451
# return a list of lines with wordwrapping
426-
#pylint: disable=invalid-name
452+
# pylint: disable=invalid-name
427453
@staticmethod
428454
def wrap_nicely(string, max_chars):
429455
"""A helper that will return a list of lines with word-break wrapping.
@@ -432,22 +458,22 @@ def wrap_nicely(string, max_chars):
432458
:param int max_chars: The maximum number of characters on a line before wrapping.
433459
434460
"""
435-
#string = string.replace('\n', '').replace('\r', '') # strip confusing newlines
436-
words = string.split(' ')
461+
# string = string.replace('\n', '').replace('\r', '') # strip confusing newlines
462+
words = string.split(" ")
437463
the_lines = []
438464
the_line = ""
439465
for w in words:
440-
if '\n' in w:
441-
w1, w2 = w.split('\n')
442-
the_line += ' '+w1
466+
if "\n" in w:
467+
w1, w2 = w.split("\n")
468+
the_line += " " + w1
443469
the_lines.append(the_line)
444470
the_line = w2
445-
elif len(the_line+' '+w) > max_chars:
471+
elif len(the_line + " " + w) > max_chars:
446472
the_lines.append(the_line)
447-
the_line = ''+w
473+
the_line = "" + w
448474
else:
449-
the_line += ' '+w
450-
if the_line: # last line remaining
475+
the_line += " " + w
476+
if the_line: # last line remaining
451477
the_lines.append(the_line)
452478
# remove first space from first line:
453479
the_lines[0] = the_lines[0][1:]

0 commit comments

Comments
 (0)