コード例 #1
0
def generate_key_prefix(d_label):
    """
    Generate the key key_prefix for a list of label.
    Return None if one of the following test fail:
        Check that the "event" name is well formatted for s3 bucket key.
        Check all label in the list have the same "event" name
    Return the key_prefix if tests are OK. Key key_prefix is defined as follow:
        "{event_name}/{picture_date}/"
        Note: the date will be the date of the first picture in the label list
    :param d_label:     [dict]  Dict of labels
    :return:            [str]   key key_prefix: "{event_name}/{picture_date}/"
    """
    event, date = _get_label_event_and_date(d_label)
    if event is None or date is None:
        return None
    for key, label in d_label.items():
        is_valid, regex = s3_utils.is_valid_s3_key(label["event"])
        if not is_valid:
            log.error(
                f'Event name "{label["event"]}" contains invalid character. Char is invalid if it match with:{regex}'
            )
            return None
        if label["event"] != event:
            log.error(
                f'Key bucket can\'t be generated because event name is not unique: "{event}" != "{label["event"]}"'
            )
            return None
    return f'{event}/{date.strftime("%Y%m%d")}/'
コード例 #2
0
def test_is_valid_s3_key_simple():
    check, regex = s3_utils.is_valid_s3_key("test")
    assert check is True
コード例 #3
0
def test_is_valid_s3_key_invalid_char5():
    check, regex = s3_utils.is_valid_s3_key(
        "s3-valid&key/nothing(should)match*here!/bu'cket")
    assert check is False
コード例 #4
0
def test_is_valid_s3_key_all_valid_char():
    check, regex = s3_utils.is_valid_s3_key(
        "s3-valid_key/nothing(should)match*here!/bu'cket")
    assert check is True