Skip to content

Instantly share code, notes, and snippets.

@ara4n
Last active February 18, 2018 00:05
Show Gist options
  • Save ara4n/9e19d51f751043aa2c02d26405c1c390 to your computer and use it in GitHub Desktop.
Save ara4n/9e19d51f751043aa2c02d26405c1c390 to your computer and use it in GitHub Desktop.
How i'd parse display_hints
#!/usr/bin/env python
# see https://docs.google.com/document/d/1m4VTRqclB3JEMZBjbr4t5cvIMQUNSGxy6rYN4YtopIk/edit#heading=h.92ptkhvsmxtd
def recognised(type):
# a smart client who understands indexed types will strip off any indexing:
# import re
# type = re.sub(r'\.\d+$', '', type)
# a typical smart client:
return type in ["m.calendar.request", "m.location", "m.file", "m.thumbnail", "m.html", "m.text"]
# or a typical dumb client:
# return type in ["m.file", "m.thumbnail", "m.html", "m.text"]
# or a very dumb client
# return type in ["m.text"]
def parse_display_hints(display_hints):
for hint in display_hints:
display_types = {}
target_type_count = len(hint)
for term in hint:
if type(term) is str and recognised(term):
display_types[term] = True
elif type(term) is list:
for option in term:
if option is None:
target_type_count -= 1
elif recognised(option):
display_types[option] = True
break
if len(display_types) == target_type_count:
return display_types
display_hints = [
[ "m.calendar.request", [ "m.location", None ], "m.file" ],
[ "m.file", [ "m.thumbnail.1", "m.thumbnail", None ], [ "m.html", "m.markdown", "m.text" ] ],
[ "m.html" ], # implicit
[ "m.text" ], # implicit
]
print parse_display_hints(display_hints).keys()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment