def _modify_client_data(self, old_cd, **kw_args): """Clone ClientData object, adjusting some properties. Produce a new ClientData object that is a copy of old_cd, changing one or more of the properties. Args: old_cd: The ClientData object to be copied and modified. kw_args: A collection of keyword arguments in which the keyword is the ClientData property to modify and the value is the new property value. Returns: A new ClientData object with updated properties. """ cd_args = { 'clientId': old_cd.client_id(), 'sharedSecret': old_cd.shared_secret(), 'counterFromTime': old_cd.counter_from_time(), 'lastCount': old_cd.last_count(), 'lastCountUpdateTime': old_cd.last_count_update_time(), 'period': old_cd.period(), 'passwordLength': old_cd.password_length(), 'tags': old_cd.tags(), 'note': old_cd.note() } for kw in kw_args: if kw in cd_args: cd_args[kw] = kw_args[kw] cd = ClientData(**cd_args) return cd
def _make_client_data(self): cd_args = { 'clientId': self.args.clientIdToAdd, 'sharedSecret': self.__shared_secret } if self.args.counter is not None: cd_args['counterFromTime'] = False cd_args['lastCount'] = self.args.counter elif self.args.period is not None: cd_args['period'] = self.args.period if self.args.passwordLength is not None: cd_args['passwordLength'] = self.args.passwordLength cd = ClientData(**cd_args) return cd
def _add_client_data_to_file(self, cd_new): """Add the ClientData object to the file. Args: cd_new: the ClientData object to add to the file. Raises: DuplicateKeyError: There already exists a ClientData object with the same client_id in the data file. """ import datetime cds_existing = self.__cf.load(self.__data_file) for cd_existing in cds_existing: if cd_new.client_id() == cd_existing.client_id(): raise DuplicateKeyError("That configuration already exists.") cds_new = cds_existing[:] now = datetime.datetime.now(ClientData.tz()) if not cd_new.counter_from_time(): cd_new.set_last_count_update_time(now.strftime(self.__iso_fmt)) cds_new.append(cd_new) self.__cf.save(self.__data_file, cds_new)