o j_ @sNdZddlmZmZmZddlmZddlmZm Z ddl m Z ddl m Z mZmZddlmZddlmZmZmZmZmZed Zed Zd Zd ZGd d d eZ  d"dedededee def ddZdedeefddZ d#dedee defddZ!dedefddZ"dededefddZ#dedeeefdeefd d!Z$dS)$arCollection is a place where secret items are stored. Normally, only the default collection should be used, but this module allows to use any registered collection. Use :func:`get_default_collection` to get the default collection (and create it, if necessary). Collections are usually automatically unlocked when user logs in, but collections can also be locked and unlocked using :meth:`Collection.lock` and :meth:`Collection.unlock` methods (unlocking requires showing the unlocking prompt to user and can be synchronous or asynchronous). Creating new items and editing existing ones is possible only in unlocked collection.)DictIteratorOptional)DBusConnection) SS_PREFIXSS_PATH)Session)LockedExceptionItemNotFoundExceptionPromptDismissedException)Item)DBusAddressWrapper exec_prompt format_secret open_sessionunlock_objects CollectionServicez(/org/freedesktop/secrets/aliases/defaultz+/org/freedesktop/secrets/collection/sessionc @seZdZdZedfdededeeddfddZ de fd d Z d$d d Z de fd dZ d$ddZd$ddZdeefddZdeeefdeefddZdefddZdeddfddZ  d%dedeeefded e d!edef d"d#ZdS)&rzRepresents a collection.N connectioncollection_pathsessionreturncCs0||_||_||_t|t||_|jddS)NLabel)rrrr COLLECTION_IFACE _collection get_property)selfrrrr:/usr/lib/python3/dist-packages/secretstorage/collection.py__init__$szCollection.__init__cCst|jdS)zDReturns :const:`True` if item is locked, otherwise :const:`False`.Locked)boolrrrrrr is_locked.szCollection.is_lockedcCs|rtddS)zSIf collection is locked, raises :exc:`~secretstorage.exceptions.LockedException`.zCollection is locked!N)r#r r"rrrensure_not_locked3szCollection.ensure_not_lockedcCst|j|jgS)aRequests unlocking the collection. Returns a boolean representing whether the prompt has been dismissed; that means :const:`False` on successful unlocking and :const:`True` if it has been dismissed. .. versionchanged:: 3.0 No longer accepts the ``callback`` argument. )rrrr"rrrunlock9s zCollection.unlockcCs$ttt|j}|dd|jgdS)zLocks the collection.LockaoN)r r SERVICE_IFACErcallr)rservicerrrlockEszCollection.lockcCsD||jdd\}|dkrt|j|\}}|r tddSdS)z/Deletes the collection and all items inside it.Delete/Prompt dismissed.N)r$rr)rrr )rprompt dismissed_resultrrrdeleteJszCollection.deleteccs*|jdD] }t|j||jVqdS)z3Returns a generator of all items in the collection.ItemsN)rrr rr)r item_pathrrr get_all_itemsSszCollection.get_all_items attributesccs4|jdd|\}|D] }t|j||jVq dS)z^Returns a generator of items with the given attributes. `attributes` should be a dictionary. SearchItemsa{ss}N)rr)r rr)rr7resultr5rrr search_itemsXs zCollection.search_itemscCs|jd}t|ts J|S)zReturns the collection label.r)rr isinstancestrrlabelrrr get_label_s zCollection.get_labelr?cCs||jdd|dS)z!Sets collection label to `label`.rsN)r$r set_propertyr>rrr set_labeleszCollection.set_labelF text/plainsecretreplace content_typec Csl||js t|j|_t|j||}tdd|ftdd|fi}|jdd|||\}} t|j||jS)acCreates a new :class:`~secretstorage.item.Item` with given `label` (unicode string), `attributes` (dictionary) and `secret` (bytestring). If `replace` is :const:`True`, replaces the existing item with the same attributes. If `content_type` is given, also sets the content type of the secret (``text/plain`` by default). Returns the created item.z Item.LabelrAzItem.Attributesr9 CreateItemza{sv}(oayays)b) r$rrrrrrr)r ) rr?r7rErFrG_secret propertiesnew_itemr0rrr create_itemjs     zCollection.create_item)rN)FrD)__name__ __module__ __qualname____doc__DEFAULT_COLLECTIONrr=rrrr!r#r$r%r+r3rr r6rr;r@rCbytesrLrrrrr!s>    r-Nrr?aliasrrc Cs|st|}tdd|fi}ttt|}|dd||\}}t|dkr+t|||dSt||\}} |r8t d| \} }| dksBJt|||dS) zCreates a new :class:`Collection` with the given `label` and `alias` and returns it. This action requires prompting. :raises: :exc:`~secretstorage.exceptions.PromptDismissedException` if the prompt is dismissed. zCollection.LabelrACreateCollectionza{sv}s)rr/o) rrr rr(r)lenrrr ) rr?rSrrJr*rr0r1r: signaturerrrcreate_collections   rYccs.ttt|}|dD]}t||Vq dS)z1Returns a generator of all available collections. CollectionsN)r rr(rr)rr*rrrrget_all_collectionss  r[cCs,zt|WStyt|dd|YSw)zAReturns the default collection. If it doesn't exist, creates it.Defaultdefault)rr rY)rrrrrget_default_collections  r^cCs^zt|WStyYnwzt|tWStyYnwtt|}|r+|dStd)zReturns any collection, in the following order of preference: - The default collection; - The "session" collection (usually temporary); - The first collection in the collections list.rzNo collections found.)rr SESSION_COLLECTIONlistr[)r collectionsrrrget_any_collections     rbcCs:ttt|}|dd|\}t|dkrtdt||S)zReturns the collection with the given `alias`. If there is no such collection, raises :exc:`~secretstorage.exceptions.ItemNotFoundException`. ReadAliasrArUzNo collection with such alias.)r rr(r)rWr r)rrSr*rrrrget_collection_by_aliass   rdr7ccs>ttt|}|dd|\}}||D]}t||VqdS)zpReturns a generator of items in all collections with the given attributes. `attributes` should be a dictionary.r8r9N)r rr(r)r )rr7r*lockedunlockedr5rrrr;s   r;)r-N)N)%rPtypingrrrjeepney.io.blockingrsecretstorage.definesrrsecretstorage.dhcryptorsecretstorage.exceptionsr r r secretstorage.itemr secretstorage.utilr rrrrrr(rQr_objectrr=rYr[r^rbrdr;rrrrsL   ^