Skip to content

Make sure pretty_hex works under Python 2 and Python 3. #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions programmer/MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ include *.md
# Include the license file
include LICENSE

include *.py
include *.sh
include *.toml
include .coveragerc
4 changes: 4 additions & 0 deletions programmer/check_doctests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env python
import doctest
import tinyprog
doctest.testmod(tinyprog)
35 changes: 28 additions & 7 deletions programmer/tinyprog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,30 @@


def pretty_hex(data):
output = ""
"""
>>> print(pretty_hex("abc123"))
61 62 63 31 32 33
>>> print(pretty_hex(b"abc123"))
61 62 63 31 32 33
>>> print(pretty_hex(u"abc123"))
61 62 63 31 32 33
>>> print(pretty_hex("\\x00a\\x02"*12))
00 61 02 00 61 02 00 61 02 00 61 02 00 61 02 00
61 02 00 61 02 00 61 02 00 61 02 00 61 02 00 61
02 00 61 02
"""
output = []
for i in range(0, len(data), 16):
output += " ".join(["%02x" % ord(x) for x in data[i:i + 16]]) + "\n"
return output
output.append(" ".join("%02x" % to_int(x) for x in data[i:i + 16]))
return "\n".join(output)


def to_int(value):
"""
>>> to_int('A')
65
>>> to_int(0xff)
256
255
>>> list(to_int(i) for i in ['T', 'i', 'n', 'y', 0xff, 0, 0])
[84, 105, 110, 121, 255, 0, 0]
"""
Expand All @@ -71,7 +83,8 @@ def get_ports(device_id):
try:
ports += [
UsbPort(usb, d)
for d in usb.core.find(idVendor=vid, idProduct=pid, find_all=True)
for d in usb.core.find(
idVendor=vid, idProduct=pid, find_all=True)
if not d.is_kernel_driver_active(1)
]
except usb.core.USBError as e:
Expand All @@ -90,6 +103,7 @@ def get_ports(device_id):
class PortError(Exception):
pass


class SerialPort(object):
def __init__(self, port_name):
self.port_name = port_name
Expand Down Expand Up @@ -129,6 +143,7 @@ def read(self, length):
except serial.SerialException as e:
raise PortError("Failed to read from serial port:\n%s" % str(e))


class UsbPort(object):
def __init__(self, usb, device):
self.usb = usb
Expand Down Expand Up @@ -166,6 +181,7 @@ def read(self, length):
except self.usb.core.USBError as e:
raise PortError("Failed to read from USB:\n%s" % str(e))


def _mirror_byte(b):
return bit_reverse_table[to_int(b)]

Expand All @@ -185,7 +201,11 @@ def __init__(self, prog):

def _parse_json(self, data):
try:
return json.loads(bytes(data).replace(b"\x00", b"").replace(b"\xff", b"").decode("utf-8"))
data = bytes(data)
data = data.replace(b"\x00", b"")
data = data.replace(b"\xff", b"")
data = data.decode("utf-8")
return json.loads(data)
except BaseException:
return None

Expand Down Expand Up @@ -239,7 +259,8 @@ def userdata_addr_range(self):

def _get_addr_range(self, name):
# get the bootmeta's addrmap or fallback to the root's addrmap.
addr_map = self.root.get(u"bootmeta", {}).get(u"addrmap", self.root.get(u"addrmap", None))
addr_map = self.root.get(u"bootmeta", {}).get(
u"addrmap", self.root.get(u"addrmap", None))
if addr_map is None:
raise Exception("Missing address map from device metadata")
addr_str = addr_map.get(name, None)
Expand Down
13 changes: 10 additions & 3 deletions programmer/tinyprog/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#!/usr/bin/env python

from __future__ import print_function

import sys
import argparse
import json
Expand Down Expand Up @@ -355,7 +359,8 @@ def parse_int(str_value):

if args.update_bootloader:
boards_needing_update = (
check_for_wrong_tinyfpga_bx_vidpid() + check_for_new_bootloader())
check_for_wrong_tinyfpga_bx_vidpid()
+ check_for_new_bootloader())

if len(boards_needing_update) == 0:
print("""\
Expand All @@ -365,7 +370,8 @@ def parse_int(str_value):
perform_bootloader_update(port)

# program the flash memory
if (args.program is not None) or (args.program_userdata is not None) or (
if (args.program is not None) or (
args.program_userdata is not None) or (
args.program_image is not None):
boot_fpga = False

Expand Down Expand Up @@ -422,7 +428,8 @@ def progress(info):
sys.exit(1)

if check_if_overwrite_bootloader(
addr, len(bitstream), fpga.meta.userdata_addr_range()):
addr, len(bitstream),
fpga.meta.userdata_addr_range()):
boot_fpga = True
print(" Programming at addr {:06x}".format(addr))
if not fpga.program_bitstream(addr, bitstream):
Expand Down
6 changes: 4 additions & 2 deletions programmer/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ commands =
python setup.py --version
check-manifest --ignore tox.ini,tinyprog/full_version.py
python setup.py check -m -s
flake8 --exclude=data_tables.py setup.py tinyprog
python check_doctests.py
flake8 setup.py tinyprog
python setup.py install
tinyprog --help
tinyprog --version

[flake8]
exclude = .tox,*.egg,build,data
exclude = .tox,*.egg,build,data,data_tables.py
select = E,W,F
ignore=W503