Skip to content

Commit

Permalink
Merge pull request #114 from henrycjc/master
Browse files Browse the repository at this point in the history
Python3 compatibility (first round)
  • Loading branch information
nehpetsde committed May 26, 2018
2 parents 3dca7fc + 8dc96f2 commit 3d3b4cc
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 27 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
.tox
.cache
.coverage*
.pytest_cache/
__pycache__/
docs/_build/
htmlcov
dist
python-2
python-3
.idea/
.env/
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ flake8 # syntax and style checks "flake8 src/"
tox # run tests before push "tox -c tox.ini"
sphinx # to create documentation "(cd docs && make html doctest)"
sphinx-rtd-theme # with the final layout "firefox docs/_build/html/index.html"
six # py3 compat
3 changes: 2 additions & 1 deletion src/nfc/clf/acr122.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ def ccid_xfr_block(self, data, timeout=0.1):
if frame[0] != 0x80:
log.error("expected a RDR_to_PC_DataBlock")
raise IOError(errno.EIO, os.strerror(errno.EIO))
if len(frame) != 10 + struct.unpack("<I", buffer(frame, 1, 4))[0]:
# if len(frame) != 10 + struct.unpack("<I", buffer(frame, 1, 4))[0]:
if len(frame) != 10 + struct.unpack("<I", frame[1:5])[0]:
log.error("RDR_to_PC_DataBlock length mismatch")
raise IOError(errno.EIO, os.strerror(errno.EIO))
return frame[10:]
Expand Down
3 changes: 2 additions & 1 deletion src/nfc/clf/pn53x.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import nfc.clf
from . import device

import six
import os
import time
import errno
Expand Down Expand Up @@ -102,7 +103,7 @@ class Chipset(object):
0x632E: "CIU_RFT3",
0x632F: "CIU_RFT4",
}
REGBYNAME = {v: k for k, v in REG.iteritems()}
REGBYNAME = {v: k for k, v in six.iteritems(REG)}

class Error(Exception):
def __init__(self, errno, strerr):
Expand Down
7 changes: 4 additions & 3 deletions src/nfc/clf/rcs380.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import struct
import operator
from binascii import hexlify
import six

import logging
log = logging.getLogger(__name__)
Expand Down Expand Up @@ -108,7 +109,7 @@ class CommunicationError:
0x00000800: "TRANSMIT_TIMEOUT_ERROR",
0x80000000: "RECEIVE_LENGTH_ERROR"
}
str2err = dict([(v, k) for k, v in err2str.iteritems()])
str2err = dict([(v, k) for k, v in six.iteritems(err2str)])

def __init__(self, status_bytes):
self.errno = struct.unpack('<L', str(status_bytes))[0]
Expand Down Expand Up @@ -248,7 +249,7 @@ def in_set_protocol(self, data=None, **kwargs):
"check_sof", "add_eof", "check_eof", "rfu", "deaf_time",
"continuous_receive_mode", "min_len_for_crm",
"type_1_tag_rrdd", "rfca", "guard_time")
for key, value in sorted(kwargs.iteritems()):
for key, value in sorted(six.iteritems(kwargs)):
data.extend(bytearray([KEYS.index(key), int(value)]))
if len(data) > 0:
data = self.send_command(0x02, data)
Expand Down Expand Up @@ -283,7 +284,7 @@ def tg_set_protocol(self, data=None, **kwargs):
data = bytearray() if data is None else bytearray(data)
KEYS = ("send_timeout_time_unit", "rf_off_error",
"continuous_receive_mode")
for key, value in sorted(kwargs.iteritems()):
for key, value in sorted(six.iteritems(kwargs)):
data.extend(bytearray([KEYS.index(key), int(value)]))
data = self.send_command(0x42, bytearray(data))
if data and data[0] != 0:
Expand Down
3 changes: 2 additions & 1 deletion src/nfc/clf/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import os
import re
import errno
import six
from binascii import hexlify

try:
Expand Down Expand Up @@ -247,7 +248,7 @@ def open(self, usb_bus, dev_adr):
raise IOError(errno.ENODEV, os.strerror(errno.ENODEV))

