Skip to content

Commit 6641f0b

Browse files
authored
Merge pull request #324 from ktemkin/fix_py2_so_we_can_kill_it
host: fix python2 compatibility
2 parents 0e1eaae + b21341a commit 6641f0b

File tree

4 files changed

+30
-34
lines changed

4 files changed

+30
-34
lines changed

host/greatfet/commands/greatfet_uart.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def main():
8989

9090
# Set up a simple argument parser.
9191
# TODO: support configurations such as '8n1'
92-
parser = GreatFETArgumentParser(description="Simple GreatFET UART monitor.")
92+
parser = GreatFETArgumentParser(description="Simple GreatFET UART monitor.", verbose_by_default=True)
9393
parser.add_argument('baud', nargs='?', type=from_eng_notation, default=115200, help="Baud rate; in symbols/second. Defaults to 115200.")
9494
parser.add_argument('-d', '--data', type=int, default=8, help="The number of data bits per frame.")
9595
parser.add_argument('-S', '--stop', type=int, default=1, help="The number of stop bits per frame.")

host/greatfet/interface.py

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,38 +36,36 @@ def run_pirate_commands(self, command_string):
3636
_CHARS_VALID_IN_NUMBER="0123456789abcdefxh"
3737

3838

39-
result = []
40-
commands = list(command_string)
39+
self._result = []
40+
self._commands = list(command_string)
4141

4242
# Simple performance enhancement: we'll gather any consecutive reads/writes and issue
4343
# them as single commands to the GreatFET.
44-
pending_write_data = []
45-
pending_read_length = 0
44+
self._pending_write_data = []
45+
self._pending_read_length = 0
4646

4747

4848
def issue_pending_writes(ends_transaction=False):
4949
""" Issues any writes pending; used when performing a non-write operation."""
50-
nonlocal pending_write_data, result
5150

52-
if not pending_write_data:
51+
if not self._pending_write_data:
5352
return
5453

5554
# Perform all of our pending writes.
56-
result.extend(self._handle_pirate_write(pending_write_data, ends_transaction=ends_transaction))
57-
pending_write_data = []
55+
self._result.extend(self._handle_pirate_write(self._pending_write_data, ends_transaction=ends_transaction))
56+
self._pending_write_data = []
5857

5958

6059
def perform_pending_reads(ends_transaction=False):
6160
""" Issues any writes pending; used when performing a non-write operation."""
62-
nonlocal pending_read_length, result
6361

6462
# If we don't have any pending reads, don't do anything.
65-
if not pending_read_length:
63+
if not self._pending_read_length:
6664
return
6765

6866
# Perform all of our pending reads.
69-
result.extend(self._handle_pirate_read(pending_read_length, ends_transaction=ends_transaction))
70-
pending_read_length = 0
67+
self._result.extend(self._handle_pirate_read(self._pending_read_length, ends_transaction=ends_transaction))
68+
self._pending_read_length = 0
7169

7270

7371
def handle_pending_io(ends_transaction=False):
@@ -83,16 +81,14 @@ def extract_number(char=None):
8381
starts with a number.
8482
"""
8583

86-
nonlocal commands
87-
8884
# Start building our number.
8985
number = []
9086

9187
try:
9288

9389
# If we don't have a starting character, read one.
9490
if char is None:
95-
char = commands.pop(0)
91+
char = self._commands.pop(0)
9692

9793
# Grab all characters from the string until we run out of numbers.
9894
while char in _CHARS_VALID_IN_NUMBER:
@@ -102,7 +98,7 @@ def extract_number(char=None):
10298
char = 'x' if (char == 'h') else char
10399

104100
number.append(char)
105-
char = commands.pop(0)
101+
char = self._commands.pop(0)
106102

107103

108104

@@ -128,12 +124,11 @@ def get_repeat_count():
128124
If it doesn't, returns a default value of 1.
129125
130126
"""
131-
nonlocal commands
132127

133-
if len(commands) and commands[0] == ':':
128+
if len(self._commands) and self._commands[0] == ':':
134129

135130
# Discard our colon...
136-
del commands[0]
131+
del self._commands[0]
137132

138133
# ... and extract the relevant number.
139134
return extract_number()
@@ -143,13 +138,13 @@ def get_repeat_count():
143138

