Created
February 8, 2016 20:46
-
-
Save projectweekend/0c0c48be955c63e26b79 to your computer and use it in GitHub Desktop.
Helper function (build_xml) used to generate Alfred items XML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from .alfred_script_filter_xml import build_xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import xml.etree.ElementTree as et | |
def build_xml(config): | |
""" | |
config: any iterable containing config dictionaries for <item> elements. | |
"XML Output Format" section at this link has Alfred specific info: | |
https://www.alfredapp.com/help/workflows/inputs/script-filter/ | |
Example config dictionary: | |
``` | |
{ | |
'attributes': { | |
'uid': 'string', # optional | |
'arg': 'string', # recommended | |
'valid': 'string', # optional, (yes | no), defaults to yes | |
'autocomplete': 'string', # recommended | |
'type': 'string' # optional, (default | file | file:skipcheck), defaults to 'default' | |
}, | |
'elements': { | |
'title': 'string', # required | |
'subtitle': 'string', # optional | |
'icon_path': 'string', # required | |
'icon_type': 'string' # optional, (fileicon | filetype) | |
} | |
} | |
``` | |
""" | |
items = et.Element('items') | |
for item in config: | |
add_item_from_config(parent=items, config=item) | |
tree = et.ElementTree(items) | |
et.dump(tree) | |
def add_item_from_config(parent, config): | |
item = et.SubElement(parent=parent, tag='item', attrib=config['attributes']) | |
title = et.SubElement(parent=item, tag='title') | |
title.text = config['elements']['title'] | |
subtitle_text = config['elements'].get('subtitle', None) | |
if subtitle_text: | |
subtitle = et.SubElement(parent=item, tag='subtitle') | |
subtitle.text = subtitle_text | |
icon_path = config['elements']['icon_path'] | |
icon_type = config['elements'].get('icon_type', None) | |
icon = et.SubElement(parent=item, tag='icon', attrib={'type': icon_type}) | |
icon.text = icon_path |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment