Skip to content

Commit

Permalink
Merge pull request whyliam#44 from liszd/develop
Browse files Browse the repository at this point in the history
增加有道智云版本
  • Loading branch information
whyliam authored Aug 19, 2017
2 parents 3d9f67b + c6be429 commit 5b8b104
Show file tree
Hide file tree
Showing 20 changed files with 493 additions and 342 deletions.
Empty file modified 91C343E7-50D8-4B0D-9034-1C16C20DA8D4.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified License
100644 → 100755
Empty file.
26 changes: 20 additions & 6 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# whyliam.workflows.youdao

## 有道翻译 workflow v2.0.1
## 有道翻译 workflow v2.1.0

默认快捷键 `yd`,查看翻译结果。

Expand All @@ -20,15 +20,29 @@

### 下载

[点击下载](https://github.com/liszd/whyliam.workflows.youdao/releases/download/2.0.1/whyliam.workflows.youdao.alfredworkflow)
[点击下载](https://github.com/liszd/whyliam.workflows.youdao/releases/download/2.1.0/whyliam.workflows.youdao.alfredworkflow)

### 安装

1. [下载](https://github.com/liszd/whyliam.workflows.youdao/releases)最新版本双击安装
2. [注册](http://fanyi.youdao.com/openapi?path=data-mode)有道翻译API
3. 在Alfred的设置中填入对应的`Key``Keyfrom`
1\. [下载](https://github.com/liszd/whyliam.workflows.youdao/releases)最新版本双击安装

**老版本有道**

2\. [注册](http://fanyi.youdao.com/openapi?path=data-mode)有道翻译API

3\. 在Alfred的设置中填入对应的`Key``Keyfrom`

![](http://ww2.sinaimg.cn/large/006tNbRwgy1feno2vevklj31a30os0u5.jpg)
4. 在Alfred的设置中设置快捷方式键

**新版本有道智云**

2\. [注册](http://ai.youdao.com/appmgr.s)有道智云应用

3\. 在Alfred的设置中填入对应的`应用ID``应用密钥`

![](https://ws2.sinaimg.cn/large/006tKfTcgy1fiosfttuejj31kw0vb493.jpg)

4\. 在Alfred的设置中设置快捷方式键
![](http://ww2.sinaimg.cn/large/006tNbRwgy1feno6pzaxdj31a60p0jsl.jpg)

### 演示
Expand Down
Empty file modified icon_error.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified icon_phonetic.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified icon_update.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 10 additions & 4 deletions info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@
</dict>
</array>
<key>readme</key>
<string>有道翻译 Workflow v2.0.1
<string>有道翻译 Workflow v2.1.0
默认快捷键 yd, 查看翻译结果。
Expand Down Expand Up @@ -415,17 +415,23 @@
<key>variables</key>
<dict>
<key>youdao_key</key>
<string>1947745091</string>
<string></string>
<key>youdao_keyfrom</key>
<string>whyliam-wf-12</string>
<string></string>
<key>zhiyun_id</key>
<string></string>
<key>zhiyun_key</key>
<string></string>
</dict>
<key>variablesdontexport</key>
<array>
<string>youdao_keyfrom</string>
<string>youdao_key</string>
<string>zhiyun_id</string>
<string>zhiyun_key</string>
</array>
<key>version</key>
<string>2.0.0</string>
<string>2.1.0</string>
<key>webaddress</key>
<string>https://github.com/liszd/whyliam.workflows.youdao</string>
</dict>
Expand Down
Empty file modified splitargs.py
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion version
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.1
2.1.0
Binary file modified whyliam.workflows.youdao.alfredworkflow
Binary file not shown.
Empty file modified workflow/Notify.tgz
100755 → 100644
Empty file.
2 changes: 1 addition & 1 deletion workflow/__init__.py
100755 → 100644
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 workflow/background.py
100755 → 100644
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 5b8b104

Please sign in to comment.