Use positional only format.

This commit is contained in:
Alex Root Junior 2017-07-25 04:45:33 +03:00
parent ea611e9fc9
commit 92253ba77a
7 changed files with 38 additions and 20 deletions

View file

@ -47,7 +47,8 @@ async def _check_result(method_name, response):
"""
if response.status != 200:
body = await response.text()
raise TelegramAPIError(f"The server returned HTTP {response.status}. Response body:\n[{body}]",
raise TelegramAPIError("The server returned HTTP {0}. Response body:\n[{1}]".format(
response.status, body),
method_name, response.status, body)
result_json = await response.json(loads=json.loads)
@ -56,9 +57,9 @@ async def _check_result(method_name, response):
body = await response.text()
code = result_json.get('error_code')
description = result_json.get('description')
raise TelegramAPIError(f"Error code: {code} Description {description}",
raise TelegramAPIError("Error code: {0} Description {1}".format(code, description),
method_name, response.status, body)
log.debug(f"Response for '{method_name}': {result_json}")
log.debug("Response for '{0}': {1}".format(method_name, result_json))
return result_json.get('result')
@ -120,7 +121,8 @@ async def request(session, token, method, data=None, files=None) -> bool or dict
:param files: files
:return: bool or dict
"""
log.debug(f"Make request: '{method}' with data: {data or {}} and files {files or {}}")
log.debug("Make request: '{0}' with data: {1} and files {2}".format(
method, data or {}, files or {}))
data = _compose_data(data, files)
url = Methods.api_url(token=token, method=method)
async with session.post(url, data=data) as response:

View file

@ -93,7 +93,7 @@ class Dispatcher:
continue
if updates:
log.info(f"Received {len(updates)} updates.")
log.info("Received {0} updates.".format(len(updates)))
offset = updates[-1].update_id + 1
await self.process_updates(updates)

View file

@ -109,4 +109,4 @@ class NextStepHandler:
def gen_identifier(chat_id, from_user_id):
return f"{chat_id}:{from_user_id}"
return "{0}:{1}".format(chat_id, from_user_id)

View file

@ -501,7 +501,9 @@ class Controller:
self.delete(key)
def __str__(self):
return f"{self._chat}:{self._user} - {self._state}"
return "{0}:{1} - {2}".format(
self._chat, self._user, self._state
)
class AsyncController:
@ -611,7 +613,9 @@ class AsyncController:
raise RuntimeError("Item assignment not allowed with async storage")
def __str__(self):
return f"{self._chat}:{self._user} - {self._state}"
return "{0}:{1} - {2}".format(
self._chat, self._user, self._state
)
class StateMachine:
@ -650,7 +654,9 @@ class StateMachine:
:param state:
:return:
"""
log.debug(f"Set state for {chat}:{user} to '{state}'")
log.debug("Set state for {0}:{1} to '{2}'".format(
chat, user, state
))
self.storage.set_state(chat, user, state)
def get_state(self, chat, user):
@ -669,7 +675,7 @@ class StateMachine:
:param user:
:return:
"""
log.debug(f"Reset state for {chat}:{user}")
log.debug("Reset state for {0}:{1}".format(chat, user))
self.storage.del_state(chat, user)
async def process_message(self, message):
@ -686,11 +692,15 @@ class StateMachine:
raise SkipHandler()
if state not in self.steps:
log.warning(f"Found unknown state '{state}' for {chat_id}:{from_user_id}. Condition will be reset.")
log.warning("Found unknown state '{0}' for {1}:{2}. Condition will be reset.".format(
state, chat_id, from_user_id
))
self.del_state(chat_id, from_user_id)
raise SkipHandler()
log.debug(f"Process state for {chat_id}:{from_user_id} - '{state}'")
log.debug("Process state for {0}:{1} - '{2}'".format(
chat_id, from_user_id, state
))
callback = self.steps[state]
controller = Controller(self, chat_id, from_user_id, state)
await callback(message, controller)
@ -731,7 +741,9 @@ class AsyncStateMachine:
:param state:
:return:
"""
log.debug(f"Set state for {chat}:{user} to '{state}'")
log.debug("Set state for {0}:{1} to '{2}'".format(
chat, user, state
))
await self.storage.set_state(chat, user, state)
async def get_state(self, chat, user):
@ -750,7 +762,7 @@ class AsyncStateMachine:
:param user:
:return:
"""
log.debug(f"Reset state for {chat}:{user}")
log.debug("Reset state for {0}:{1}".format(chat, user))
await self.storage.del_state(chat, user)
async def process_message(self, message):
@ -767,11 +779,15 @@ class AsyncStateMachine:
raise SkipHandler()
if state not in self.steps:
log.warning(f"Found unknown state '{state}' for {chat_id}:{from_user_id}. Condition will be reset.")
log.warning("Found unknown state '{0}' for {1}:{2}. Condition will be reset.".format(
state, chat_id, from_user_id
))
await self.del_state(chat_id, from_user_id)
raise SkipHandler()
log.debug(f"Process state for {chat_id}:{from_user_id} - '{state}'")
log.debug("Process state for {0}:{1} - '{2}'".format(
chat_id, from_user_id, state
))
callback = self.steps[state]
controller = AsyncController(self, chat_id, from_user_id, state)
await callback(message, controller)

View file

@ -8,7 +8,7 @@ class ValidationError(Exception):
class TelegramAPIError(Exception):
def __init__(self, message, method, status, body):
super(TelegramAPIError, self).__init__(
f"A request to the Telegram API was unsuccessful.\n{message}")
"A request to the Telegram API was unsuccessful.\n" + message)
self.method = method
self.status = status
self.body = body

View file

@ -66,7 +66,7 @@ class Deserializable:
Bot instance
"""
if not hasattr(self, '_bot'):
raise AttributeError(f"{self.__class__.__name__} is not configured.")
raise AttributeError("{0} is not configured.".format(self.__class__.__name__))
return getattr(self, '_bot')
@bot.setter

View file

@ -64,11 +64,11 @@ def hpre(*content, sep='\n'):
def link(title, url):
return f"[{_escape(title)}]({url})"
return "[{0}]({1})".format(_escape(title), url)
def hlink(title, url):
return f"<a href=\"{url}\">{_escape(title)}</a>"
return "<a href=\"{0}\">{1}</a>".format(url, _escape(title))
def escape_md(*content, sep=' '):