Skip to content

Commit

Permalink
Update Alfred-Workflow to 1.27 (requires Alfred 3.4.1)
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed Jul 23, 2017
1 parent 627ad95 commit 6ed10fa
Show file tree
Hide file tree
Showing 12 changed files with 459 additions and 365 deletions.
Binary file removed Fixum-0.3.alfredworkflow
Binary file not shown.
Binary file added Fixum-0.4.alfredworkflow
Binary file not shown.
94 changes: 48 additions & 46 deletions src/info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,52 @@
<dict>
<key>config</key>
<dict>
<key>lastpathcomponent</key>
<false/>
<key>onlyshowifquerypopulated</key>
<true/>
<key>removeextension</key>
<key>concurrently</key>
<false/>
<key>text</key>
<string>{query}</string>
<key>title</key>
<string>Fixum</string>
<key>escaping</key>
<integer>102</integer>
<key>script</key>
<string>mode=$1
datadir="$alfred_workflow_data"
cachedir="$alfred_workflow_cache"
blacklist="${datadir}/blacklist.txt"
logfile="${cachedir}/net.deanishe.alfred.fixum.log"
# create data &amp; cache directories, logfile and blacklist
test -d "$cachedir" || mkdir -p "$cachedir"
test -f "$logfile" || touch "$logfile"
test -d "$datadir" || mkdir -p "$datadir"
test -f "$blacklist" || cp blacklist.default.txt "$blacklist"
# script actions
[[ "$mode" = dryrun ]] &amp;&amp; /usr/bin/python fixum.py --nothing
[[ "$mode" = fix ]] &amp;&amp; /usr/bin/python fixum.py
[[ "$mode" = blacklist ]] &amp;&amp; open "$blacklist"
[[ "$mode" = log ]] &amp;&amp; open -a Console "$logfile"
exit 0</string>
<key>scriptargtype</key>
<integer>1</integer>
<key>scriptfile</key>
<string></string>
<key>type</key>
<integer>5</integer>
</dict>
<key>type</key>
<string>alfred.workflow.output.notification</string>
<string>alfred.workflow.action.script</string>
<key>uid</key>
<string>90302262-60E4-4C1C-AAEA-2A5C3F4C025A</string>
<string>97033D94-9B6F-446C-94E5-AB677B5ABB4F</string>
<key>version</key>
<integer>1</integer>
<integer>2</integer>
</dict>
<dict>
<key>config</key>
<dict>
<key>alfredfiltersresults</key>
<false/>
<key>argumenttrimmode</key>
<integer>0</integer>
<key>argumenttype</key>
<integer>1</integer>
<key>escaping</key>
Expand Down Expand Up @@ -112,44 +135,23 @@
<dict>
<key>config</key>
<dict>
<key>concurrently</key>
<key>lastpathcomponent</key>
<false/>
<key>escaping</key>
<integer>102</integer>
<key>script</key>
<string>mode=$1
datadir="$alfred_workflow_data"
cachedir="$alfred_workflow_cache"
blacklist="${datadir}/blacklist.txt"
logfile="${cachedir}/net.deanishe.alfred.fixum.log"
# create data &amp; cache directories, logfile and blacklist
test -d "$cachedir" || mkdir -p "$cachedir"
test -f "$logfile" || touch "$logfile"
test -d "$datadir" || mkdir -p "$datadir"
test -f "$blacklist" || cp blacklist.default.txt "$blacklist"
# script actions
[[ "$mode" = dryrun ]] &amp;&amp; /usr/bin/python fixum.py --nothing
[[ "$mode" = fix ]] &amp;&amp; /usr/bin/python fixum.py
[[ "$mode" = blacklist ]] &amp;&amp; open "$blacklist"
[[ "$mode" = log ]] &amp;&amp; open -a Console "$logfile"
exit 0</string>
<key>scriptargtype</key>
<integer>1</integer>
<key>scriptfile</key>
<string></string>
<key>type</key>
<integer>5</integer>
<key>onlyshowifquerypopulated</key>
<true/>
<key>removeextension</key>
<false/>
<key>text</key>
<string>{query}</string>
<key>title</key>
<string>Fixum</string>
</dict>
<key>type</key>
<string>alfred.workflow.action.script</string>
<string>alfred.workflow.output.notification</string>
<key>uid</key>
<string>97033D94-9B6F-446C-94E5-AB677B5ABB4F</string>
<string>90302262-60E4-4C1C-AAEA-2A5C3F4C025A</string>
<key>version</key>
<integer>2</integer>
<integer>1</integer>
</dict>
</array>
<key>readme</key>
Expand Down Expand Up @@ -181,7 +183,7 @@ It is primarily a workaround to fix bugs that are preventing the workflows from
</dict>
</dict>
<key>version</key>
<string>0.3</string>
<string>0.4</string>
<key>webaddress</key>
<string></string>
</dict>
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion src/workflow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
__version__ = open(os.path.join(os.path.dirname(__file__), 'version')).read()
__author__ = 'Dean Jackson'
__licence__ = 'MIT'
__copyright__ = 'Copyright 2014 Dean Jackson'
__copyright__ = 'Copyright 2014-2017 Dean Jackson'

