import uuid


def uuid_from_string(uuid_str):
    """Returns a uuid object for the given uuid string; hyphens are ignored.

    arguments:
       uuid_str (string): the hexadecimal representation of the 128 bit uuid integer;
          hyphens are ignored

    returns:
       uuid.UUID object

    notes:
       if a uuid.UUID object is passed by accident, it is returned;
       if the string starts with an underscore, the underscore is skipped (to cater for a fesapi quirk);
       any tail beyond the uuid string is ignored
    """

    if uuid_str is None:
        return None
    if isinstance(uuid_str, uuid.UUID):
        return uuid_str  # resilience to accidentally passing a uuid object
    try:
        if uuid_str[0] == '_':  # tolerate one of the fesapi quirks
            if len(uuid_str) < 37:
                return None
            return uuid.UUID(uuid_str[1:37])
        else:
            if len(uuid_str) < 36:
                return None
            return uuid.UUID(uuid_str[:36])
    except Exception:
        # could log or raise an error or warning?
        return None