
    <i2                         S r SSKrSSKrSSKrSSKrSSKJrJr  SSKJ	r	  SSK
JrJrJr  SSKJrJr  SSKJr  SS jr " S	 S
\5      r " S S\5      r " S S\5      r " S S\5      rg)zPython STIX2 Memory Source/Sink    N)v20v21)	_STIXBase)DataSink
DataSourceDataStoreMixin)	FilterSetapply_common_filters)parsec                    [        U[        5      (       a  U H  n[        XX#5        M     gUS   S:X  a&  UR                  S/ 5       H  n[        XX#5        M     g[        U[        5      (       a  UnO[        XU5      nSU;   aS  US   U R                  ;   a  U R                  US      nO[        5       nXPR                  US   '   UR                  U5        gX@R                  US   '   g)a  Add STIX objects to MemoryStore/Sink.

Adds STIX objects to an in-memory dictionary for fast lookup.
Recursive function, breaks down STIX Bundles and lists.

Args:
    store: A MemoryStore, MemorySink or MemorySource object.
    stix_data (list OR dict OR STIX object): STIX objects to be added
    allow_custom (bool): Whether to allow custom properties as well unknown
        custom objects. Note that unknown custom objects cannot be parsed
        into STIX objects, and will be returned as is. Default: False.
    version (str): Which STIX2 version to lock the parser to. (e.g. "2.0",
        "2.1"). If None, the library makes the best effort to figure
        out the spec representation of the object.

typebundleobjectsmodifiedidN)	
isinstancelist_addgetr   r   _data_ObjectFamilyadd)store	stix_dataallow_customversionstix_obj
obj_familys         P/home/james-whalen/.local/lib/python3.13/site-packages/stix2/datastore/memory.pyr   r      s    " )T""!H,8 " 
6	h	&!i4H,8 5
 i++ HYg>H
 !~,"[[$8
*_
.8HTN+NN8$ +3KK'    c                   0    \ rS rSrSrS rS rS rS rSr	g)	r   A   z
An internal implementation detail of memory sources/sinks/stores.
Represents a "family" of STIX objects: all objects with a particular
ID.  (I.e. all versions.)  The latest version is also tracked so that it
can be obtained quickly.
c                      0 U l         S U l        g Nall_versionslatest_versionselfs    r   __init___ObjectFamily.__init__H   s    "r    c                 z    XR                   US   '   U R                  b  US   U R                  S   :  a  Xl        g g )Nr   r%   )r)   objs     r   r   _ObjectFamily.addL   sB    -0#j/*'
Od11*=="% >r    c                 T    SR                  U R                  U R                  S   5      $ )Nz<<{}; latest={}>>r   )formatr&   r'   r(   s    r   __str___ObjectFamily.__str__T   s,    "))
+
 	
r    c                     [        U 5      $ r$   )strr(   s    r   __repr___ObjectFamily.__repr__Z   s    4yr    r%   N)
__name__
__module____qualname____firstlineno____doc__r*   r   r1   r5   __static_attributes__ r    r   r   r   A   s    #&
r    r   c                   <   ^  \ rS rSrSrSU 4S jjrS rS rSrU =r	$ )MemoryStore^   a  Interface to an in-memory dictionary of STIX objects.

MemoryStore is a wrapper around a paired MemorySink and MemorySource.

Note: It doesn't make sense to create a MemoryStore by passing
in existing MemorySource and MemorySink because there could
be data concurrency issues. As well, just as easy to create new MemoryStore.

Args:
    stix_data (list OR dict OR STIX object): STIX content to be added
    allow_custom (bool): whether to allow custom STIX content.
        Only applied when export/input functions called, i.e.
        load_from_file() and save_to_file(). Defaults to True.

Attributes:
    _data (dict): the in-memory dict that holds STIX objects
    source (MemorySource): MemorySource
    sink (MemorySink): MemorySink

c           
         > 0 U l         U(       a  [        XX#5        [        [        U ]  [        U R                   X#SS9[        U R                   X#SS9S9  g )NT)r   r   r   _store)sourcesink)r   r   superr?   r*   MemorySource
MemorySink)r)   r   r   r   	__class__s       r   r*   MemoryStore.__init__s   sM    
,8k4)$**<imndjj|eij 	* 	
r    c                 :    U R                   R                  " U0 UD6$ )a+  Write SITX objects from in-memory dictionary to JSON file, as a STIX
Bundle. If a directory is given, the Bundle 'id' will be used as
filename. Otherwise, the provided value will be used.

