Skip to content

Commit

Permalink
🎉 Release 1.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
torrua committed Dec 12, 2020
1 parent b914f35 commit 4312cd1
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 53 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
# 👨‍🍳 callbaker

[![Download Callbaker](https://img.shields.io/pypi/v/callbaker.svg)](https://pypi.python.org/pypi/callbaker)
![CodeQL](https://github.com/torrua/callbaker/workflows/CodeQL/badge.svg?branch=master)
[![Build Status](https://travis-ci.com/torrua/callbaker.svg?branch=main)](https://travis-ci.com/torrua/callbaker)
[![codecov](https://codecov.io/gh/torrua/callbaker/branch/master/graph/badge.svg?token=CHCS5JEGZI)](https://codecov.io/gh/torrua/callbaker)

Telegram callback queries converter

## What it is

This tiny package can help you parse the callback data request response for telegram bots.

## How to install

``pip install callbaker``

## How to use

Under developing...
52 changes: 1 addition & 51 deletions callbaker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,54 +12,4 @@
IMS = INTERNAL_MARK_SEPARATOR
DMV = DEFAULT_MARK_VALUE


def info_from_callback(call_data: str, separators: tuple = (EMS, IMS, DMV)) -> dict:
"""
:param call_data:
:param separators:
:return:
"""

if not isinstance(call_data, str):
raise TypeError("call_data should be a str type. You input %s." % type(call_data))

for sep in separators:
if not isinstance(sep, str):
raise TypeError("Separator should be a str type. You input %s." % type(sep))

_ems, _ims, _ = separators
separated_items = call_data.split(_ems)
parsed_items = [element.split(_ims)
for element in separated_items if element]
result = {k: v for k, v in parsed_items if k and v}

for mark, value in result.items():
if value.isdigit():
result[mark] = int(value)
_ = [result.update({mark: item}) for item in (False, True, None) if value == str(item)]
return result


def callback_from_info(info: dict, separators: tuple = (EMS, IMS, DMV)) -> str:
"""
:param info:
:param separators:
:return:
"""
if not isinstance(info, dict):
raise TypeError("Info should be a dict type. You input %s." % type(info))

for sep in separators:
if not isinstance(sep, str):
raise TypeError("Separator should be a str type. You input %s." % type(sep))

_ems, _ims, _ = separators

callback = "".join(["%s%s%s%s" % (_ems, mark, _ims, value) for mark, value in info.items()])

if len(callback) > 64:
raise ValueError("The length of callback_data should not be more that 64 symbols."
"Your callback's length is %s symbols." % len(callback))
return callback
from callbaker.functions import info_from_callback, callback_from_info
53 changes: 53 additions & 0 deletions callbaker/functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from callbaker import EMS, IMS, DMV


def info_from_callback(call_data: str, separators: tuple = (EMS, IMS, DMV)) -> dict:
"""
:param call_data:
:param separators:
:return:
"""

if not isinstance(call_data, str):
raise TypeError("call_data should be a str type. You input %s." % type(call_data))

for sep in separators:
if not isinstance(sep, str):
raise TypeError("Separator should be a str type. You input %s." % type(sep))

_ems, _ims, _ = separators
separated_items = call_data.split(_ems)
parsed_items = [element.split(_ims)
for element in separated_items if element]
result = {k: v for k, v in parsed_items if k and v}

for mark, value in result.items():
if value.isdigit():
result[mark] = int(value)
_ = [result.update({mark: item}) for item in (False, True, None) if value == str(item)]
return result


def callback_from_info(info: dict, separators: tuple = (EMS, IMS, DMV)) -> str:
"""
:param info:
:param separators:
:return:
"""
if not isinstance(info, dict):
raise TypeError("Info should be a dict type. You input %s." % type(info))

for sep in separators:
if not isinstance(sep, str):
raise TypeError("Separator should be a str type. You input %s." % type(sep))

_ems, _ims, _ = separators

callback = "".join(["%s%s%s%s" % (_ems, mark, _ims, value) for mark, value in info.items()])

if len(callback) > 64:
raise ValueError("The length of callback_data should not be more that 64 symbols."
"Your callback's length is %s symbols." % len(callback))
return callback
2 changes: 1 addition & 1 deletion tests/test_callback_from_info.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from callbaker import callback_from_info
from callbaker.functions import callback_from_info


class TestCallbackFromInfo:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_info_from_callback.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from callbaker import info_from_callback
from callbaker.functions import info_from_callback


class TestInfoFromCallback:
Expand Down

0 comments on commit 4312cd1

Please sign in to comment.