Пример #1
0
def test_encode():
    ecode = Ecode(locale=EcodeLocale.EN,
                  flags=frozenset([EcodeFlag.SIZE_FIXED, EcodeFlag.STRETCH]),
                  align=EcodeAlign.RIGHT,
                  size=EcodeSize.XHDPI,
                  fmt=EcodeFmt.WEBP,
                  font_id=0b1100_1111,
                  foreground_color=0x12345678,
                  background_color=0x9abcdef0,
                  text='ab\nc')
    code = EcodeEncoder().encode(ecode)
    print('ecode=' + code)  # 'BA0hzxI0VniavN7wYWIKYw'

    actual = base64.urlsafe_b64decode(code + '=' * (len(code) % 4))
    expected = bytes([
        0b0000_0100,  # Version:4, Locale:4
        0b0000_1110,  # Flags:6, Align:2
        0b0010_0001,  # Size:4, Fmt:4
        0b1100_1111,  # FontId:8
        0x12,  # ForegroundColor_R:8
        0x34,  # ForegroundColor_G:8
        0x56,  # ForegroundColor_B:8
        0x78,  # ForegroundColor_A:8
        0x9a,  # BackgroundColor_R:8
        0xbc,  # BackgroundColor_G:8
        0xde,  # BackgroundColor_B:8
        0xf0,  # BackgroundColor_A:8
        0x61,  # Text:8,
        0x62,  # Text:8
        0x0a,  # Text:8
        0x63,  # Text:8
    ])
    assert actual == expected
Пример #2
0
def convert(emoji_log):
    flags = set()
    if emoji_log['size_fixed']:
        flags.add(EcodeFlag.SIZE_FIXED)
    if emoji_log['stretch']:
        flags.add(EcodeFlag.STRETCH)

    fonts_config = ContextHolder.context.config.fonts_config
    fonts = fonts_config.by_locale(emoji_log['locale'])
    font = next(filter(lambda f: f['key'] == emoji_log['font'], fonts), None)
    if not font:
        raise 'Font not found : locale={}, font={}'.format(
            emoji_log['locale'], emoji_log['font'])

    ecode = Ecode(
        locale=EcodeLocale.from_code(emoji_log['locale']),
        flags=flags,
        align=EcodeAlign.from_code(emoji_log['align']),
        size=EcodeSize.MDPI,
        fmt=EcodeFmt.PNG,
        font_id=font['id'],
        foreground_color=int(emoji_log['color'], 16),
        background_color=int(emoji_log['back_color'], 16),
        text=emoji_log['text'],
    )
    code = EcodeEncoder().encode(ecode)

    return { 'code': code }
Пример #3
0
def test_background_color_hex():
    ecode = Ecode(text='ab\nc', background_color=0x123456AB)
    assert ecode.background_color_hex == '123456AB'
Пример #4
0
def test_foreground_color_hex():
    ecode = Ecode(text='ab\nc', foreground_color=0x123456AB)
    assert ecode.foreground_color_hex == '123456AB'
Пример #5
0
def test_with_locale():
    ecode1 = Ecode(locale=EcodeLocale.JA, text='ab\nc')
    ecode2 = ecode1.with_locale(EcodeLocale.EN)
    assert ecode1 != ecode2
    assert ecode1.locale == EcodeLocale.JA
    assert ecode2.locale == EcodeLocale.EN
Пример #6
0
def test_init_empty_text():
    with pytest.raises(ValueError, match='empty string is not allowed'):
        Ecode(text='')
Пример #7
0
def test_init_background_color_illegal_value():
    with pytest.raises(ValueError,
                       match='`background_color` must be between 0 and 0xFFFFFFFF, but it is ' + repr(0xFFFFFFFF + 1)):
        Ecode(text='ab\nc', background_color=0xFFFFFFFF + 1)
Пример #8
0
def test_init_background_color_default_value():
    ecode = Ecode(text='ab\nc')
    assert ecode.background_color == 0xFFFFFFFF
Пример #9
0
def test_init_foreground_color_default_value():
    ecode = Ecode(text='ab\nc')
    assert ecode.foreground_color == 0x000000FF
Пример #10
0
def test_init_fmt_illegal_value():
    with pytest.raises(ValueError, match='`fmt` must be an instance of `EcodeFmt`, but it is \'fmt\''):
        Ecode(text='ab\nc', fmt='fmt')
Пример #11
0
def test_ecode_illegal_size():
    with pytest.raises(ValueError, match='`size` must be an instance of `EcodeSize`, but it is \'size\''):
        Ecode(text='ab\nc', size='size')
Пример #12
0
def test_ecode_illegal_align():
    with pytest.raises(ValueError, match='`align` must be an instance of `EcodeAlign`, but it is \'align\''):
        Ecode(text='ab\nc', align='align')
Пример #13
0
def test_ecode_illegal_flag_contents():
    with pytest.raises(ValueError,
                       match='`flags` must include instances of `EcodeFlag` only, but \'flag\' is contained'):
        Ecode(text='ab\nc', flags=frozenset(['flag']))
Пример #14
0
def test_ecode_illegal_flags():
    with pytest.raises(ValueError, match='`flags` must be an instance of `Set`, but it is \'flags\''):
        Ecode(text='ab\nc', flags='flags')
Пример #15
0
def test_ecode_illegal_locale():
    with pytest.raises(ValueError, match='`locale` must be an instance of `EcodeLocale`, but it is \'xxx\''):
        Ecode(text='ab\nc.', locale='xxx')