Skip to content

Commit

Permalink
Merge of all TDF Ultra work.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjsr committed Feb 12, 2025
1 parent 7a7e3e7 commit 0fc2804
Show file tree
Hide file tree
Showing 37 changed files with 2,386 additions and 338 deletions.
3 changes: 2 additions & 1 deletion Actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,8 @@ def refresh( self ):
self.chipTimingOptions.SetSelection( 0 )
self.chipTimingOptions.Enable( False )

with Model.LockRace() as race:
with Model.LockRace() as modelRace:
race: Model.Race = modelRace # To get our type checking
if race:
self.ShowChecklist()

Expand Down
25 changes: 25 additions & 0 deletions ByteUtils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from typing import List, Union

UTF8_ENCODING = "utf-8"
ASCII_ENCODING = 'ascii'
ISO_ENCODING = 'iso-8859-1'
LF = b'\n'
CR = b'\r'
EOL = CR
CRLF = b'\r\n'

def encode_to_bytes(input_array: List[Union[str, bytes]], encoding: str = ISO_ENCODING) -> List[bytes]:
encoded_array = []

if isinstance(input_array, bytes):
raise ValueError("Input array must be a list of strings or bytes, not an array of bytes")

if isinstance(input_array, str):
raise ValueError("Input array must be a list of strings or bytes, not string")

for element in input_array:
if isinstance(element, bytes):
encoded_array.append(element)
else:
encoded_array.append(element.encode(encoding))
return encoded_array
60 changes: 60 additions & 0 deletions ByteUtilsTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import unittest
from typing import List, Union

from ByteUtils import CR, LF, encode_to_bytes

class TestEncodeToBytes(unittest.TestCase):
def test_all_strings(self):
input_array = ['hello', 'world', 'test']
expected_output = [b'hello', b'world', b'test']
self.assertEqual(encode_to_bytes(input_array), expected_output)

def test_all_bytes(self):
input_array = [b'hello', b'world', b'test']
expected_output = [b'hello', b'world', b'test']
self.assertEqual(encode_to_bytes(input_array), expected_output)

def test_mixed_strings_and_bytes(self):
input_array = ['hello', b'world', 'test']
expected_output = [b'hello', b'world', b'test']
self.assertEqual(encode_to_bytes(input_array), expected_output)

def test_empty_array(self):
input_array = []
expected_output = []
self.assertEqual(encode_to_bytes(input_array), expected_output)

def test_custom_encoding(self):
input_array = ['hello', 'world', 'test']
expected_output = [b'hello', b'world', b'test']
self.assertEqual(encode_to_bytes(input_array, encoding='utf-8'), expected_output)

def test_single_string(self):
input_array = ['hello']
expected_output = [b'hello']
self.assertEqual(encode_to_bytes(input_array), expected_output)

def test_single_byte(self):
input_array = b'\n'
expected_output = [b'\n']
self.assertRaises(ValueError, encode_to_bytes, input_array, 'utf-8')

def test_single_string_custom_encoding(self):
input_array = '\r'
self.assertRaises(ValueError, encode_to_bytes, input_array, 'utf-8')

def test_convert_cr_or_lf_list(self):
input_array = [CR, LF]
expected_output = [b'\r', b'\n']
self.assertEqual(encode_to_bytes(input_array), expected_output)

def test_convert_cr_or_lf_bytes(self):
input_array = b'\r\n'
self.assertRaises(ValueError, encode_to_bytes, input_array)

def test_convert_cr_or_lf_string(self):
input_array = '\r\n'
self.assertRaises(ValueError, encode_to_bytes, input_array)

if __name__ == '__main__':
unittest.main()
20 changes: 20 additions & 0 deletions ChipReader.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from abc import abstractmethod
from datetime import datetime

import JChip
import RaceResult
import Ultra
Expand All @@ -10,6 +13,11 @@ class ChipReader:

def __init__( self ):
self.chipReaderType = None
self.StartListener = None
self.GetData = None
self.StopListener = None
self.CleanupListener = None
self.IsListening = None
self.reset()

def reset( self, chipReaderType=None ):
Expand Down Expand Up @@ -56,6 +64,18 @@ def reset( self, chipReaderType=None ):
self.StopListener = JChip.StopListener
self.CleanupListener = JChip.CleanupListener
self.IsListening = JChip.IsListening

@abstractmethod
def StartListener( self, time: datetime, host: str, port: int, test: bool | None = None ) -> None:
pass

@abstractmethod
def GetData( self ) -> list[str]:
pass

@abstractmethod
def StopListener( self ) -> None:
pass

chipReaderCur = ChipReader()

Expand Down
6 changes: 6 additions & 0 deletions Debug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import os

def DebugMode() -> bool:
if os.getenv('DEBUG', 'False').lower() in ('true', '1', 't'):
return True
return True
38 changes: 38 additions & 0 deletions FileUtils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import os
from typing import List, Callable


def get_file_from_directory(file_name: str, dir: str) -> str|None:
file_path = os.path.join(dir, file_name)
if os.path.isfile(file_path):
return file_path
return None


def get_project_base_path() -> str:
return os.path.abspath(os.path.dirname(__file__))


def get_user_home_directory() -> str:
return os.path.expanduser('~')


def find_file(file_name: str, search_functions: List[Callable[[], str|None]]) -> str|None:
for func in search_functions:
directory = func()
file_path = os.path.join(directory, file_name)
if os.path.isfile(file_path):
return file_path
return None


default_search_functions = [
os.getcwd,
get_project_base_path,
get_user_home_directory
]

def config_search(file_name: str) -> str:
config_path = find_file(file_name, default_search_functions)
return config_path

2 changes: 1 addition & 1 deletion JChip.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def reset( raceDate = None ):
tSmall = datetime.timedelta( seconds = 0.00001 )
tDay = datetime.timedelta( days = 1 )

def parseTime( tStr, day = 0 ):
def parseTime( tStr: str, day: int = 0 ) -> datetime:
global dateToday
global tLast
global tSameCount
Expand Down
Loading

0 comments on commit 0fc2804

Please sign in to comment.