From f201f039e9f086c64371e97226dfee411c7fd945 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sat, 16 Mar 2019 19:28:47 +0100 Subject: [PATCH] Extended and normalized key names in GoogleFitEvent type --- platypush/backend/google/fit.py | 21 ++++++++++++++------- platypush/utils/__init__.py | 5 +++++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/platypush/backend/google/fit.py b/platypush/backend/google/fit.py index 985541e0..72980eca 100644 --- a/platypush/backend/google/fit.py +++ b/platypush/backend/google/fit.py @@ -3,6 +3,7 @@ import time from platypush.backend import Backend from platypush.context import get_plugin from platypush.message.event.google.fit import GoogleFitEvent +from platypush.utils import camel_case_to_snake_case class GoogleFitBackend(Backend): @@ -58,22 +59,28 @@ class GoogleFitBackend(Backend): while not self.should_stop(): last_timestamp = float(get_plugin('variable'). get(self._last_timestamp_varname).output. - get(self._last_timestamp_varname)) or 0 + get(self._last_timestamp_varname) or 0) for data_source in self.data_sources: new_last_timestamp = last_timestamp for dp in get_plugin('google.fit').get_data( user_id=self.user_id, data_source_id=data_source).output: - dp_time = dp.get('startTime', 0) + dp_time = dp.pop('startTime', 0) + if 'dataSourceId' in dp: + del dp['dataSourceId'] + if dp_time > last_timestamp: self.bus.post(GoogleFitEvent( user_id=self.user_id, data_source_id=data_source, - data_type=dp.get('dataTypeName'), - start_time=dp.get('startTime'), - end_time=dp.get('endTime'), - modified_time=dp.get('modifiedTime'), - values=dp.get('values'))) + data_type=dp.pop('dataTypeName'), + start_time=dp_time, + end_time=dp.pop('endTime'), + modified_time=dp.pop('modifiedTime'), + values=dp.pop('values'), + **{ camel_case_to_snake_case(k): v + for k,v in dp.items() } + )) if dp_time > new_last_timestamp: new_last_timestamp = dp_time diff --git a/platypush/utils/__init__.py b/platypush/utils/__init__.py index 4bada345..791a95a9 100644 --- a/platypush/utils/__init__.py +++ b/platypush/utils/__init__.py @@ -5,6 +5,7 @@ import importlib import inspect import logging import os +import re import signal import socket import ssl @@ -229,5 +230,9 @@ def get_mime_type(resource): mime = magic.Magic(mime=True) return mime.from_file(resource) +def camel_case_to_snake_case(string): + s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', string) + return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower() + # vim:sw=4:ts=4:et: