99import abc
1010import os
1111import errno
12+ import hashlib
1213import logging
1314import sys
1415try :
@@ -50,6 +51,9 @@ def _mkdir_p(path):
5051 else :
5152 raise
5253
54+ def _auto_hash (input_string ):
55+ return hashlib .sha256 (input_string .encode ('utf-8' )).hexdigest ()
56+
5357
5458# We do not aim to wrap every os-specific exception.
5559# Here we define only the most common one,
@@ -197,19 +201,17 @@ class KeychainPersistence(BasePersistence):
197201 and protected by native Keychain libraries on OSX"""
198202 is_encrypted = True
199203
200- def __init__ (self , signal_location , service_name , account_name ):
204+ def __init__ (self , signal_location , service_name = None , account_name = None ):
201205 """Initialization could fail due to unsatisfied dependency.
202206
203207 :param signal_location: See :func:`persistence.LibsecretPersistence.__init__`
204208 """
205- if not (service_name and account_name ): # It would hang on OSX
206- raise ValueError ("service_name and account_name are required" )
207209 from .osx import Keychain , KeychainError # pylint: disable=import-outside-toplevel
208210 self ._file_persistence = FilePersistence (signal_location ) # Favor composition
209211 self ._Keychain = Keychain # pylint: disable=invalid-name
210212 self ._KeychainError = KeychainError # pylint: disable=invalid-name
211- self ._service_name = service_name
212- self ._account_name = account_name
213+ self ._service_name = service_name or _auto_hash ( signal_location )
214+ self ._account_name = account_name or _auto_hash ( signal_location )
213215
214216 def save (self , content ):
215217 with self ._Keychain () as locker :
@@ -247,7 +249,7 @@ class LibsecretPersistence(BasePersistence):
247249 and protected by native libsecret libraries on Linux"""
248250 is_encrypted = True
249251
250- def __init__ (self , signal_location , schema_name , attributes , ** kwargs ):
252+ def __init__ (self , signal_location , schema_name = None , attributes = None , ** kwargs ):
251253 """Initialization could fail due to unsatisfied dependency.
252254
253255 :param string signal_location:
@@ -262,7 +264,8 @@ def __init__(self, signal_location, schema_name, attributes, **kwargs):
262264 from .libsecret import ( # This uncertain import is deferred till runtime
263265 LibSecretAgent , trial_run )
264266 trial_run ()
265- self ._agent = LibSecretAgent (schema_name , attributes , ** kwargs )
267+ self ._agent = LibSecretAgent (
268+ schema_name or _auto_hash (signal_location ), attributes or {}, ** kwargs )
266269 self ._file_persistence = FilePersistence (signal_location ) # Favor composition
267270
268271 def save (self , content ):
0 commit comments