
    <i+                     l    S r SSKJr  SSKJr  SSKJr  SSKJr  SS jr	SS jr
S rS	 rSS
 jrSS jrg)z3Functions for working with STIX2 granular markings.    )
exceptions)utils)
is_marking)new_versionc           
      f   [         R                  " U5      n[         R                  " X5        U R                  S/ 5      nU(       d  / $ [	        5       nU H  nU H  n	UR                  S/ 5       H  n
[        X:H  U	R                  U
5      =(       a    UU
R                  U	5      =(       a    U/5      (       d  MJ  UR                  S5      nUR                  S5      nU(       a  U(       a  UR                  U5        U(       d  M  U(       d  M  UR                  U5        M     M     M     [        U5      $ )a  
Get all granular markings associated to with the properties.

Args:
    obj: An SDO or SRO object.
    selectors: string or list of selector strings relative to the SDO or
        SRO in which the properties appear.
    inherited (bool): If True, include markings inherited relative to the
        properties.
    descendants (bool): If True, include granular markings applied to any
        children relative to the properties.
    marking_ref (bool): If False, excludes markings that use
        ``marking_ref`` property.
    lang (bool): If False, excludes markings that use ``lang`` property.

Raises:
    InvalidSelectorError: If `selectors` fail validation.

Returns:
    list: Marking identifiers that matched the selectors expression.

granular_markings	selectorsmarking_reflang)	r   convert_to_listvalidategetsetany
startswithaddlist)objr	   	inheriteddescendantsr
   r   r   resultsmarkinguser_selectormarking_selectorreflngs                Z/home/james-whalen/.local/lib/python3.13/site-packages/stix2/markings/granular_markings.pyget_markingsr   	   s    . %%i0I	NN3" 3R8	eG$&M$+KKR$@ "6"--.>?MI%00?OK  
 "++m4C!++f-C{C(sttC( %A ' %  =    c                 2    [        XX45      n [        XU5      $ )a  
Remove all granular markings associated with selectors and append a new
granular marking. Refer to `clear_markings` and `add_markings` for details.

Args:
    obj: An SDO or SRO object.
    selectors: string or list of selector strings relative to the SDO or
        SRO in which the properties appear.
    marking: identifier or list of marking identifiers that apply to the
        properties selected by `selectors`.
    marking_ref (bool): If False, markings that use the ``marking_ref``
        property will not be removed.
    lang (bool): If False, markings that use the ``lang`` property
        will not be removed.

Returns:
    A new version of the given SDO or SRO with specified markings removed
    and new ones added.

)clear_markingsadd_markings)r   r   r	   r
   r   s        r   set_markingsr#   =   s    * 
;Ci00r   c                   ^ [         R                  " U5      n[         R                  " U5      n[         R                  " X5        U R	                  S5      mT(       d  U $ [         R
                  " T5      m/ nU H;  n[        U5      (       a  UR                  XBS.5        M(  UR                  XBS.5        M=     [         R                  " U5      R	                  S5      n[        U4S jU 5       5      (       d  [        R                  " X5      eT Vs/ s H  oDU;  d  M
  UPM     snm[         R                  " T5      mT(       a  [        U TSS9$ [        U SSS9$ s  snf )a  
Remove a granular marking from the granular_markings collection. The method
makes a best-effort attempt to distinguish between a marking-definition
or language granular marking.

Args:
    obj: An SDO or SRO object.
    marking: identifier or list of marking identifiers that apply to the
        properties selected by `selectors`.
    selectors: string or list of selectors strings relative to the SDO or
        SRO in which the properties appear.

Raises:
    InvalidSelectorError: If `selectors` fail validation.
    MarkingNotFoundError: If markings to remove are not found on
        the provided SDO or SRO.

Returns:
    A new version of the given SDO or SRO with specified markings removed.

r   r
   r	   r   r	   c              3   ,   >#    U  H	  oT;   v   M     g 7fN ).0r   r   s     r   	<genexpr>"remove_markings.<locals>.<genexpr>   s     B6++6   Tr   allow_customN)r   r   convert_to_marking_listr   r   expand_markingsr   appendbuild_granular_markingr   r   MarkingNotFoundErrorcompress_markingsr   )r   r   r	   	to_removemremover   s         @r   remove_markingsr9   V   s2   , %%i0I++G4G	NN3" 34
--.?@Ia==QGHa@A	  )))4889LMFB6BBB--c:: %$a$ //0AB32CRVWW3$TJJs   	EEc                    [         R                  " U5      n[         R                  " U5      n[         R                  " X5        / nU HO  n[	        U5      (       a  UR                  U[        U5      S.5        M2  UR                  U[        U5      S.5        MQ     U R                  S5      (       a   UR                  U R                  S5      5        [         R                  " U5      n[         R                  " U5      n[        XSS9$ )aU  
Append a granular marking to the granular_markings collection. The method
makes a best-effort attempt to distinguish between a marking-definition
or language granular marking.

Args:
    obj: An SDO or SRO object.
    marking: identifier or list of marking identifiers that apply to the
        properties selected by `selectors`.
    selectors: list of type string, selectors must be relative to the TLO
        in which the properties appear.

Raises:
    InvalidSelectorError: If `selectors` fail validation.

Returns:
    A new version of the given SDO or SRO with specified markings added.

r%   r&   r   Tr.   )r   r   r0   r   r   r2   sortedr   extendr1   r5   r   )r   r   r	   granular_markingr7   s        r   r"   r"      s    ( %%i0I++G4G	NN3"a==##AF9DU$VW##QVI=N$OP	  ww"##(; <=,,-=>../?@sTRRr   c                   ^
 [         R                  " U5      n[         R                  " X5        U R                  S5      nU(       d  U $ [         R                  " U5      n[         R
                  " USS.USS./5      nUR                  S/ 5      m
[        U
4S jU 5       5      (       d  [        R                  " U T
5      eU Ho  nU Hf  nXvR                  S/ 5      ;   d  M  UR                  S5      nUR                  S5      n	U(       a  U(       a  S	US'   U	(       d  MX  U(       d  Ma  S	US'   Mh     Mq     [         R                  " U5      nU(       a
  [        XS
S9$ [        U SS
S9$ )a  
Remove all granular markings associated with the selectors.

Args:
    obj: An SDO or SRO object.
    selectors: string or list of selectors strings relative to the SDO or
        SRO in which the properties appear.
    marking_ref (bool): If False, markings that use the ``marking_ref``
        property will not be removed.
    lang (bool): If False, markings that use the ``lang`` property
        will not be removed.

Raises:
    InvalidSelectorError: If `selectors` fail validation.
    MarkingNotFoundError: If markings to remove are not found on
        the provided SDO or SRO.

Returns:
    A new version of the given SDO or SRO with specified markings cleared.

r   zN/A)r	   r
   )r	   r   c              3      >#    U  H=  nT  H3  nUR                  S / 5        H  nX1R                  S / 5      ;   v   M     M5     M?     g7f)r	   N)r   )r*   sdo_selectorsclear_markingclear_selectorclears       r   r+   !clear_markings.<locals>.<genexpr>   sR      .M"M+//R@N 	++K<< A 	=" 	=.s   AAr	   r
   r    Tr.   N)r   r   r   r   r1   r3   r   r   r4   r5   r   )r   r	   r
   r   r   granular_dictr=   sr   r   rC   s             @r   r!   r!      sT   , %%i0I	NN3" 34
--.?@006/2 M
 126E .   --c599-A((b99&**=9&**62;68$]3344/1$V,  . //0AB3RVWW3$TJJr   Nc           
      $  ^^ Uc  [        S5      e[        R                  " U5      n[        R                  " U5      n[        R                  " X5        U R                  S/ 5      nSn[        5       nU H  nU H  n	UR                  S/ 5       H  n
[        X:H  U	R                  U
5      =(       a    UU
R                  U	5      =(       a    U/5      (       d  MJ  UR                  SS5      mUR                  SS5      mU(       a,  [        U4S jU 5       5      (       a  UR                  T/5        U(       a,  [        U4S	 jU 5       5      (       a  UR                  T/5        S
nM     M     M     U(       a  UR                  [        U5      5      $ U$ )a]  
Check if field is marked by any marking or by specific marking(s).

Args:
    obj: An SDO or SRO object.
    marking: identifier or list of marking identifiers that apply to the
        properties selected by `selectors`.
    selectors (bool): string or list of selectors strings relative to the
        SDO or SRO in which the properties appear.
    inherited (bool): If True, return markings inherited from the given
        selector.
    descendants (bool): If True, return granular markings applied to any
        children of the given selector.

Raises:
    InvalidSelectorError: If `selectors` fail validation.

Returns:
    bool: True if ``selectors`` is found on internal SDO or SRO collection.
        False otherwise.

Note:
    When a list of marking identifiers is provided, if ANY of the provided
    marking identifiers match, True is returned.

z.Required argument 'selectors' must be providedr   Fr	   r
   rE   r   c              3   ,   >#    U  H	  oT:H  v   M     g 7fr(   r)   )r*   xr
   s     r   r+   is_marked.<locals>.<genexpr>+  s     &IAK'7r-   c              3   ,   >#    U  H	  oT:H  v   M     g 7fr(   r)   )r*   rJ   r   s     r   r+   rK   -  s     &B'QDy'r-   T)	TypeErrorr   r   r0   r   r   r   r   r   update
issuperset)r   r   r	   r   r   r   markedmarkingsr=   r   r   r   r
   s              @@r   	is_markedrR      sZ   6 HII%%i0I++G4G	NN3" 3R8FuH-&M$4$8$8b$I "6"--.>?MI%00?OK  
 #3"6"6}b"IK+//;D3&I&I#I#I 63&B'&B#B#B /!F %J ' .& ""3w<00Mr   )FFTT)TT)NNFF)__doc__stix2r   stix2.markingsr   stix2.utilsr   stix2.versioningr   r   r#   r9   r"   r!   rR   r)   r   r   <module>rX      s9    9    " (1h126Kr$SN?KD>r   