diff options
| author | Daniel Schadt <kingdread@gmx.de> | 2025-11-09 18:23:30 +0100 |
|---|---|---|
| committer | Daniel Schadt <kingdread@gmx.de> | 2025-11-09 18:23:30 +0100 |
| commit | ae55e81fd0c422eb4f1a232f0fcdc13c18022dbc (patch) | |
| tree | d7953bb06c65dc89f32ccfa9c8ffabfc4b1c702b /fietsboek/convert.py | |
| parent | 0b26bb759f2b479a6a7b274a4e1436f62608dcf5 (diff) | |
| download | fietsboek-ae55e81fd0c422eb4f1a232f0fcdc13c18022dbc.tar.gz fietsboek-ae55e81fd0c422eb4f1a232f0fcdc13c18022dbc.tar.bz2 fietsboek-ae55e81fd0c422eb4f1a232f0fcdc13c18022dbc.zip | |
better error handling for convert.smart_convert
Returning None is pretty nondescript, so let's make it official that we
raise an exception there.
Diffstat (limited to 'fietsboek/convert.py')
| -rw-r--r-- | fietsboek/convert.py | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/fietsboek/convert.py b/fietsboek/convert.py index 9cd3297..de6314f 100644 --- a/fietsboek/convert.py +++ b/fietsboek/convert.py @@ -12,6 +12,17 @@ from .models import Track, Waypoint FIT_RECORD_FIELDS = ["position_lat", "position_long", "altitude", "timestamp"] +class ConversionError(Exception): + """Error that occurred when loading a track from a file.""" + + +class UnknownFormat(ConversionError): + """The format of the source file could not be identified.""" + + def __str__(self): + return type(self).__doc__ + + def semicircles_to_deg(circles: int) -> float: """Convert semicircles coordinate to degree coordinate. @@ -30,6 +41,7 @@ def from_fit(data: bytes) -> Track: :param data: The input bytes. :return: The converted structure. + :raises ConversionError: If conversion failed. """ fitfile = fitparse.FitFile(data) start_time = None @@ -68,6 +80,7 @@ def from_gpx(data: bytes) -> Track: :param data: The input bytes. :return: The converted structure. + :raises ConversionError: If conversion failed. """ # pylint: disable=too-many-locals gpx = gpxpy.parse(data) @@ -132,7 +145,7 @@ def from_gpx(data: bytes) -> Track: return track -def smart_convert(data: bytes) -> Optional[Track]: +def smart_convert(data: bytes) -> Track: """Tries to be smart in converting the input bytes. This function automatically applies the correct conversion if possible. @@ -142,12 +155,13 @@ def smart_convert(data: bytes) -> Optional[Track]: :param data: The input bytes. :return: The converted content. + :raises ConversionError: When conversion fails. """ if len(data) > 11 and data[9:12] == b"FIT": return from_fit(data) if data.startswith(b"<?xml") and b"<gpx" in data[:200]: return from_gpx(data) - return None + raise UnknownFormat() -__all__ = ["from_fit", "from_gpx", "smart_convert"] +__all__ = ["ConversionError", "from_fit", "from_gpx", "smart_convert"] |
