Move enum_extend_if_invalid -> Enum._extend_if_invalid
This commit is contained in:
@@ -1,8 +1,11 @@
|
|||||||
# -*- coding: UTF-8 -*-
|
# -*- coding: UTF-8 -*-
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import logging
|
||||||
import aenum
|
import aenum
|
||||||
|
|
||||||
|
log = logging.getLogger("client")
|
||||||
|
|
||||||
|
|
||||||
class Enum(aenum.Enum):
|
class Enum(aenum.Enum):
|
||||||
"""Used internally by fbchat to support enumerations"""
|
"""Used internally by fbchat to support enumerations"""
|
||||||
@@ -10,3 +13,14 @@ class Enum(aenum.Enum):
|
|||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
# For documentation:
|
# For documentation:
|
||||||
return "{}.{}".format(type(self).__name__, self.name)
|
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)
|
||||||
|
@@ -34,7 +34,7 @@ def graphql_color_to_enum(color):
|
|||||||
return ThreadColor.MESSENGER_BLUE
|
return ThreadColor.MESSENGER_BLUE
|
||||||
color = color[2:] # Strip the alpha value
|
color = color[2:] # Strip the alpha value
|
||||||
color_value = "#{}".format(color.lower())
|
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):
|
def get_customization_info(thread):
|
||||||
@@ -379,7 +379,7 @@ def graphql_to_message(message):
|
|||||||
if message.get("unread") is not None:
|
if message.get("unread") is not None:
|
||||||
rtn.is_read = not message["unread"]
|
rtn.is_read = not message["unread"]
|
||||||
rtn.reactions = {
|
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")
|
for r in message.get("message_reactions")
|
||||||
}
|
}
|
||||||
if message.get("blob_attachments") is not None:
|
if message.get("blob_attachments") is not None:
|
||||||
|
@@ -11,7 +11,6 @@ from os.path import basename
|
|||||||
import warnings
|
import warnings
|
||||||
import logging
|
import logging
|
||||||
import requests
|
import requests
|
||||||
import aenum
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from urllib.parse import urlencode, parse_qs, urlparse
|
from urllib.parse import urlencode, parse_qs, urlparse
|
||||||
@@ -331,19 +330,6 @@ def get_files_from_paths(filenames):
|
|||||||
fp.close()
|
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):
|
def get_url_parameters(url, *args):
|
||||||
params = parse_qs(urlparse(url).query)
|
params = parse_qs(urlparse(url).query)
|
||||||
return [params[arg][0] for arg in args if params.get(arg)]
|
return [params[arg][0] for arg in args if params.get(arg)]
|
||||||
|
@@ -28,7 +28,6 @@ from ._util import (
|
|||||||
mimetype_to_key,
|
mimetype_to_key,
|
||||||
get_files_from_urls,
|
get_files_from_urls,
|
||||||
get_files_from_paths,
|
get_files_from_paths,
|
||||||
enum_extend_if_invalid,
|
|
||||||
get_url_parameters,
|
get_url_parameters,
|
||||||
get_url_parameter,
|
get_url_parameter,
|
||||||
)
|
)
|
||||||
|
Reference in New Issue
Block a user