I have a bug in my workflow, and I don´t understand the cause - so I thought maybe some of you have experienced the same problem.
My workflow is a script filter getting a wifi-scan terminal output and populates the workflow with available networks to connect. Most of the time, it is working fine. However, sometimes Alfred shows no items. It seems to me that this happens when the workflow hasn´t been used for a while, but I haven´t tested that hypothesis extensively.
I use the Python Library in my script filter.
Anyone experienced something similar? It takes a few seconds to scan and import to the script, can that be the issue?
PS: Is there a spoiler-function here?
# -*- coding: utf-8 -*-
"""This populates the workflow with networks available for connection
using the tool "m-cli" or "Swiss Army Knife" command line tools.
"""
import sys
import subprocess
from workflow import Workflow
def getNetwork():
"""This fetches a network scan using m-cli"""
subprocess.call("./get_network.sh", shell=True)
networkScan = []
with open('networkScan.txt') as inputfile:
for line in inputfile:
networkScan.append(line.strip().split(','))
scanNames = []
scanRS = []
for i in xrange(len(networkScan) - 1):
results_line = networkScan[i + 1][0].split(":", 1)
scanRS.append(results_line[1][15:18])
scanNames.append(results_line[0][0:-3])
return scanNames, scanRS
def search_key(key):
"""This function is a search key for the wf filter.
It means that the Title of the item is the only relevance for the
search filter"""
elements = []
elements.append(key['title'])
return u' '.join(elements)
def main(wf):
"""This function generates actionable items in Workflow"""
if len(wf.args):
query = wf.args[0]
else:
query = None
networks = wf.cached_data('networks', getNetwork, max_age=30)
items = []
for i in xrange(len(networks[0])):
if int(networks[1][i]) > -55:
newItem = dict(title=networks[0][i],
subtitle="Signal strength " + networks[1][i],
autocomplete=networks[0][i],
arg=networks[0][i],
icon="wifi_100.png")
elif int(networks[1][i]) > -75:
newItem = dict(title=networks[0][i],
subtitle="Signal strength " + networks[1][i],
autocomplete=networks[0][i],
arg=networks[0][i],
icon="wifi_66.png")
else:
newItem = dict(title=networks[0][i],
subtitle="Signal strength" + networks[1][i],
autocomplete=networks[0][i],
arg=networks[0][i],
icon="wifi_33.png")
items.append(newItem)
if query:
items = wf.filter(query, items, key=search_key)
# if not items: # This is when there are no items, no data
# wf.add_item('no WiFi connections found',
# icon=u"bummer_fax.jpeg")
# wf.send_feedback()
# return 0
for item in items:
wf.add_item(title=item["title"],
subtitle=item["subtitle"],
autocomplete=item['autocomplete'],
arg=item["arg"],
valid=True,
icon=item["icon"])
wf.send_feedback()
if __name__ == u'__main__':
wf = Workflow()
sys.exit(wf.run(main))