Args:
    path (str): file path to write STIX data to.
    encoding (str): The file encoding. Default utf-8.

)rD   save_to_filer)   argskwargss      r   rK   MemoryStore.save_to_file~   s     yy%%t6v66r    c                 :    U R                   R                  " U0 UD6$ )zLoad STIX data from JSON file.

File format is expected to be a single JSON STIX object or JSON STIX
bundle.

Args:
    path (str): file path to load STIX data from

)rC   load_from_filerL   s      r   rQ   MemoryStore.load_from_file   s     {{))4:6::r    )r   )NTN)
r7   r8   r9   r:   r;   r*   rK   rQ   r<   __classcell__rH   s   @r   r?   r?   ^   s    (	

7
; 
;r    r?   c                      ^  \ rS rSrSrSU 4S jjrSS jr\R                  \l        S	S jr\	R                  R                  \l        Sr
U =r$ )
rG      a  Interface for adding/pushing STIX objects to an in-memory dictionary.

Designed to be paired with a MemorySource, together as the two
components of a MemoryStore.

Args:
    stix_data (dict OR list): valid STIX 2.0 content in
        bundle or a list.
    _store (bool): whether the MemorySink is a part of a MemoryStore,
        in which case "stix_data" is a direct reference to
        shared memory with DataSource. Not user supplied
    allow_custom (bool): whether to allow custom objects/properties
        when exporting STIX content to file.
        Default: True.
    version (str): If present, it forces the parser to use the version
        provided. Otherwise, the library will make the best effort based
        on checking the "spec_version" property.

Attributes:
    _data (dict): the in-memory dict that holds STIX objects.
        If part of a MemoryStore, the dict is shared with a MemorySource