__all__ = [
'Variables',
Expand Down
53 changes: 31 additions & 22 deletions src/workflow/background.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
# Created on 2014-04-06
#

"""Run background tasks."""
"""
This module provides an API to run commands in background processes.
Combine with the :ref:`caching API <caching-data>` to work from cached data
while you fetch fresh data in the background.
See :ref:`the User Manual <background-processes>` for more information
and examples.
"""

from __future__ import print_function, unicode_literals

Expand All @@ -31,6 +38,10 @@ def wf():
return _wf


def _log():
return wf().logger


def _arg_cache(name):
"""Return path to pickle cache file for arguments.
Expand All @@ -40,7 +51,7 @@ def _arg_cache(name):
:rtype: ``unicode`` filepath
"""
return wf().cachefile('{0}.argcache'.format(name))
return wf().cachefile(name + '.argcache')


def _pid_file(name):
Expand All @@ -52,7 +63,7 @@ def _pid_file(name):
:rtype: ``unicode`` filepath
"""
return wf().cachefile('{0}.pid'.format(name))
return wf().cachefile(name + '.pid')


def _process_exists(pid):
Expand All @@ -72,12 +83,12 @@ def _process_exists(pid):


def is_running(name):
"""Test whether task is running under ``name``.
"""Test whether task ``name`` is currently running.
:param name: name of task
:type name: ``unicode``
:type name: unicode
:returns: ``True`` if task with name ``name`` is running, else ``False``
:rtype: ``Boolean``
:rtype: bool
"""
pidfile = _pid_file(name)
Expand Down Expand Up @@ -114,8 +125,7 @@ def _fork_and_exit_parent(errmsg):
if pid > 0:
os._exit(0)
except OSError as err:
wf().logger.critical('%s: (%d) %s', errmsg, err.errno,
err.strerror)
_log().critical('%s: (%d) %s', errmsg, err.errno, err.strerror)
raise err

# Do first fork.
Expand Down Expand Up @@ -145,11 +155,11 @@ def run_in_background(name, args, **kwargs):
r"""Cache arguments then call this script again via :func:`subprocess.call`.
:param name: name of task
:type name: ``unicode``
:type name: unicode
:param args: arguments passed as first argument to :func:`subprocess.call`
:param \**kwargs: keyword arguments to :func:`subprocess.call`
:returns: exit code of sub-process
:rtype: ``int``
:rtype: int
When you call this function, it caches its arguments and then calls
``background.py`` in a subprocess. The Python subprocess will load the
Expand All @@ -167,24 +177,24 @@ def run_in_background(name, args, **kwargs):
"""
if is_running(name):
wf().logger.info('Task `{0}` is already running'.format(name))
_log().info('[%s] job already running', name)
return

argcache = _arg_cache(name)

# Cache arguments
with open(argcache, 'wb') as file_obj:
pickle.dump({'args': args, 'kwargs': kwargs}, file_obj)
wf().logger.debug('Command arguments cached to `{0}`'.format(argcache))
_log().debug('[%s] command cached: %s', name, argcache)

# Call this script
cmd = ['/usr/bin/python', __file__, name]
wf().logger.debug('Calling {0!r} ...'.format(cmd))
_log().debug('[%s] passing job to background runner: %r', name, cmd)
retcode = subprocess.call(cmd)
if retcode: # pragma: no cover
wf().logger.error('Failed to call task in background')
_log().error('[%s] background runner failed with %d', retcode)
else:
wf().logger.debug('Executing task `{0}` in background...'.format(name))
_log().debug('[%s] background job started', name)
return retcode


Expand All @@ -195,10 +205,11 @@ def main(wf): # pragma: no cover
:meth:`subprocess.call` with cached arguments.
"""
log = wf.logger
name = wf.args[0]
argcache = _arg_cache(name)
if not os.path.exists(argcache):
wf.logger.critical('No arg cache found : {0!r}'.format(argcache))
log.critical('[%s] command cache not found: %r', name, argcache)
return 1

# Load cached arguments
Expand All @@ -219,23 +230,21 @@ def main(wf): # pragma: no cover

# Write PID to file
with open(pidfile, 'wb') as file_obj:
file_obj.write('{0}'.format(os.getpid()))
file_obj.write(str(os.getpid()))

# Run the command
try:
wf.logger.debug('Task `{0}` running'.format(name))
wf.logger.debug('cmd : {0!r}'.format(args))
log.debug('[%s] running command: %r', name, args)

retcode = subprocess.call(args, **kwargs)

if retcode:
wf.logger.error('Command failed with [{0}] : {1!r}'.format(
retcode, args))
log.error('[%s] command failed with status %d', name, retcode)

finally:
if os.path.exists(pidfile):
os.unlink(pidfile)
wf.logger.debug('Task `{0}` finished'.format(name))
log.debug('[%s] job complete', name)


if __name__ == '__main__': # pragma: no cover
Expand Down
Loading

0 comments on commit 6ed10fa

Please sign in to comment.