def _init(self): if self.props is None: # created with "from_path" self.props = pickle.load(open(self.path)) else: # regular constructor with properties self._write() from wader.common.backends import get_backend keyring = get_backend().get_keyring(self.secrets_path) self.secrets = ProfileSecrets(self, keyring)
class PlainProfile(Profile): """I am a group of settings required to dial up""" def __init__(self, opath, path, secrets_path, props=None): super(PlainProfile, self).__init__(opath) self.path = path self.secrets_path = secrets_path self.props = props self.secrets = None self._init() def _init(self): if self.props is None: # created with "from_path" self.props = pickle.load(open(self.path)) else: # regular constructor with properties self._write() from wader.common.backends import get_backend keyring = get_backend().get_keyring(self.secrets_path) self.secrets = ProfileSecrets(self, keyring) def _write(self): with open(self.path, 'w') as configfile: pickle.dump(self.props, configfile, pickle.HIGHEST_PROTOCOL) @classmethod def from_path(cls, opath, path, secrets_path): return cls(opath, path, secrets_path) def get_settings(self): """Returns the profile settings""" return patch_list_signature(self.props) def get_secrets(self, tag, hints=None, ask=True): """ Returns the secrets associated with the profile :param tag: The section to use :param hints: what specific setting are we interested in :param ask: Should we ask the user if there is no secret? """ return self.secrets.get() def get_timestamp(self): """Returns the last time this profile was used""" return self.props['connection'].get('timestamp', 0) def is_good(self): """Has this profile been successfully used?""" return bool(self.get_timestamp()) def on_open_keyring(self, tag): """Callback to be executed when the keyring has been opened""" secrets = self.secrets.get(tag) if secrets: self.GetSecrets.reply(self, result=(secrets,)) else: self.KeyNeeded(self, tag) def set_secrets(self, tag, secrets): """ Sets or updates the secrets associated with the profile :param tag: The section to use :param secrets: The new secret to store """ self.secrets.update(secrets) self.GetSecrets.reply(self, result=(secrets,)) def update(self, props): """Updates the profile with settings ``props``""" self.props = props self._write() # emit the signal self.Updated(patch_list_signature(props)) def remove(self): """Removes the profile""" os.unlink(self.path) # emit Removed and unexport from DBus self.Removed() self.remove_from_connection()