import logging


def ExtractLiteralAssignment(node, target_variable_names, operators=None):
    """Returns list of literals assigned, added or removed to either variable.

    If |node| assigns, adds or removes string literal values by a list to either
    of the target variable, returns list of all strings literals. Otherwise
    returns an empty list.

    Args:
        node: A dict representing a token subtree.
        target_variable_names: List of strings representing variable names to be
            detected for its modification.
        operators: Optional list of assignment operators to detect. Defaults to
            ['=', '+=', '-='].

    Returns:
        List of nodes used with the assignment operators to either variable.
    """
    if operators is None:
        operators = ['=', '+=', '-=']
    if node.get('type') != 'BINARY' or node.get('value') not in operators:
        return []
    # Detected pattern is like:
    #    BINARY(=)
    #     IDENTIFIER(ldflags)
    #     LIST
    #      LITERAL("-l")
    child = node.get('child')
    # BINARY assignment node should have LHS and RHS.
    if not isinstance(child, list) or len(child) != 2:
        logging.warning('Unexpected tree structure. Skipping: %s', node)
        return []
    if child[0].get('value') not in target_variable_names:
        return []
    if child[1].get('type') != 'LIST':
        return []
    literals = []
    for element in child[1].get('child'):
        if element.get('type') != 'LITERAL':
            continue
        literals.append(element)
    return literals