예제 #1
0
def remove_markings(obj, marking):
    """
    Remove an object level marking from the object_marking_refs collection.

    Args:
        obj: A SDO or SRO object.
        marking: identifier or list of identifiers that apply to the
            SDO or SRO object.

    Raises:
        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.

    """
    marking = utils.convert_to_marking_list(marking)

    object_markings = obj.get('object_marking_refs', [])

    if not object_markings:
        return obj

    if any(x not in obj['object_marking_refs'] for x in marking):
        raise exceptions.MarkingNotFoundError(obj, marking)

    new_markings = [x for x in object_markings if x not in marking]
    if new_markings:
        return new_version(obj,
                           object_marking_refs=new_markings,
                           allow_custom=True)
    else:
        return new_version(obj, object_marking_refs=None, allow_custom=True)
예제 #2
0
def clear_markings(obj, selectors):
    """
    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.

    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.

    """
    selectors = utils.convert_to_list(selectors)
    utils.validate(obj, selectors)

    granular_markings = obj.get('granular_markings')

    if not granular_markings:
        return obj

    granular_markings = utils.expand_markings(granular_markings)

    sdo = utils.build_granular_marking([{
        'selectors': selectors,
        'marking_ref': 'N/A'
    }], )

    clear = sdo.get('granular_markings', [])

    if not any(clear_selector in sdo_selectors.get('selectors', [])
               for sdo_selectors in granular_markings
               for clear_marking in clear
               for clear_selector in clear_marking.get('selectors', [])):
        raise exceptions.MarkingNotFoundError(obj, clear)

    for granular_marking in granular_markings:
        for s in selectors:
            if s in granular_marking.get('selectors', []):
                marking_refs = granular_marking.get('marking_ref')

                if marking_refs:
                    granular_marking['marking_ref'] = ''

    granular_markings = utils.compress_markings(granular_markings)

    if granular_markings:
        return new_version(obj,
                           granular_markings=granular_markings,
                           allow_custom=True)
    else:
        return new_version(obj, granular_markings=None, allow_custom=True)
예제 #3
0
def remove_markings(obj, marking, selectors):
    """
    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.

    """
    selectors = utils.convert_to_list(selectors)
    marking = utils.convert_to_marking_list(marking)
    utils.validate(obj, selectors)

    granular_markings = obj.get('granular_markings')

    if not granular_markings:
        return obj

    granular_markings = utils.expand_markings(granular_markings)

    to_remove = []
    for m in marking:
        if is_marking(m):
            to_remove.append({'marking_ref': m, 'selectors': selectors})
        else:
            to_remove.append({'lang': m, 'selectors': selectors})

    remove = utils.build_granular_marking(to_remove).get('granular_markings')

    if not any(marking in granular_markings for marking in remove):
        raise exceptions.MarkingNotFoundError(obj, remove)

    granular_markings = [m for m in granular_markings if m not in remove]

    granular_markings = utils.compress_markings(granular_markings)

    if granular_markings:
        return new_version(obj,
                           granular_markings=granular_markings,
                           allow_custom=True)
    else:
        return new_version(obj, granular_markings=None, allow_custom=True)
예제 #4
0
def remove_markings(obj, marking, selectors):
    """
    Removes granular_marking from the granular_markings collection.

    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: identifier or list of marking identifiers that apply to the
            properties selected by `selectors`.

    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.

    """
    selectors = utils.convert_to_list(selectors)
    marking = utils.convert_to_marking_list(marking)
    utils.validate(obj, selectors)

    granular_markings = obj.get("granular_markings")

    if not granular_markings:
        return obj

    granular_markings = utils.expand_markings(granular_markings)

    to_remove = []
    for m in marking:
        to_remove.append({"marking_ref": m, "selectors": selectors})

    remove = utils.build_granular_marking(to_remove).get("granular_markings")

    if not any(marking in granular_markings for marking in remove):
        raise exceptions.MarkingNotFoundError(obj, remove)

    granular_markings = [m for m in granular_markings if m not in remove]

    granular_markings = utils.compress_markings(granular_markings)

    if granular_markings:
        return new_version(obj, granular_markings=granular_markings)
    else:
        return new_version(obj, granular_markings=None)
