diff --git a/fbchat/_core.py b/fbchat/_core.py index e62df71..31f4019 100644 --- a/fbchat/_core.py +++ b/fbchat/_core.py @@ -1,8 +1,11 @@ # -*- coding: UTF-8 -*- from __future__ import unicode_literals +import logging import aenum +log = logging.getLogger("client") + class Enum(aenum.Enum): """Used internally by fbchat to support enumerations""" @@ -10,3 +13,14 @@ class Enum(aenum.Enum): def __repr__(self): # For documentation: return "{}.{}".format(type(self).__name__, self.name) + + @classmethod + def _extend_if_invalid(cls, value): + try: + return cls(value) + except ValueError: + log.warning( + "Failed parsing {.__name__}({!r}). Extending enum.".format(cls, value) + ) + aenum.extend_enum(cls, "UNKNOWN_{}".format(value).upper(), value) + return cls(value) diff --git a/fbchat/_graphql.py b/fbchat/_graphql.py index 4bf8cab..7e83500 100644 --- a/fbchat/_graphql.py +++ b/fbchat/_graphql.py @@ -34,7 +34,7 @@ def graphql_color_to_enum(color): return ThreadColor.MESSENGER_BLUE color = color[2:] # Strip the alpha value color_value = "#{}".format(color.lower()) - return enum_extend_if_invalid(ThreadColor, color_value) + return ThreadColor._extend_if_invalid(color_value) def get_customization_info(thread): @@ -379,7 +379,7 @@ def graphql_to_message(message): if message.get("unread") is not None: rtn.is_read = not message["unread"] rtn.reactions = { - str(r["user"]["id"]): enum_extend_if_invalid(MessageReaction, r["reaction"]) + str(r["user"]["id"]): MessageReaction._extend_if_invalid(r["reaction"]) for r in message.get("message_reactions") } if message.get("blob_attachments") is not None: diff --git a/fbchat/_util.py b/fbchat/_util.py index 5794fe7..f18d6e1 100644 --- a/fbchat/_util.py +++ b/fbchat/_util.py @@ -11,7 +11,6 @@ from os.path import basename import warnings import logging import requests -import aenum try: from urllib.parse import urlencode, parse_qs, urlparse @@ -331,19 +330,6 @@ def get_files_from_paths(filenames): fp.close() -def enum_extend_if_invalid(enumeration, value): - try: - return enumeration(value) - except ValueError: - log.warning( - "Failed parsing {.__name__}({!r}). Extending enum.".format( - enumeration, value - ) - ) - aenum.extend_enum(enumeration, "UNKNOWN_{}".format(value).upper(), value) - return enumeration(value) - - def get_url_parameters(url, *args): params = parse_qs(urlparse(url).query) return [params[arg][0] for arg in args if params.get(arg)] diff --git a/fbchat/utils.py b/fbchat/utils.py index 4491d1c..66c5b9a 100644 --- a/fbchat/utils.py +++ b/fbchat/utils.py @@ -28,7 +28,6 @@ from ._util import ( mimetype_to_key, get_files_from_urls, get_files_from_paths, - enum_extend_if_invalid, get_url_parameters, get_url_parameter, )