c                    > [         [        U ]  5         X l        U(       a  Xl        g 0 U l        U(       a  [        XX#5        g g r$   )rE   rG   r*   r   r   r   r)   r   r   r   rB   rH   s        r   r*   MemorySink.__init__   s7    j$(*("JDJTl< r    c                 2    [        XU R                  U5        g r$   )r   r   )r)   r   r   s      r   r   MemorySink.add   s    Td//9r    c                    [         R                  R                  U5      n[        [        R
                  R                  S U R                  R                  5        5       5      5      n[        S U 5       5      (       a  [        R                  " X0R                  S9nO[        R                  " X0R                  S9nUR                  S5      (       au  [         R                  R                  [         R                  R!                  U5      5      (       d3  [         R"                  " [         R                  R!                  U5      5        O_[         R                  R                  U5      (       d  [         R"                  " U5        [         R                  R%                  XS   S-   5      n[&        R(                  " USUS9 nUR+                  SUS	S
9nUR-                  U5        S S S 5        U$ ! , (       d  f       U$ = f)Nc              3      #    U  H7  n[        U[        5      (       a  UR                  R                  5       OU/v   M9     g 7fr$   r   r   r&   values.0values     r   	<genexpr>*MemorySink.save_to_file.<locals>.<genexpr>   s>      * 1E 0:%/O/O""))+W0   ?Ac              3   ,   #    U  H
  nS U;   v   M     g7f)spec_versionNr=   )ra   xs     r   rc   rd      s     5Hq~"Hs   )r   z.jsonr   wencodingTF)prettyrk   ensure_ascii)ospathabspathr   	itertoolschainfrom_iterabler   r_   anyr   Bundler   r   endswithexistsdirnamemakedirsjoinioopen	serializewrite)r)   ro   rk   all_objsr   fs         r   rK   MemorySink.save_to_file   s]   wwt$OO)) * "ZZ..0* 
 5H555ZZ7H7HIFZZ7H7HIF==!!77>>"''//$"788BGGOOD1277>>$''D! 77<<TlW&<=DWWT32a%%THSX%YFGGFO 3 	 32 s   ;#G((
G7r   r   NTNFr$   )utf-8)r7   r8   r9   r:   r;   r*   r   r   rK   r?   r<   rS   rT   s   @r   rG   rG      s;    .	=:,,CK> '33;;Lr    rG   c                      ^  \ rS rSrSrS	U 4S jjrS
S jrS
S jrSS jrSS jr	\
R                  R                  \	l        SrU =r$ )rF      a  Interface for searching/retrieving STIX objects from an in-memory
dictionary.

Designed to be paired with a MemorySink, together as the two
components of a MemoryStore.

Args:
    stix_data (dict OR list OR STIX object): valid STIX 2.0 content in
        bundle or list.
    _store (bool): if the MemorySource is a part of a MemoryStore,
        in which case "stix_data" is a direct reference to shared
        memory with DataSink. Not user supplied
    allow_custom (bool): whether to allow custom objects/properties
        when importing STIX content from file.
        Default: True.
    version (str): If present, it forces the parser to use the version
        provided. Otherwise, the library will make the best effort based
        on checking the "spec_version" property.

Attributes:
    _data (dict): the in-memory dict that holds STIX objects.
        If part of a MemoryStore, the dict is shared with a MemorySink

c                    > [         [        U ]  5         X l        U(       a  Xl        g 0 U l        U(       a  [        XX#5        g g r$   )rE   rF   r*   r   r   r   rX   s        r   r*   MemorySource.__init__   s7    lD*,("JDJTl< r    c                 8   SnU R                   R                  U5      nU(       a$  [        U[        5      (       a  UR                  nOUnU(       aJ  [        [        R                  " U=(       d    / U R                  5      5      n[        [        U/U5      S5      nU$ )aD  Retrieve STIX object from in-memory dict via STIX ID.

Args:
    stix_id (str): The STIX ID of the STIX object to be retrieved.
    _composite_filters (FilterSet): collection of filters passed from the parent
        CompositeDataSource, not user supplied

Returns:
    (STIX object): STIX object that has the supplied ID.

N)r   r   r   r   r'   r   rq   rr   filtersnextr
   )r)   stix_id_composite_filtersr   mapped_valueall_filterss         r   r   MemorySource.get  s     zz~~g.,66'66'&,"LLK 0([I4PHr    c                 N   / nU R                   R                  U5      nU(       a  [        U[        5      (       a  UR                  R                  5       nOU/n[        [        R                  " U=(       d    / U R                  5      5      nUR                  [        XV5      5        U$ )aZ  Retrieve STIX objects from in-memory dict via STIX ID, all versions
of it.

Args:
    stix_id (str): The STIX ID of the STIX 2 object to retrieve.
    _composite_filters (FilterSet): collection of filters passed from
        the parent CompositeDataSource, not user supplied

Returns:
    (list): list of STIX objects that have the supplied ID.

)r   r   r   r   r&   r_   r   rq   rr   r   extendr
   )r)   r   r   resultsr   stix_objs_to_filterr   s          r   r&   MemorySource.all_versions%  s     zz~~g.,66&2&?&?&F&F&H#'3n#&,"LLK NN$%8F r    c                 H   [        U5      nU R                  (       a  UR                  U R                  5        U(       a  UR                  U5        [        R                  R                  S U R                  R                  5        5       5      n[        [        X15      5      nU$ )a  Search and retrieve STIX objects based on the complete query.

A "complete query" includes the filters from the query, the filters
attached to this MemorySource, and any filters passed from a
CompositeDataSource (i.e. _composite_filters).

Args:
    query (list): list of filters to search on
    _composite_filters (FilterSet): collection of filters passed from
        the CompositeDataSource, not user supplied

Returns:
    (list): list of STIX objects that match the supplied query.

c              3      #    U  H7  n[        U[        5      (       a  UR                  R                  5       OU/v   M9     g 7fr$   r^   r`   s     r   rc   %MemorySource.query.<locals>.<genexpr>_  s>      1
 - ,6e]+K+KE%%',re   )
r	   r   r   rq   rr   rs   r   r_   r   r
   )r)   queryr   r   all_datas        r   r   MemorySource.queryG  s~      %  <<IIdll#II()??00 1
 **,1
 
 ,X=>r    c                     [         R                  " [        R                  R	                  U5      SUS9 n[
        R                  " U5      nS S S 5        [        U WU R                  U5        g ! , (       d  f       N'= f)Nrrj   )	r{   r|   rn   ro   rp   jsonloadr   r   )r)   	file_pathr   rk   r   r   s         r   rQ   MemorySource.load_from_filej  sT    WWRWW__Y/xHA		!I I 	T9d//9 IHs   A,,
A:r   r   r$   )NN)Nr   )r7   r8   r9   r:   r;   r*   r   r&   r   rQ   r?   r<   rS   rT   s   @r   rF   rF      s=    0	=B D!F:
 )77??Nr    rF   )TN)r;   r{   rq   r   rn   stix2r   r   
stix2.baser   stix2.datastorer   r   r   stix2.datastore.filtersr	   r
   stix2.parsingr   r   objectr   r?   rG   rF   r=   r    r   <module>r      sd    % 	   	    @ @ C /3dF :6;. 6;rF< F<RO@: O@r    