Ubuntu – Show personalized banner exhibits on Ubuntu Software Center

pythonsoftware-center

How can I show my own personalized banner exhibits on Ubuntu Software Center?

I've seen some URL definitions on /usr/share/software-center/softwarecenter/enums.py and /usr/share/software-center/softwarecenter/distro/Ubuntu.py so far.

I also tried to track the code from the views to the core. But I got lost. _append_banner_adds calls SoftwareCenterAgent. It calls SpawnHelper. Then I get lost. There are also some calls at SimpleFileDownloader but I'm not able to track them.

Also, I've noticed this entry on the debug log.

2013-02-08 15:07:43,731 - softwarecenter.simplefiledownloader - DEBUG - download_file: http://software-center.ubuntu.com/site_media/exhibits/2012/12/SC_banner_Mixxx_2.png None True

Is there documentation on how this is implemented? Some simple recipe to change the default banners and put my own banners on a clean way would be very helpful.

I suppose I can simply rewrite the _append_banner_adds function, but I'm not very prolific on python and I'd like to understand and use the same methods Ubuntu is using, if possible.

Best Answer

Open up /usr/share/software-center/softwarecenter/backend/scagent.py and edit the beginning of this function, so that it says:

def query_exhibits(self):
    import urllib, json
    class Obj:
      def __init__(self, obj):
        self.obj = obj
      def __getattr__(self, name):
        if name[:2] == "__": return object.__getattr__(self, name)
        return self.obj[name]

    self.emit("exhibits", [Obj(x) for x in json.loads(urllib.urlopen("http://localhost:8800/cgi-bin/bannerlist.py").read())])
    return

You can leave the rest as it is, it will never be reached.

If you want scripting support in your <iframe>, edit

/usr/share/software-center/softwarecenter/ui/gtk3/widgets/exhibits.py

and find settings.set_property("enable-scripts", False). Change False to True.

Now make /var/www/cgi-bin/bannerlist.py and make it executable:

#!/usr/bin/env python
import json

print("Content-type: application/json\n")

print(json.dumps([
{
  "html": "<iframe src='file:/tmp/test.html'></iframe>",
  "title_translated": "Hey dawg",
  "click_url": "http://4chan.org",
  "package_names": ("gimp"),
  "banner_urls": ["file:/"],
  "published": True
},
{
  "html": "<iframe src='http://localhost:8800/cgi-bin/banner.py'></iframe>",
  "title_translated": "Hey dawg",
  "click_url": "http://4chan.org",
  "package_names": ("gimp"),
  "banner_urls": ["file:/"],
  "published": True
}
]))

This demonstrates a generated banner list.

Now make /var/www/cgi-bin/banner.py and make it executable:

#!/usr/bin/env python3
import time
print("Content-type: image/svg+xml\n")
print("""
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  <rect width="300" height="100"
  style="fill:rgba(0,0,255,0.5);stroke-width:1;stroke:rgba(0,0,0,0.5)"/>
  <text x="0" y="25" fill="black">Time is """ + str(time.time()) + """</text>
</svg> 
""")

This demonstrates a generated banner.

You might need to clear the software-center cache. You can do that using rm -rf ~/.cache/software-center.

Obviously you need to put something in /tmp/test.html for the first banner to work.

You also need a webserver running at 8800 with a cgi-bin for this to work. If you don't have this, run this in Bash:

cd /var/www
python -c "import BaseHTTPServer as h, CGIHTTPServer as c;
i = c.CGIHTTPRequestHandler;
i.cgi_directories = ['/cgi-bin'];
h.HTTPServer(('', 8800),i).serve_forever()"

You need to style the iframe to make it fill the space, but you figured that out.

Related Question