-
Notifications
You must be signed in to change notification settings - Fork 4
/
workflow.py
73 lines (58 loc) · 1.88 KB
/
workflow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import xml.etree.ElementTree as ET
import sys
from os import environ
from plistlib import readPlist, writePlist
class ItemMod:
Cmd, Ctrl, Alt, Shift, Fn = ('cmd', 'ctrl', 'alt', 'shift', 'fn')
class Item:
def __init__(self, title, subtitle="", icon=None, arg=None, autocomplete=None, valid=False, uid=None, modifierSubtitles=None):
self.title = title
self.subtitle = subtitle
self.icon = icon
self.arg = arg
self.autocomplete = autocomplete
self.valid = valid
self.uid = uid
self.modifierSubtitles = modifierSubtitles if modifierSubtitles is not None else {}
def item_xml(self):
attrib = { "valid": "yes" if self.valid else "no" }
if self.autocomplete is not None:
attrib["autocomplete"] = self.autocomplete
if self.uid is not None:
attrib["uid"] = self.uid
if self.arg is not None:
attrib["arg"] = self.arg
item = ET.Element('item', attrib)
title = ET.SubElement(item, "title")
title.text = self.title
if self.subtitle:
subtitle = ET.SubElement(item, "subtitle")
subtitle.text = self.subtitle
mods = (ItemMod.Cmd, ItemMod.Ctrl, ItemMod.Alt, ItemMod.Shift, ItemMod.Fn)
for mod in mods:
if mod in self.modifierSubtitles:
subtitle = ET.SubElement(item, "subtitle", { "mod": mod })
subtitle.text = self.modifierSubtitles[mod]
if self.icon:
icon = ET.SubElement(item, "icon")
icon.text = self.icon
return item
@staticmethod
def generate_output(items):
root = ET.Element('items')
for item in items:
root.append(item.item_xml())
sys.stdout.write('<?xml version="1.0" encoding="utf-8"?>\n')
sys.stdout.write(ET.tostring(root).encode('utf-8'))
sys.stdout.flush()
def set_variable(name, value):
info = readPlist('info.plist')
# Set a variable
info['variables'][name] = value
# Save changes
writePlist(info, 'info.plist')
def get_variable(name):
if name in environ:
return environ[name]
else:
return None