def _get_body(self): # H2N assert callable(self.settings_getter), repr(self.settings_getter) pre_settings = self.settings_getter() assert isinstance(pre_settings, dict), repr(pre_settings) settings = {name: (value, strftime(time)) for name, (value, time) in pre_settings.iteritems()} result = {} # For "revive", we always get the up-to-date information from the app. # Note that .revive field is in AbstractMessage, and we override # it here. self.revive = _revive = self.revive_getter() logger.debug('For heartbeat, revive=%r', _revive) if not _revive: result['revive'] = 0 if settings: result['settings'] = settings if self.inbox_update: result['msgs'] = HeartbeatMessage.serialize_inbox_update( self.inbox_update) logger.verbose('Host sends inbox_update: %s', self.inbox_update) if self.last_auth_token_sync_ts is not None: assert isinstance(self.last_auth_token_sync_ts, datetime), \ repr(self.last_auth_token_sync_ts) result['last_auth_token_sync_ts'] = \ dt_to_ts(self.last_auth_token_sync_ts) logger.verbose('Host sends last_auth_token_sync_ts: %s', self.last_auth_token_sync_ts) return result
def serialize_inbox_update(cls, unser): """ @type unser: dict """ result = { 'last_msg_read_ts': dt_to_ts(unser['last_msg_read_ts']) if unser.get('last_msg_read_ts') else None, 'last_msg_sync_ts': dt_to_ts(unser['last_msg_sync_ts']) if unser.get('last_msg_sync_ts') else None, 'messages': list(m.to_json() for m in unser['messages']) \ if unser.get('messages') else None, } return {key: value for key, value in result.iteritems() if value is not None}
def serialize_restore_progress(unser): """ @type unser: dict """ result = { 'started': dt_to_ts(unser['started']), 'uuid': unser['uuid'].hex, 'num': unser['num'], 'of': unser['of'], 'num_bytes': unser['num_bytes'], 'of_bytes': unser['of_bytes'], 'elapsed': td_to_sec(unser['elapsed']), 'remaining': '' if unser['remaining'] is None else td_to_sec(unser['remaining']) } assert set(result.iterkeys()) == set(unser.iterkeys()), \ (unser, result) return result
def serialize_inbox_update(cls, unser): """ @type unser: dict """ result = { 'last_msg_read_ts': dt_to_ts(unser['last_msg_read_ts']) if unser.get('last_msg_read_ts') else None, 'last_msg_sync_ts': dt_to_ts(unser['last_msg_sync_ts']) if unser.get('last_msg_sync_ts') else None, 'messages': list(m.to_json() for m in unser['messages']) \ if unser.get('messages') else None, } return { key: value for key, value in result.iteritems() if value is not None }
def _get_body(self): # H2N assert callable(self.settings_getter), repr(self.settings_getter) pre_settings = self.settings_getter() assert isinstance(pre_settings, dict), repr(pre_settings) settings = { name: (value, strftime(time)) for name, (value, time) in pre_settings.iteritems() } result = {} # For "revive", we always get the up-to-date information from the app. # Note that .revive field is in AbstractMessage, and we override # it here. self.revive = _revive = self.revive_getter() logger.debug('For heartbeat, revive=%r', _revive) if not _revive: result['revive'] = 0 if settings: result['settings'] = settings if self.inbox_update: result['msgs'] = HeartbeatMessage.serialize_inbox_update( self.inbox_update) logger.verbose('Host sends inbox_update: %s', self.inbox_update) if self.last_auth_token_sync_ts is not None: assert isinstance(self.last_auth_token_sync_ts, datetime), \ repr(self.last_auth_token_sync_ts) result['last_auth_token_sync_ts'] = \ dt_to_ts(self.last_auth_token_sync_ts) logger.verbose('Host sends last_auth_token_sync_ts: %s', self.last_auth_token_sync_ts) return result