144139

145140
# Handle each byte in the command string.
146-
while commands:
141+
while self._commands:
147142

148143
# Start off with no repeat modifier, and no pending operation.
149144
length = None
150145

151146
# Grab the next command-character in the queue.
152-
char = commands.pop(0)
147+
char = self._commands.pop(0)
153148

154149
# If this character starts a number, we have a write operation.
155150
if char in _CHARS_VALID_TO_START_NUMBER:
@@ -160,7 +155,7 @@ def get_repeat_count():
160155

161156
# Schedule the write.
162157
perform_pending_reads()
163-
pending_write_data.append(byte)
158+
self._pending_write_data.append(byte)
164159

165160
# Handle our core commands.
166161
elif char in _START_CHARS:
@@ -175,7 +170,7 @@ def get_repeat_count():
175170
issue_pending_writes()
176171

177172
length = get_repeat_count()
178-
pending_read_length += length
173+
self._pending_read_length += length
179174

180175
elif char in _DELAY_CHARS:
181176
handle_pending_io()
@@ -193,7 +188,7 @@ def get_repeat_count():
193188
# TODO: support 'D/d'?
194189
# TODO: message on 'Ww'?
195190

196-
return result
191+
return self._result
197192

198193

199194
#

host/greatfet/interfaces/gpio.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
from enum import IntEnum
77
from warnings import warn
8-
from abc import ABC, abstractmethod
98

109
from ..interface import GreatFETInterface
1110

@@ -28,7 +27,7 @@ class Directions(IntEnum):
2827

2928

3029

31-
class GPIOProvider(ABC, GreatFETInterface):
30+
class GPIOProvider(GreatFETInterface):
3231
""" Base class for an object that provides access to GPIO pins. """
3332

3433
# For convenience.
@@ -212,7 +211,6 @@ def release_pin(self, gpio_pin):
212211
self.mark_pin_as_unused(gpio_pin.name)
213212

214213

215-
@abstractmethod
216214
def set_up_pin(self, line, direction, initial_value=False):
217215
"""
218216
Configure a GPIO line for use as an input or output. This must be
@@ -225,7 +223,6 @@ def set_up_pin(self, line, direction, initial_value=False):
225223
pass
226224

227225

228-
@abstractmethod
229226
def set_pin_state(self, line, state):
230227
"""
231228
Set the state of an output line. The line must have previously been
@@ -238,7 +235,6 @@ def set_pin_state(self, line, state):
238235
pass
239236

240237

241-
@abstractmethod
242238
def read_pin_state(self, line):
243239
"""
244240
Get the state of an input line. The line must have previously been
@@ -253,7 +249,6 @@ def read_pin_state(self, line):
253249
pass
254250

255251

256-
@abstractmethod
257252
def get_pin_direction(self, line):
258253
"""
259254
Gets the direction of a GPIO pin.
@@ -267,7 +262,6 @@ def get_pin_direction(self, line):
267262
pass
268263

269264

270-
@abstractmethod
271265
def get_pin_port(self, line):
272266
""" Returns the 'port number' for a given GPIO pin.
273267
@@ -277,7 +271,6 @@ def get_pin_port(self, line):
277271
pass
278272

279273

280-
@abstractmethod
281274
def get_pin_identifier(self, line):
282275
""" Returns the 'pin number' for a given GPIO pin.
283276

host/greatfet/util/console.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ def getkey(self):
4040

4141
def write_bytes(self, byte_string):
4242
"""Write bytes (already encoded)"""
43+
44+
if isinstance(byte_string, tuple):
45+
byte_string = byte_string[0]
46+
4347
self.byte_output.write(byte_string)
4448
self.byte_output.flush()
4549

@@ -140,7 +144,11 @@ def setup(self):
140144
termios.tcsetattr(self.fd, termios.TCSANOW, new)
141145

142146
def getkey(self):
143-
c = sys.stdin.buffer.read(1)
147+
try:
148+
c = sys.stdin.buffer.read(1)
149+
except AttributeError:
150+
c = sys.stdin.read(1)
151+
144152
if c == unichr(0x7f):
145153
c = unichr(8) # map the BS key (which yields DEL) to backspace
146154
return c

0 commit comments

Comments
 (0)