33
44import pandas as pd
55
6- from ..helpers import stream_chunk_contains , stream_starts_with
6+ from ..helpers import stream_starts_with
77from ..parser_base import CSVParser , Parsable
88from .columns import GPSHarmonizedColumn
99from .mixin import GPSHarmonizationMixin
@@ -54,6 +54,17 @@ class GPSCatTrackParser(GPSHarmonizationMixin, CSVParser):
5454 GPSHarmonizedColumn .TRIP_NR : None ,
5555 }
5656
57+ @classmethod
58+ def can_parse (cls , parsable ):
59+ """Check if the file starts with the expected START_WITH prefix."""
60+ if not cls .START_WITH :
61+ return True
62+ try :
63+ with parsable .get_stream (binary = False ) as stream :
64+ return stream_starts_with (stream , cls .START_WITH )
65+ except (UnicodeDecodeError , OSError ):
66+ return False
67+
5768 def harmonize_data (self , data ):
5869 # Combine Date and Time columns into timestamp
5970 data ["timestamp" ] = pd .to_datetime (
@@ -73,13 +84,13 @@ def __init__(self, parsable: Parsable):
7384 self ._raise_not_supported ("Stream must start with Name:CatLog" )
7485
7586 if self .DIVIDER :
76- if stream_chunk_contains (stream , 500 , self .DIVIDER ):
77- _intro , data = stream .read ().split (self .DIVIDER )
78- content = io .StringIO (data )
79- else :
87+ full_content = stream .read ()
88+ if self .DIVIDER not in full_content :
8089 self ._raise_not_supported (
8190 f"Stream doesn't have the divider { self .DIVIDER } "
8291 )
92+ _intro , data = full_content .split (self .DIVIDER , 1 )
93+ content = io .StringIO (data )
8394 else :
8495 content = stream
8596
@@ -191,13 +202,13 @@ def __init__(self, parsable: Parsable):
191202 self ._raise_not_supported ("Stream must start with Name:CatLog" )
192203
193204 if self .DIVIDER :
194- if stream_chunk_contains (stream , 500 , self .DIVIDER ):
195- _intro , data = stream .read ().split (self .DIVIDER )
196- content = io .StringIO (data )
197- else :
205+ full_content = stream .read ()
206+ if self .DIVIDER not in full_content :
198207 self ._raise_not_supported (
199208 f"Stream doesn't have the divider { self .DIVIDER } "
200209 )
210+ _intro , data = full_content .split (self .DIVIDER , 1 )
211+ content = io .StringIO (data )
201212 else :
202213 content = stream
203214
0 commit comments