try:
first_setting = dev.iterSettings().next()
first_setting = six.next(dev.iterSettings())
except StopIteration:
log.error("no usb configuration settings, please replug device")
raise IOError(errno.ENODEV, os.strerror(errno.ENODEV))
Expand Down
8 changes: 5 additions & 3 deletions src/nfc/ndef/bt_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@

import io
import struct
import six
from uuid import UUID
from record import Record
from error import *
from .record import Record
from .error import *


class BluetoothConfigRecord(Record):
def __init__(self, record=None):
Expand All @@ -46,7 +48,7 @@ def __init__(self, record=None):
def data(self):
f = io.BytesIO()
f.write(str(bytearray(reversed(self._bdaddr))))
for key, value in self.eir.iteritems():
for key, value in six.iteritems(self.eir):
f.write(chr(1 + len(value)) + chr(key) + str(value))
oob_length = 2 + f.tell()
f.seek(0,0)
Expand Down
10 changes: 5 additions & 5 deletions src/nfc/ndef/handover.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@

import io
import struct
from record import Record, RecordList
from message import Message
from error import *
from bt_record import BluetoothConfigRecord
from wifi_record import WifiConfigRecord
from .record import Record, RecordList
from .message import Message
from .error import *
from .bt_record import BluetoothConfigRecord
from .wifi_record import WifiConfigRecord

def parse_carrier_structure(ac_record, records):
carrier_record = records.get(ac_record.carrier_data_reference)
Expand Down
2 changes: 1 addition & 1 deletion src/nfc/ndef/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import re

import nfc.ndef
from error import LengthError, FormatError
from .error import LengthError, FormatError

type_name_prefix = (
'', 'urn:nfc:wkt:', '', '', 'urn:nfc:ext:', 'unknown', 'unchanged')
Expand Down
13 changes: 7 additions & 6 deletions src/nfc/ndef/smart_poster.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@

import io
import struct
from record import Record
from message import Message
from uri_record import UriRecord
from text_record import TextRecord
import six
from .record import Record
from .message import Message
from .uri_record import UriRecord
from .text_record import TextRecord

actions = ('default', "exec", "save", "edit")

Expand Down Expand Up @@ -76,9 +77,9 @@ def __init__(self, uri, title=None, icons=None, action='default',
def data(self):
# encode smart poster payload as ndef message
message = Message(UriRecord(self._uri))
for lang, text in self.title.iteritems():
for lang, text in six.iteritems(self.title):
message.append(TextRecord(text=text, language=lang))
for image_type, image_data in self.icons.iteritems():
for image_type, image_data in six.iteritems(self.icons):
message.append(Record("image/"+image_type, data=image_data))
if self._action >= 0:
message.append(Record("urn:nfc:wkt:act", data=chr(self._action)))
Expand Down
2 changes: 1 addition & 1 deletion src/nfc/ndef/text_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import logging
log = logging.getLogger(__name__)

from record import Record
from .record import Record

class TextRecord(Record):
"""Wraps an NDEF Text record and provides access to the
Expand Down
2 changes: 1 addition & 1 deletion src/nfc/ndef/uri_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import logging
log = logging.getLogger(__name__)

from record import Record
from .record import Record

class UriRecord(Record):
"""Wraps an NDEF URI record and provides access to the :attr:`uri`
Expand Down
9 changes: 5 additions & 4 deletions src/nfc/ndef/wifi_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@

import io
import struct
from record import Record
from error import DecodeError, EncodeError
import six
from .record import Record
from .error import DecodeError, EncodeError

VERSION1 = "\x10\x4A"
CREDENTIAL = "\x10\x0e"
Expand Down Expand Up @@ -64,10 +65,10 @@
}

auth_type_keys = \
dict([(v,k) for k,v in auth_type_names.iteritems()])
dict([(v, k) for k, v in six.iteritems(auth_type_names)])

crypt_type_keys = \
dict([(v,k) for k,v in crypt_type_names.iteritems()])
dict([(v, k) for k, v in six.iteritems(crypt_type_names)])

class WifiConfigRecord(Record):
def __init__(self, record=None):
Expand Down

0 comments on commit 3d3b4cc

Please sign in to comment.