Merge pull request #371 from carpedm20/fix-enums
Fix `ThreadColor` and `MessageReaction` enums
This commit is contained in:
@@ -26,12 +26,11 @@ class ConcatJSONDecoder(json.JSONDecoder):
|
|||||||
def graphql_color_to_enum(color):
|
def graphql_color_to_enum(color):
|
||||||
if color is None:
|
if color is None:
|
||||||
return None
|
return None
|
||||||
if len(color) == 0:
|
if not color:
|
||||||
return ThreadColor.MESSENGER_BLUE
|
return ThreadColor.MESSENGER_BLUE
|
||||||
try:
|
color = color[2:] # Strip the alpha value
|
||||||
return ThreadColor('#{}'.format(color[2:].lower()))
|
color_value = '#{}'.format(color.lower())
|
||||||
except ValueError:
|
return enum_extend_if_invalid(ThreadColor, color_value)
|
||||||
raise FBchatException('Could not get ThreadColor from color: {}'.format(color))
|
|
||||||
|
|
||||||
def get_customization_info(thread):
|
def get_customization_info(thread):
|
||||||
if thread is None or thread.get('customization_info') is None:
|
if thread is None or thread.get('customization_info') is None:
|
||||||
@@ -285,7 +284,10 @@ def graphql_to_message(message):
|
|||||||
rtn.unsent = False
|
rtn.unsent = False
|
||||||
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 = {str(r['user']['id']):MessageReaction(r['reaction']) for r in message.get('message_reactions')}
|
rtn.reactions = {
|
||||||
|
str(r['user']['id']): enum_extend_if_invalid(MessageReaction, r['reaction'])
|
||||||
|
for r in message.get('message_reactions')
|
||||||
|
}
|
||||||
if message.get('blob_attachments') is not None:
|
if message.get('blob_attachments') is not None:
|
||||||
rtn.attachments = [graphql_to_attachment(attachment) for attachment in message['blob_attachments']]
|
rtn.attachments = [graphql_to_attachment(attachment) for attachment in message['blob_attachments']]
|
||||||
if message.get('extensible_attachment') is not None:
|
if message.get('extensible_attachment') is not None:
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
# -*- coding: UTF-8 -*-
|
# -*- coding: UTF-8 -*-
|
||||||
|
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
import enum
|
import aenum
|
||||||
|
|
||||||
|
|
||||||
class FBchatException(Exception):
|
class FBchatException(Exception):
|
||||||
@@ -602,7 +602,7 @@ class Plan(object):
|
|||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return '<Plan ({}): {} time={}, location={}, location_id={}>'.format(self.uid, repr(self.title), self.time, repr(self.location), repr(self.location_id))
|
return '<Plan ({}): {} time={}, location={}, location_id={}>'.format(self.uid, repr(self.title), self.time, repr(self.location), repr(self.location_id))
|
||||||
|
|
||||||
class Enum(enum.Enum):
|
class Enum(aenum.Enum):
|
||||||
"""Used internally by fbchat to support enumerations"""
|
"""Used internally by fbchat to support enumerations"""
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
# For documentation:
|
# For documentation:
|
||||||
|
@@ -11,6 +11,7 @@ from os.path import basename
|
|||||||
import warnings
|
import warnings
|
||||||
import logging
|
import logging
|
||||||
import requests
|
import requests
|
||||||
|
import aenum
|
||||||
from .models import *
|
from .models import *
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -300,6 +301,14 @@ def get_files_from_paths(filenames):
|
|||||||
for fn, fp, ft in files:
|
for fn, fp, ft in files:
|
||||||
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)]
|
||||||
|
@@ -1,3 +1,3 @@
|
|||||||
requests
|
requests
|
||||||
beautifulsoup4
|
beautifulsoup4
|
||||||
enum34; python_version < '3.4'
|
aenum
|
||||||
|
@@ -43,9 +43,6 @@ include_package_data = True
|
|||||||
packages = find:
|
packages = find:
|
||||||
python_requires = >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, <4.0
|
python_requires = >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, <4.0
|
||||||
install_requires =
|
install_requires =
|
||||||
|
aenum
|
||||||
requests
|
requests
|
||||||
beautifulsoup4
|
beautifulsoup4
|
||||||
# May not work in pip with bdist_wheel
|
|
||||||
# See https://wheel.readthedocs.io/en/latest/#defining-conditional-dependencies
|
|
||||||
# It is therefore defined in setup.py
|
|
||||||
# enum34; python_version < '3.4'
|
|
||||||
|
Reference in New Issue
Block a user