Skip to content

Commit

Permalink
Explicit path to pint default units to avoid pint issue with non-ASCI…
Browse files Browse the repository at this point in the history
…I paths
  • Loading branch information
deanishe committed Oct 17, 2017
1 parent b461236 commit ec18609
Show file tree
Hide file tree
Showing 13 changed files with 465 additions and 355 deletions.
Binary file not shown.
2 changes: 2 additions & 0 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
# ----------------------------------------------------------------------
# Unit definition files
# ----------------------------------------------------------------------
DEFAULT_UNIT_DEFINITIONS = os.path.join(os.path.dirname(__file__),
'pint/default_en.txt')
CUSTOM_DEFINITIONS_FILENAME = 'unit_definitions.txt'
BUILTIN_UNIT_DEFINITIONS = os.path.join(os.path.dirname(__file__),
CUSTOM_DEFINITIONS_FILENAME)
Expand Down
9 changes: 7 additions & 2 deletions src/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from workflow.background import run_in_background, is_running
from config import (
bootstrap,
DEFAULT_UNIT_DEFINITIONS,
BUILTIN_UNIT_DEFINITIONS,
COPY_UNIT,
CURRENCY_CACHE_AGE,
Expand All @@ -39,8 +40,7 @@
log = None

# Pint objects
ureg = UnitRegistry()
ureg.default_format = 'P'
ureg = None
# Q = ureg.Quantity


Expand Down Expand Up @@ -242,6 +242,7 @@ def register_units():
"""Add built-in and user units to unit registry."""
# Add custom units from workflow and user data
ureg.load_definitions(BUILTIN_UNIT_DEFINITIONS)

user_definitions = wf.datafile(CUSTOM_DEFINITIONS_FILENAME)

# User's custom units
Expand Down Expand Up @@ -355,6 +356,10 @@ def main(wf):
wf (workflow.Workflow): Current Workflow object.
"""
global ureg
ureg = UnitRegistry(wf.decode(DEFAULT_UNIT_DEFINITIONS))
ureg.default_format = 'P'

if not len(wf.args):
return

Expand Down
6 changes: 5 additions & 1 deletion src/info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@
<dict>
<key>alfredfiltersresults</key>
<false/>
<key>alfredfiltersresultsmatchmode</key>
<integer>0</integer>
<key>argumenttrimmode</key>
<integer>0</integer>
<key>argumenttype</key>
Expand Down Expand Up @@ -360,6 +362,8 @@ variables={allvars}</string>
<dict>
<key>alfredfiltersresults</key>
<false/>
<key>alfredfiltersresultsmatchmode</key>
<integer>0</integer>
<key>argumenttrimmode</key>
<integer>0</integer>
<key>argumenttype</key>
Expand Down Expand Up @@ -627,7 +631,7 @@ UPDATE_INTERVAL is the number of minutes between exchange rate updates.</string>
<string>360</string>
</dict>
<key>version</key>
<string>3.0.1</string>
<string>3.0.2</string>
<key>webaddress</key>
<string></string>
</dict>
Expand Down
Empty file removed src/workflow/.alfredversionchecked
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 ec18609

Please sign in to comment.