Skip to content

Commit

Permalink
Workflow3 defaults to Alfred 3 data and cache directories when not ru…
Browse files Browse the repository at this point in the history
…n from Alfred.

Improves behaviour in a shell #95
  • Loading branch information
deanishe committed Jul 9, 2016
1 parent b1ee565 commit 116c453
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 15 deletions.
Binary file not shown.
7 changes: 7 additions & 0 deletions tests/test_workflow3.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,10 @@ def test_item_config(info3):

c = r['config']
assert c['var1'] == 'val2'


def test_default_directories(info3):
"""Workflow3: Default directories."""
wf3 = Workflow3()
assert 'Alfred 3' in wf3.datadir
assert 'Alfred-3' in wf3.cachedir
2 changes: 1 addition & 1 deletion workflow/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.18.2
1.19
9 changes: 3 additions & 6 deletions workflow/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import zlib


USER_AGENT = u'Alfred-Workflow/1.18.2 (+http://www.deanishe.net/alfred-workflow)'
USER_AGENT = u'Alfred-Workflow/1.19 (+http://www.deanishe.net/alfred-workflow)'

# Valid characters for multipart form data boundaries
BOUNDARY_CHARS = string.digits + string.ascii_letters
Expand Down Expand Up @@ -171,7 +171,7 @@ class Response(object):
"""
Returned by :func:`request` / :func:`get` / :func:`post` functions.
A simplified version of the ``Response`` object in the ``requests`` library.
Simplified version of the ``Response`` object in the ``requests`` library.
>>> r = request('http://www.google.com')
>>> r.status_code
Expand Down Expand Up @@ -375,7 +375,7 @@ def generate():
return chunks

def save_to_path(self, filepath):
"""Save retrieved data to file at ``filepath``
"""Save retrieved data to file at ``filepath``.
.. versionadded: 1.9.6
Expand Down Expand Up @@ -496,7 +496,6 @@ def request(method, url, params=None, data=None, headers=None, cookies=None,
will be used.
"""

# TODO: cookies
socket.setdefaulttimeout(timeout)

Expand Down Expand Up @@ -575,7 +574,6 @@ def get(url, params=None, headers=None, cookies=None, auth=None,
:returns: :class:`Response` instance
"""

return request('GET', url, params, headers=headers, cookies=cookies,
auth=auth, timeout=timeout, allow_redirects=allow_redirects,
stream=stream)
Expand Down Expand Up @@ -614,7 +612,6 @@ def encode_multipart_formdata(fields, files):
- ``mimetype`` is optional. If not provided, :mod:`mimetypes` will be used to guess the mimetype, or ``application/octet-stream`` will be used.
"""

def get_content_type(filename):
"""Return or guess mimetype of ``filename``.
Expand Down
22 changes: 16 additions & 6 deletions workflow/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1348,14 +1348,19 @@ def cachedir(self):
dirpath = self.alfred_env.get('workflow_cache')

else:
dirpath = os.path.join(
dirpath = self._default_cachedir

return self._create(dirpath)

@property
def _default_cachedir(self):
"""Alfred 2's default cache directory."""
return os.path.join(
os.path.expanduser(
'~/Library/Caches/com.runningwithcrayons.Alfred-2/'
'Workflow Data/'),
self.bundleid)

return self._create(dirpath)

@property
def datadir(self):
"""Path to workflow's data directory.
Expand All @@ -1374,12 +1379,17 @@ def datadir(self):
dirpath = self.alfred_env.get('workflow_data')

else:
dirpath = os.path.join(os.path.expanduser(
'~/Library/Application Support/Alfred 2/Workflow Data/'),
self.bundleid)
dirpath = self._default_datadir

return self._create(dirpath)

@property
def _default_datadir(self):
"""Alfred 2's default data directory."""
return os.path.join(os.path.expanduser(
'~/Library/Application Support/Alfred 2/Workflow Data/'),
self.bundleid)

@property
def workflowdir(self):
"""Path to workflow's root directory (where ``info.plist`` is).
Expand Down
26 changes: 24 additions & 2 deletions workflow/workflow3.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
#

"""
:class:`Workflow3` is an Alfred 3-only version of
:class:`~workflow.workflow.Workflow`.
:class:`Workflow3` supports Alfred 3's new features.
It is an Alfred 3-only version of :class:`~workflow.workflow.Workflow`.
It supports setting :ref:`workflow-variables` and
:class:`the more advanced modifiers <Modifier>` supported by Alfred 3.
Expand All @@ -23,6 +24,7 @@
from __future__ import print_function, unicode_literals, absolute_import

import json
import os
import sys

from .workflow import Workflow
Expand Down Expand Up @@ -282,9 +284,29 @@ class Workflow3(Workflow):
item_class = Item3

def __init__(self, **kwargs):
"""Create a new :class:`Workflow3` object.
See :class:`~workflow.workflow.Workflow` for documentation.
"""
Workflow.__init__(self, **kwargs)
self.variables = {}

@property
def _default_cachedir(self):
"""Alfred 3's default cache directory."""
return os.path.join(
os.path.expanduser(
'~/Library/Caches/com.runningwithcrayons.Alfred-3/'
'Workflow Data/'),
self.bundleid)

@property
def _default_datadir(self):
"""Alfred 3's default data directory."""
return os.path.join(os.path.expanduser(
'~/Library/Application Support/Alfred 3/Workflow Data/'),
self.bundleid)

def setvar(self, name, value):
"""Set a workflow variable that will be inherited by all new items.
Expand Down

0 comments on commit 116c453

Please sign in to comment.