Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix enums #371

Merged
merged 7 commits into from
Jan 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions fbchat/graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ def decode(self, s, _w=WHITESPACE.match):
def graphql_color_to_enum(color):
if color is None:
return None
if len(color) == 0:
if not color:
return ThreadColor.MESSENGER_BLUE
Copy link

@Demindiro Demindiro Dec 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that line 30 won't execute because there is a None check before this one (or is the not color intended to catch another value?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The not color is intended to catch the empty string, but I guess that could be clearer

try:
return ThreadColor('#{}'.format(color[2:].lower()))
except ValueError:
raise FBchatException('Could not get ThreadColor from color: {}'.format(color))
color = color[2:] # Strip the alpha value
color_value = '#{}'.format(color.lower())
return enum_extend_if_invalid(ThreadColor, color_value)

def get_customization_info(thread):
if thread is None or thread.get('customization_info') is None:
Expand Down Expand Up @@ -285,7 +284,10 @@ def graphql_to_message(message):
rtn.unsent = False
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')}
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']]
if message.get('extensible_attachment') is not None:
Expand Down
4 changes: 2 additions & 2 deletions fbchat/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: UTF-8 -*-

from __future__ import unicode_literals
import enum
import aenum


class FBchatException(Exception):
Expand Down Expand Up @@ -602,7 +602,7 @@ def __repr__(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))

class Enum(enum.Enum):
class Enum(aenum.Enum):
"""Used internally by fbchat to support enumerations"""
def __repr__(self):
# For documentation:
Expand Down
9 changes: 9 additions & 0 deletions fbchat/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import warnings
import logging
import requests
import aenum
from .models import *

try:
Expand Down Expand Up @@ -300,6 +301,14 @@ def get_files_from_paths(filenames):
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 {.__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)]
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
requests
beautifulsoup4
enum34; python_version < '3.4'
aenum
5 changes: 1 addition & 4 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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'
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

from setuptools import setup

setup(extras_require={':python_version < "3.4"': ['enum34']})
setup()