예제 #5
0
def add_markings(obj, marking, selectors):
    """
    Appends a granular_marking to the granular_markings collection.

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

    Raises:
        InvalidSelectorError: If `selectors` fail validation.

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

    """
    selectors = utils.convert_to_list(selectors)
    marking = utils.convert_to_marking_list(marking)
    utils.validate(obj, selectors)

    granular_marking = []
    for m in marking:
        granular_marking.append({
            "marking_ref": m,
            "selectors": sorted(selectors)
        })

    if obj.get("granular_markings"):
        granular_marking.extend(obj.get("granular_markings"))

    granular_marking = utils.expand_markings(granular_marking)
    granular_marking = utils.compress_markings(granular_marking)
    return new_version(obj, granular_markings=granular_marking)
예제 #6
0
def clear_markings(obj):
    """
    Remove all object level markings from the object_marking_refs collection.

    Args:
        obj: A SDO or SRO object.

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

    """
    return new_version(obj, object_marking_refs=None, allow_custom=True)
예제 #7
0
def add_markings(obj, marking):
    """
    Appends an object level marking to the object_marking_refs collection.

    Args:
        obj: A SDO or SRO object.
        marking: identifier or list of identifiers to apply SDO or SRO object.

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

    """
    marking = utils.convert_to_marking_list(marking)

    object_markings = set(obj.get("object_marking_refs", []) + marking)

    return new_version(obj, object_marking_refs=list(object_markings))
예제 #8
0
def add_markings(obj, marking, selectors):
    """
    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.

    """
    selectors = utils.convert_to_list(selectors)
    marking = utils.convert_to_marking_list(marking)
    utils.validate(obj, selectors)

    granular_marking = []
    for m in marking:
        if is_marking(m):
            granular_marking.append({
                'marking_ref': m,
                'selectors': sorted(selectors)
            })
        else:
            granular_marking.append({
                'lang': m,
                'selectors': sorted(selectors)
            })

    if obj.get('granular_markings'):
        granular_marking.extend(obj.get('granular_markings'))

    granular_marking = utils.expand_markings(granular_marking)
    granular_marking = utils.compress_markings(granular_marking)
    return new_version(obj,
                       granular_markings=granular_marking,
                       allow_custom=True)
예제 #9
0
def clear_markings(obj, selectors, marking_ref=True, lang=True):
    """
    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.

    """
    selectors = utils.convert_to_list(selectors)
    utils.validate(obj, selectors)

    granular_markings = obj.get('granular_markings')

    if not granular_markings:
        return obj

    granular_markings = utils.expand_markings(granular_markings)

    granular_dict = utils.build_granular_marking([
        {
            'selectors': selectors,
            'marking_ref': 'N/A'
        },
        {
            'selectors': selectors,
            'lang': 'N/A'
        },
    ])

    clear = granular_dict.get('granular_markings', [])

    if not any(clear_selector in sdo_selectors.get('selectors', [])
               for sdo_selectors in granular_markings
               for clear_marking in clear
               for clear_selector in clear_marking.get('selectors', [])):
        raise exceptions.MarkingNotFoundError(obj, clear)

    for granular_marking in granular_markings:
        for s in selectors:
            if s in granular_marking.get('selectors', []):
                ref = granular_marking.get('marking_ref')
                lng = granular_marking.get('lang')

                if ref and marking_ref:
                    granular_marking['marking_ref'] = ''
                if lng and lang:
                    granular_marking['lang'] = ''

    granular_markings = utils.compress_markings(granular_markings)

    if granular_markings:
        return new_version(obj,
                           granular_markings=granular_markings,
                           allow_custom=True)
    else:
        return new_version(obj, granular_markings=None, allow_custom=True)