Skip to content

Commit

Permalink
correct filename on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
GeekyDeaks committed Apr 11, 2023
1 parent 9ff178b commit 85426cf
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
19 changes: 10 additions & 9 deletions stm/ams2/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@
class AMS2Logger(BaseLogger):

channels = [
'beacon', 'br2', 'lap', 'rpm', 'gear',
'beacon', 'br2', # lap or sector points
'lap', 'rpm', 'gear',
'throttle', 'brake', 'steer', 'speed',
'lat', 'long',
'glat', 'gvert', 'glong',
'suspfl', 'suspfr', 'susprl', 'susprr',
'tyretempfl', 'tyretempfr', 'tyretemprl', 'tyretemprr',
'braketempfl', 'braketempfr', 'braketemprl', 'braketemprr',
'tyretempflo', 'tyretempfro',
'tyretempflc', 'tyretempfrc',
'tyretempfli', 'tyretempfri',
'glat', 'gvert', 'glong', # g forces
'suspfl', 'suspfr', 'susprl', 'susprr', # suspension
'tyretempfl', 'tyretempfr', 'tyretemprl', 'tyretemprr', # combined tyre temp
'braketempfl', 'braketempfr', 'braketemprl', 'braketemprr', # brake temp
'tyretempflo', 'tyretempfro', # outer temp
'tyretempflc', 'tyretempfrc', # center temp
'tyretempfli', 'tyretempfri', # inner temp
'lap', 'laptime',
'racestate'
'racestate' # AMS2 race status
]

def __init__(self,
Expand Down
17 changes: 13 additions & 4 deletions stm/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import os
import re
import sqlite3
from pathlib import Path
from datetime import datetime
from logging import getLogger
l = getLogger(__name__)


class BaseLogger:

def __init__(self, sampler=None, filetemplate=None, rawfile=None):
Expand Down Expand Up @@ -124,9 +126,16 @@ def update_event(self, event=None):

template_vars[k] = v

filename = self.filetemplate.format(**template_vars)
filename = re.sub(r'_+', '_', filename)
self.filename = re.sub(r'/_', '/', filename)
filepath = Path(self.filetemplate).parts
# do substitution
filepath = [ p.format(**template_vars) for p in filepath ]
# sub out any duplicate _
filepath = [ re.sub(r'_+', '_', p) for p in filepath ]
# sub out any leading or trailing _
filepath = [ re.sub(r'^_|_$', '', p ) for p in filepath ]
# remove any blank
filepath = [ p for p in filepath if p ]
self.filename = os.path.join(*filepath)

def add_samples(self, samples):
self.log.add_samples(samples)
Expand All @@ -138,7 +147,7 @@ def add_lap(self, laptime=0.0, lap=None):
freq = self.sampler.freq
sample_time = samples / freq
# check we have a sensible laptime for the number of samples
if abs(sample_time - laptime) > (2 / freq):
if abs(sample_time - laptime) > (3 / freq):
# just use the sample_time
l.warning(f"lap {lap}, ignoring time {laptime:.3f} as too far from sample period {sample_time:.3f}")
laptime = sample_time
Expand Down
30 changes: 28 additions & 2 deletions stm/logger_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from stm.logger import BaseLogger
from stm.event import STMEvent
import os


class TestBaseLogger(unittest.TestCase):
Expand All @@ -16,9 +17,34 @@ def test_new_log(self):
def test_valid_filename(self):

event = STMEvent(driver="Steve:McQueen", datetime="2021-12-01")
l = BaseLogger(filetemplate="a/b/{driver}_{vehicle}_{datetime}")
l = BaseLogger(filetemplate=os.path.join("a", "b", "{driver}_{vehicle}_{datetime}") )
l.new_log(event=event, channels=[])
self.assertEqual(l.filename, "a/b/SteveMcQueen_2021-12-01")
expected = os.path.join("a", "b", "SteveMcQueen_2021-12-01")
self.assertEqual(l.filename, expected)

def test_remove_dir(self):

event = STMEvent(driver="Steve:McQueen", datetime="2021-12-01")
l = BaseLogger(filetemplate=os.path.join("a", "{vehicle}", "{driver}_{vehicle}_{datetime}") )
l.new_log(event=event, channels=[])
expected = os.path.join("a", "SteveMcQueen_2021-12-01")
self.assertEqual(l.filename, expected)

def test_remove_trailing_underscore(self):

event = STMEvent(driver="Steve:McQueen", datetime="2021-12-01")
l = BaseLogger(filetemplate=os.path.join("a", "b", "{driver}_{datetime}_{vehicle}") )
l.new_log(event=event, channels=[])
expected = os.path.join("a", "b", "SteveMcQueen_2021-12-01")
self.assertEqual(l.filename, expected)

def test_remove_leading_underscore(self):

event = STMEvent(driver="Steve:McQueen", datetime="2021-12-01")
l = BaseLogger(filetemplate=os.path.join("a", "b", "{vehicle}_{driver}_{datetime}") )
l.new_log(event=event, channels=[])
expected = os.path.join("a", "b", "SteveMcQueen_2021-12-01")
self.assertEqual(l.filename, expected)

if __name__ == '__main__':
unittest.main()

0 comments on commit 85426cf

Please sign in to comment.