From 7a7d6dedf37b35a51123c921cafb0769a50ef258 Mon Sep 17 00:00:00 2001 From: Thomas Klaehn Date: Mon, 12 Oct 2020 17:53:16 +0200 Subject: [PATCH] Add exception catching --- gpx_parser/__init__.py | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/gpx_parser/__init__.py b/gpx_parser/__init__.py index 254e4ab..e0f9a99 100644 --- a/gpx_parser/__init__.py +++ b/gpx_parser/__init__.py @@ -61,26 +61,30 @@ class Tracks(object): if filename not in self.__files: self.logger.info("Adding file %s.", filename) with open(filename, 'r') as f: - self.__files.append(filename) - gpx = gpxpy.parse(f) - for raw in gpx.tracks: - track = Track(raw) - self.__tracks.append(track) - trk_month = track.start_time.month - trk_year = track.start_time.year + try: + self.__files.append(filename) + gpx = gpxpy.parse(f) + for raw in gpx.tracks: + track = Track(raw) + self.__tracks.append(track) + trk_month = track.start_time.month + trk_year = track.start_time.year - if trk_year not in self.__distance: - self.__distance[trk_year] = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0} - self.__distance[trk_year][trk_month] += track.distance / 1000 + if trk_year not in self.__distance: + self.__distance[trk_year] = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0} + self.__distance[trk_year][trk_month] += track.distance / 1000 - if trk_year not in self.__duration: - self.__duration[trk_year] = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0} - self.__duration[trk_year][trk_month] += track.duration.total_seconds() + if trk_year not in self.__duration: + self.__duration[trk_year] = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0} + self.__duration[trk_year][trk_month] += track.duration.total_seconds() - if trk_year not in self.__avg_speed: - self.__avg_speed[trk_year] = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0} - self.__avg_speed[trk_year][trk_month] = self.__distance[trk_year][trk_month] / (self.__duration[trk_year][trk_month] / 3600) - self.logger.info("Adding done.") + if trk_year not in self.__avg_speed: + self.__avg_speed[trk_year] = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0} + self.__avg_speed[trk_year][trk_month] = self.__distance[trk_year][trk_month] / (self.__duration[trk_year][trk_month] / 3600) + self.logger.info("Adding done.") + except Exception as exception: + # TODO: Add logging mechanism. + pass def years(self): ret = None