Skip to content

Commit

Permalink
Merge pull request #3 from fyfe/python3
Browse files Browse the repository at this point in the history
Python 3 support.
  • Loading branch information
jamesremuscat committed Sep 26, 2016
2 parents 7301fcb + 923e54c commit 95f2065
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 28 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
language: python
python:
- "2.7"
- "3.4"
- "3.5"
install:
- pip install coveralls
script:
Expand Down
33 changes: 19 additions & 14 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from setuptools import setup, find_packages
import re
import sys

VERSIONFILE = "src/pcars/_version.py"
verstr = "unknown"
Expand All @@ -10,26 +11,30 @@
if mo:
verstr = mo.group(1)
except EnvironmentError:
print "unable to find version in %s" % (VERSIONFILE,)
print("unable to find version in %s" % (VERSIONFILE,))
raise RuntimeError("if %s exists, it is required to be well-formed" % (VERSIONFILE,))

install_requires = ["binio"]
if sys.version_info < (3, 4):
install_requires.append("enum34")

setup(
name='pcars',
name="pcars",
version=verstr,
description='Project CARS UDP feed client',
author='James Muscat',
author_email='jamesremuscat@gmail.com',
url='https://github.com/jamesremuscat/pcars',
packages=find_packages('src', exclude=["*.tests"]),
package_dir = {'':'src'},
long_description="""\
description="Project CARS UDP feed client",
author="James Muscat",
author_email="jamesremuscat@gmail.com",
url="https://github.com/jamesremuscat/pcars",
packages=find_packages("src", exclude=["*.tests"]),
package_dir={"": "src"},
long_description="""
pcars is a Python client for Project CARS's UDP data feed.
""",
setup_requires=["nose>=1.0"],
tests_require=[],
install_requires=['binio', 'enum34'],
install_requires=install_requires,
entry_points={
'console_scripts': [
],
}
)
"console_scripts": [
],
}
)
10 changes: 5 additions & 5 deletions src/pcars/packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ def __init__(self, buildVersion, sequenceNumber, packetType, buf):
@staticmethod
def readFrom(buf):
header = Packet.HEADER.read_dict(buf)
buildVersion = header['buildVersion']
sequenceNumber = (header['seq_packet'] & 0xFC) >> 2
packetType = header['seq_packet'] & 0x3
buildVersion = header["buildVersion"]
sequenceNumber = (header["seq_packet"] & 0xFC) >> 2
packetType = header["seq_packet"] & 0x3
pClass = PACKET_TYPES.get(packetType, Packet)
return pClass(buildVersion, sequenceNumber, packetType, buf)

Expand Down Expand Up @@ -213,8 +213,8 @@ def __init__(self, buildVersion, sequenceNumber, packetType, buf):
self.data["antiLockActive"] = (self.data["raceStateFlags"] & 16) > 0
self.data["boostActive"] = (self.data["raceStateFlags"] & 32) > 0

self.data["gear"] = self.data['gearNumGears'] & 0x0F
self.data["numGears"] = (self.data['gearNumGears'] & 0xF0) >> 4
self.data["gear"] = self.data["gearNumGears"] & 0x0F
self.data["numGears"] = (self.data["gearNumGears"] & 0xF0) >> 4

self.data["pitMode"] = PitMode(self.data["pitModeSchedule"] & 0x07)
self.data["pitSchedule"] = PitSchedule((self.data["pitModeSchedule"] & 0xF0) << 4)
Expand Down
10 changes: 5 additions & 5 deletions src/pcars/stream.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from pcars.packet import Packet
from StringIO import StringIO
from io import BytesIO
from threading import Thread
import socket
import struct


MCAST_ANY = "224.0.0.1"
_MCAST_ANY = "224.0.0.1"


class PCarsStreamReceiver(Thread):
Expand All @@ -23,13 +23,13 @@ def run(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# Bind to the server address
sock.bind(('', self.port))
group = socket.inet_aton(MCAST_ANY)
sock.bind(("", self.port))
group = socket.inet_aton(_MCAST_ANY)
mreq = struct.pack("4sL", group, socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)

while True:
data = sock.recv(1400)
packet = Packet.readFrom(StringIO(data))
packet = Packet.readFrom(BytesIO(data))
for listener in self.listeners:
listener.handlePacket(packet)
9 changes: 5 additions & 4 deletions src/pcars/test/test_packet.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
from pcars.enums import GameState, SessionState, RaceState, TyreFlags, FlagColour,\
Sector, PitMode, PitSchedule
from pcars.packet import Packet, TelemetryPacket
from StringIO import StringIO
from io import BytesIO
from unittest import TestCase
import os

__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))

_location = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))


class TestPacket(TestCase):

def testParsePacketHeader(self):
p = Packet.readFrom(StringIO('\xd2\x04\x01'))
p = Packet.readFrom(BytesIO(b"\xd2\x04\x01"))
self.assertEqual(1234, p.buildVersion)
self.assertEqual(1, p.packetType)

def testParseSamplePacket0(self):
f = open(os.path.join(__location__, "packet_0.bin"), 'rb')
f = open(os.path.join(_location, "packet_0.bin"), "rb")
p = Packet.readFrom(f)
self.assertEqual(TelemetryPacket, p.__class__)
self.assertEqual(1122, p.buildVersion)
Expand Down

0 comments on commit 95f2065

Please sign in to comment.