Interesting results with fedmsg.meta

The next task my mentor gave me was to convert the messages that were in json dictionary like format into something that is good looking and in human readable form. For that, he asked me to read documentation on fedmsg. Fedmsg is a python package that can be used to send and receive messages to and from applications. After reading it, I found fedmsg.meta module very interesting and something that could solve my problem. It has beautiful functions that can be used to produce nice html and can convert messages into some beautiful string representations, icons, secondary_icon, link, title and subtitle. Also I liked it because it produced interesting results and is easy to learn and use.

I had message in json-dict format

  I wanted to have output like this.

The first thing i do is to convert the messages(dict format) into string.I used

       fedmsg.meta.msg2repr(msg, legacy=False, **config)

You will have to implement this code

>>> import fedmsg.config
>>> import fedmsg.meta
>>> config = fedmsg.config.load_config([], None)
>>> fedmsg.meta.make_processors(**config)
>>> text = fedmsg.meta.msg2repr(msg_in_dict, legacy=False, **config)                        

To get the desired output, I wrote request_wants_html function in this way

# return HTML content else json
if request_wants_html():
# convert string into python dictionary
obj = json.loads(body)
# extract the messages
messageList = obj["raw_messages"]
#using fedmsg.meta function
config = fedmsg.config.load_config([], None)

finalMessageList = []

for msg in messageList:
d = {}
# create primary icon associated with message
icon = fedmsg.meta.msg2repr(msg,legacy=False,**config)
d['icon'] = icon
# create URL associated with message
link = fedmsg.meta.msg2link(msg, legacy=False, **config)
d['link'] = link
# create title associated with message
title = fedmsg.meta.msg2title(msg, legacy=False, **config)
d['title'] = title
# create secondary icon associated with message
secondary_icon = fedmsg.meta.msg2secondary_icon(msg, legacy=False, **config)
d['secondary_icon'] = secondary_icon
subtitle = fedmsg.meta.msg2subtitle(msg, legacy=False, **config)
d['subtitle'] = subtitle


return flask.render_template("raw.html", response=finalMessageList)
To render the title, subtitle, icon, link and secondary_icon, i used the html file this way
{% for dict in response %}
<p><b>{{dict['title']}}</b></p> </br>
<p> {{dict['subtitle']}} </p> </br>
<A HREF={{dict['link']}}><Img SRC= {{dict['icon']}} WIDTH=30 HEIGHT=40></A>
<Img SRC={{dict['secondary_icon']}} WIDTH=30 HEIGHT=40></A>
{% endfor %}

So, this was my patch 2. In my coming blogs I will be sharing with you a short introduction to jinja2 framework.

Happy learning 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s