Skip to content

Instantly share code, notes, and snippets.

@upsuper
Created December 25, 2014 03:36
Show Gist options
  • Select an option

  • Save upsuper/8780a3be8fef6ae0502d to your computer and use it in GitHub Desktop.

Select an option

Save upsuper/8780a3be8fef6ae0502d to your computer and use it in GitHub Desktop.
Customize Firefox Tiles
#!/usr/bin/env python3
import sys as _sys
import json as _json
import base64 as _base64
import logging as _logging
import mimetypes as _mimetypes
import urllib.parse as _url
import configparser as _configparser
def parse_config(file):
config = _configparser.ConfigParser()
config.read(file)
return config
def generate_image_uri(filename):
try:
with open(filename, 'rb') as f:
imagedata = f.read()
except Exception as e:
_logging.error('Cannot open %s: %s', filename, e)
return
imagedata = _base64.b64encode(imagedata)
type, encoding = _mimetypes.guess_type(filename)
if not type:
return
return "data:{};base64,{}".format(type, _url.quote(imagedata))
def get_transparent_image():
return "data:image/png;base64," + \
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQAAAAA3bvkkAAAABGdBTUEAALGPC/" + \
"xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8" + \
"AAAAAnRSTlMAAHaTzTgAAAACYktHRAAB3YoTpAAAAApJREFUCNdjYAAAAAIAAe" + \
"IhvDMAAAAldEVYdGRhdGU6Y3JlYXRlADIwMTQtMTItMjVUMTE6Mjk6MjMrMTE6" + \
"MDCLn0VkAAAAJXRFWHRkYXRlOm1vZGlmeQAyMDE0LTEyLTI1VDExOjI5OjIzKz" + \
"ExOjAw%2BsL92AAAAABJRU5ErkJggg%3D%3D"
def generate_data_item(section):
if 'image' not in section:
return
imageuri = generate_image_uri(section['image'])
if not imageuri or \
'url' not in section or \
'title' not in section:
return
item = {
'url': section['url'],
'title': section['title'],
'type': 'custom',
'imageURI': imageuri,
'bgColor': "",
'enhancedImageURI': get_transparent_image()
}
if 'color' in section:
item['bgColor'] = section['color']
if 'enhanced' in section:
enhancedUri = generate_image_uri(section['enhanced'])
if enhancedUri:
item['enhancedImageURI'] = enhancedUri
return item
def generate_data(config):
data = []
for name in config.sections():
item = generate_data_item(config[name])
if item:
data.append(item)
return data
def generate_json(data, locale='en-US'):
return _json.dumps({locale: data})
def usage():
print("Usage: {} config [locale]".format(_sys.argv[0]))
def main():
if len(_sys.argv) < 2:
usage()
exit(2)
locale = 'en-US'
if len(_sys.argv) >= 3:
locale = _sys.argv[2]
config = parse_config(_sys.argv[1])
data = generate_data(config)
json = generate_json(data, locale)
print(json)
if __name__ == '__main__':
main()
[dxr]
url: http://dxr.mozilla.org/
color: #cccccc
image: dxr.png
title: Mozilla DXR
[bugzilla]
url: https://bugzilla.mozilla.org/
color: #E5E3DC
image: bugzilla.png
title: Bugzilla
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment