From 381227af664131929f6e3f32207e2f7fa2a96ff9 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 12 Dec 2018 22:39:31 +0100 Subject: [PATCH 1/6] Make use `aenum` instead of the default `enum` --- fbchat/models.py | 4 ++-- requirements.txt | 2 +- setup.cfg | 5 +---- setup.py | 2 +- 4 files changed, 5 insertions(+), 8 deletions(-) diff --git a/fbchat/models.py b/fbchat/models.py index 7cc9d65..a870913 100644 --- a/fbchat/models.py +++ b/fbchat/models.py @@ -1,7 +1,7 @@ # -*- coding: UTF-8 -*- from __future__ import unicode_literals -import enum +import aenum class FBchatException(Exception): @@ -523,7 +523,7 @@ class Plan(object): def __unicode__(self): return ''.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""" def __repr__(self): # For documentation: diff --git a/requirements.txt b/requirements.txt index 9cb2a6c..40ce5a1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ requests beautifulsoup4 -enum34; python_version < '3.4' +aenum diff --git a/setup.cfg b/setup.cfg index b554b7b..45cdd3b 100644 --- a/setup.cfg +++ b/setup.cfg @@ -43,9 +43,6 @@ include_package_data = True packages = find: python_requires = >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, <4.0 install_requires = + aenum requests 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' diff --git a/setup.py b/setup.py index 1157ca5..3216505 100755 --- a/setup.py +++ b/setup.py @@ -5,4 +5,4 @@ from __future__ import unicode_literals from setuptools import setup -setup(extras_require={':python_version < "3.4"': ['enum34']}) +setup() From e41d98144934ab95f1aae6398cb1a182a951ee08 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 12 Dec 2018 22:44:19 +0100 Subject: [PATCH 2/6] Extend `ThreadColor` when encountering unknown values --- fbchat/graphql.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/fbchat/graphql.py b/fbchat/graphql.py index 510e93b..f130a0d 100644 --- a/fbchat/graphql.py +++ b/fbchat/graphql.py @@ -3,6 +3,7 @@ from __future__ import unicode_literals import json import re +import aenum from .models import * from .utils import * @@ -26,12 +27,16 @@ class ConcatJSONDecoder(json.JSONDecoder): def graphql_color_to_enum(color): if color is None: return None - if len(color) == 0: + if not color: return ThreadColor.MESSENGER_BLUE + color = color[2:] # Strip the alpha value + color_value = '#{}'.format(color.lower()) try: - return ThreadColor('#{}'.format(color[2:].lower())) + return ThreadColor(color_value) except ValueError: - raise FBchatException('Could not get ThreadColor from color: {}'.format(color)) + log.warning("Failed parsing color {}. Extending enum.".format(color)) + aenum.extend_enum(ThreadColor, "UNKNOWN_{}".format(color), color_value) + return ThreadColor(color_value) def get_customization_info(thread): if thread is None or thread.get('customization_info') is None: From 78e7841b5ef3c3e3a3ab8f008eef4c5f85b939c9 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 12 Dec 2018 22:53:23 +0100 Subject: [PATCH 3/6] Extend `MessageReaction` when encountering unknown values --- fbchat/graphql.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/fbchat/graphql.py b/fbchat/graphql.py index f130a0d..bcc3c1f 100644 --- a/fbchat/graphql.py +++ b/fbchat/graphql.py @@ -214,7 +214,16 @@ def graphql_to_message(message): rtn.timestamp = message.get('timestamp_precise') if message.get('unread') is not None: rtn.is_read = not message['unread'] - rtn.reactions = {str(r['user']['id']):MessageReaction(r['reaction']) for r in message.get('message_reactions')} + + for r in message.get('message_reactions'): + try: + reaction = MessageReaction(r['reaction']) + except ValueError: + log.warning("Failed parsing reaction {}. Extending enum.".format(r['reaction'])) + aenum.extend_enum(MessageReaction, "UNKNOWN_{}".format(r['reaction']), r['reaction']) + reaction = MessageReaction(r['reaction']) + rtn.reactions[str(r['user']['id'])] = reaction + if message.get('blob_attachments') is not None: rtn.attachments = [graphql_to_attachment(attachment) for attachment in message['blob_attachments']] # TODO: This is still missing parsing: From c57b84cd0b8790e1adfdc7663af82a475fd854a8 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 12 Dec 2018 23:04:26 +0100 Subject: [PATCH 4/6] Refactor enum extending --- fbchat/graphql.py | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/fbchat/graphql.py b/fbchat/graphql.py index bcc3c1f..86e011f 100644 --- a/fbchat/graphql.py +++ b/fbchat/graphql.py @@ -24,6 +24,14 @@ class ConcatJSONDecoder(json.JSONDecoder): return objs # End shameless copy +def enum_extend_if_invalid(enumeration, value): + try: + return enumeration(value) + except ValueError: + log.warning("Failed parsing {}({!r}). Extending enum.".format(enumeration, value)) + aenum.extend_enum(enumeration, "UNKNOWN_{}".format(value).upper(), value) + return enumeration(value) + def graphql_color_to_enum(color): if color is None: return None @@ -31,12 +39,7 @@ def graphql_color_to_enum(color): return ThreadColor.MESSENGER_BLUE color = color[2:] # Strip the alpha value color_value = '#{}'.format(color.lower()) - try: - return ThreadColor(color_value) - except ValueError: - log.warning("Failed parsing color {}. Extending enum.".format(color)) - aenum.extend_enum(ThreadColor, "UNKNOWN_{}".format(color), color_value) - return ThreadColor(color_value) + return enum_extend_if_invalid(ThreadColor, color_value) def get_customization_info(thread): if thread is None or thread.get('customization_info') is None: @@ -214,16 +217,10 @@ def graphql_to_message(message): rtn.timestamp = message.get('timestamp_precise') if message.get('unread') is not None: rtn.is_read = not message['unread'] - - for r in message.get('message_reactions'): - try: - reaction = MessageReaction(r['reaction']) - except ValueError: - log.warning("Failed parsing reaction {}. Extending enum.".format(r['reaction'])) - aenum.extend_enum(MessageReaction, "UNKNOWN_{}".format(r['reaction']), r['reaction']) - reaction = MessageReaction(r['reaction']) - rtn.reactions[str(r['user']['id'])] = reaction - + 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: rtn.attachments = [graphql_to_attachment(attachment) for attachment in message['blob_attachments']] # TODO: This is still missing parsing: From b6a6d7dc68b3f1c4b750a0dd24aa358df9ddc3c7 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 12 Dec 2018 23:06:16 +0100 Subject: [PATCH 5/6] Move `enum_extend_if_invalid` to `utils.py` --- fbchat/graphql.py | 9 --------- fbchat/utils.py | 10 ++++++++++ 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/fbchat/graphql.py b/fbchat/graphql.py index 86e011f..a8e2609 100644 --- a/fbchat/graphql.py +++ b/fbchat/graphql.py @@ -3,7 +3,6 @@ from __future__ import unicode_literals import json import re -import aenum from .models import * from .utils import * @@ -24,14 +23,6 @@ class ConcatJSONDecoder(json.JSONDecoder): return objs # End shameless copy -def enum_extend_if_invalid(enumeration, value): - try: - return enumeration(value) - except ValueError: - log.warning("Failed parsing {}({!r}). Extending enum.".format(enumeration, value)) - aenum.extend_enum(enumeration, "UNKNOWN_{}".format(value).upper(), value) - return enumeration(value) - def graphql_color_to_enum(color): if color is None: return None diff --git a/fbchat/utils.py b/fbchat/utils.py index 92e8a9d..d3008dc 100644 --- a/fbchat/utils.py +++ b/fbchat/utils.py @@ -11,6 +11,7 @@ from os.path import basename import warnings import logging import requests +import aenum from .models import * try: @@ -297,3 +298,12 @@ def get_files_from_paths(filenames): yield files for fn, fp, ft in files: fp.close() + + +def enum_extend_if_invalid(enumeration, value): + try: + return enumeration(value) + except ValueError: + log.warning("Failed parsing {}({!r}). Extending enum.".format(enumeration, value)) + aenum.extend_enum(enumeration, "UNKNOWN_{}".format(value).upper(), value) + return enumeration(value) From 45d8b45d96250b9f5f2fee9a376ea05658aa16ec Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 12 Dec 2018 23:22:08 +0100 Subject: [PATCH 6/6] Fix `enum_extend_if_invalid` warning --- fbchat/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fbchat/utils.py b/fbchat/utils.py index d3008dc..5fcf0d6 100644 --- a/fbchat/utils.py +++ b/fbchat/utils.py @@ -304,6 +304,6 @@ def enum_extend_if_invalid(enumeration, value): try: return enumeration(value) except ValueError: - log.warning("Failed parsing {}({!r}). Extending enum.".format(enumeration, value)) + log.warning("Failed parsing {.__name__}({!r}). Extending enum.".format(enumeration, value)) aenum.extend_enum(enumeration, "UNKNOWN_{}".format(value).upper(), value) return enumeration(value)