def check_all_valid_str_keys(tp, option_keywords):
    options = hdf5storage.Options(**option_keywords)
    key_value_names = (options.dict_like_keys_name,
                       options.dict_like_values_name)

    data = random_dict(tp)
    for k in key_value_names:
        if k in data:
            del data[k]

    # Make a random name.
    name = random_name()

    # Write the data to the proper file with the given name with the
    # provided options. The file needs to be deleted after to keep junk
    # from building up.
    fld = None
    try:
        fld = tempfile.mkstemp()
        os.close(fld[0])
        filename = fld[1]
        hdf5storage.write(data, path=name, filename=filename, options=options)

        with h5py.File(filename, mode='r') as f:
            for k in key_value_names:
                assert escape_path(k) not in f[name]
            for k in data:
                assert escape_path(k) in f[name]
    except:
        raise
    finally:
        if fld is not None:
            os.remove(fld[1])
def test_process_path_leading_periods():
    for i in range(10):
        pth = [make_str_for_esc(include_leading_periods=True)
               for j in range(10)]
        beginning = tuple([escape_path(s) for s in pth[:-1]])
        gs = posixpath.join(*beginning)
        ts = escape_path(pth[-1])
        gname, tname = process_path(pth)
        assert_equal(gs, gname)
        assert_equal(ts, tname)
def test_process_path_escapes_bytes():
    for i in range(10):
        pth = [make_str_for_esc(
               include_escapes=chars_to_escape).encode('utf-8')
               for j in range(10)]
        beginning = tuple([escape_path(s) for s in pth[:-1]])
        gs = posixpath.join(*beginning)
        ts = escape_path(pth[-1])
        gname, tname = process_path(pth)
        assert_equal(gs, gname)
        assert_equal(ts, tname)
def test_escape_reversibility_escapes_leading_periods():
    for i in range(20):
        s = make_str_for_esc(include_escapes=chars_to_escape,
                             include_leading_periods=True)
        s_e = escape_path(s)
        s_e_u = unescape_path(s_e)
        assert_equal(s, s_e_u)
def test_escape_reversibility_leading_periods_bytes():
    for i in range(20):
        s = make_str_for_esc(include_leading_periods=True)
        s = s.encode('utf-8')
        s_e = escape_path(s)
        s_e_u = unescape_path(s_e)
        assert_equal(s, s_e_u.encode('utf-8'))
def test_escape_reversibility_escapes_bytes():
    for i in range(20):
        s = make_str_for_esc(include_escapes=chars_to_escape)
        s = s.encode('utf-8')
        s_e = escape_path(s)
        s_e_u = unescape_path(s_e)
        assert_equal(s, s_e_u.encode('utf-8'))
def test_escape_reversibility_no_escapes():
    for i in range(20):
        s = make_str_for_esc()
        s_e = escape_path(s)
        s_e_u = unescape_path(s_e)
        assert_equal(s, s_e)
        assert_equal(s, s_e_u)
def test_escaping():
    for i in range(20):
        s = make_str_for_esc(include_escapes=chars_to_escape,
                                 include_leading_periods=True)
        s_e = s
        for j, c in enumerate(chars_to_escape):
            s_e = s_e.replace(c, substitutions[j])
        length = len(s_e)
        s_e = s_e.lstrip(period)
        s_e = period_substitute * (length - len(s_e)) + s_e
        assert_equal(s_e, escape_path(s))
def check_str_key_previously_invalid_char(tp, ch, option_keywords):
    options = hdf5storage.Options(**option_keywords)
    key_value_names = (options.dict_like_keys_name,
                       options.dict_like_values_name)

    data = random_dict(tp)
    for k in key_value_names:
        if k in data:
            del data[k]

    # Add a random invalid str key using the provided character
    key = key_value_names[0]
    while key in key_value_names:
        key = ch.join(
            [random_str_ascii(max_dict_key_length) for i in range(2)])
    data[key] = random_int()

    # Make a random name.
    name = random_name()

    # Write the data to the proper file with the given name with the
    # provided options. The file needs to be deleted after to keep junk
    # from building up.
    fld = None
    try:
        fld = tempfile.mkstemp()
        os.close(fld[0])
        filename = fld[1]
        hdf5storage.write(data, path=name, filename=filename, options=options)

        with h5py.File(filename, mode='r') as f:
            for k in key_value_names:
                assert escape_path(k) not in f[name]
            for k in data:
                assert escape_path(k) in f[name]
    except:
        raise
    finally:
        if fld is not None:
            os.remove(fld[1])