Skip to content

Commit

Permalink
Merge pull request #56 from liszd/develop
Browse files Browse the repository at this point in the history
显示历史查询记录
  • Loading branch information
whyliam authored May 2, 2018
2 parents d7ac7b0 + 23413df commit 1705fd9
Show file tree
Hide file tree
Showing 17 changed files with 764 additions and 384 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# whyliam.workflows.youdao

## 有道翻译 workflow v2.1.1
## 有道翻译 workflow v2.1.2

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

Expand All @@ -9,6 +9,7 @@
3. 翻译短语句子
4. 打开有道翻译页面,查看详细内容
5. 直接在打出翻译结果
6. 显示历史查询记录

### 功能

Expand All @@ -17,10 +18,11 @@
3.`Command ⌘+回车` 直接在打出翻译结果
4.`Shift ⇧+回车` 直接发音
5. 选中文字 双击`Option ⌥`进行翻译(需要另行设置)
6. `yd *` 显示历史查询记录

### 下载

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

### 安装

Expand All @@ -47,7 +49,7 @@

### 问题

如果新版本有道智云遇到问题,请参见 [错误代码列表](http://ai.youdao.com/docs/api.s#id6)
如果新版本有道智云遇到问题,请参见 [错误代码列表](http://ai.youdao.com/docs/doc-trans-api.s#p06)

### 演示

Expand Down
Empty file added history.log
Empty file.
78 changes: 42 additions & 36 deletions info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
<false/>
</dict>
</array>
<key>5751065C-52C1-4D19-8F7D-03B730BFE440</key>
<array/>
<key>7C1ABC41-3B36-401F-96C7-30BCB39181FF</key>
<array>
<dict>
Expand Down Expand Up @@ -171,6 +173,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 @@ -251,39 +255,6 @@
<key>version</key>
<integer>2</integer>
</dict>
<dict>
<key>config</key>
<dict>
<key>action</key>
<integer>1</integer>
<key>argument</key>
<integer>1</integer>
<key>argumenttext</key>
<string>yd </string>
<key>focusedappvariable</key>
<false/>
<key>focusedappvariablename</key>
<string></string>
<key>hotkey</key>
<integer>-1</integer>
<key>hotmod</key>
<integer>524288</integer>
<key>hotstring</key>
<string>double tap</string>
<key>leftcursor</key>
<false/>
<key>modsmode</key>
<integer>0</integer>
<key>relatedAppsMode</key>
<integer>0</integer>
</dict>
<key>type</key>
<string>alfred.workflow.trigger.hotkey</string>
<key>uid</key>
<string>5751065C-52C1-4D19-8F7D-03B730BFE440</string>
<key>version</key>
<integer>2</integer>
</dict>
<dict>
<key>config</key>
<dict>
Expand Down Expand Up @@ -324,9 +295,42 @@
<key>version</key>
<integer>2</integer>
</dict>
<dict>
<key>config</key>
<dict>
<key>action</key>
<integer>1</integer>
<key>argument</key>
<integer>1</integer>
<key>argumenttext</key>
<string>yd </string>
<key>focusedappvariable</key>
<false/>
<key>focusedappvariablename</key>
<string></string>
<key>hotkey</key>
<integer>0</integer>
<key>hotmod</key>
<integer>0</integer>
<key>hotstring</key>
<string></string>
<key>leftcursor</key>
<false/>
<key>modsmode</key>
<integer>0</integer>
<key>relatedAppsMode</key>
<integer>0</integer>
</dict>
<key>type</key>
<string>alfred.workflow.trigger.hotkey</string>
<key>uid</key>
<string>5751065C-52C1-4D19-8F7D-03B730BFE440</string>
<key>version</key>
<integer>2</integer>
</dict>
</array>
<key>readme</key>
<string>有道翻译 Workflow v2.1.1
<string>有道翻译 Workflow v2.1.2
默认快捷键 yd, 查看翻译结果。
Expand All @@ -335,6 +339,7 @@
3. 翻译短语句子
4. 打开有道翻译页面,查看详细内容
5. 直接在打出翻译结果
6. 显示历史查询记录
功能
Expand All @@ -343,6 +348,7 @@
3. 按`Comman+回车`直接在打出翻译结果
4. 按`Shift+回车`直接发音
5. 选中文字 双击`Command`进行翻译
6. `yd *` 显示历史查询记录
更多
参见 https://blog.naaln.com/2017/04/alfred-youdao-intro/</string>
Expand Down Expand Up @@ -376,7 +382,7 @@
<key>xpos</key>
<integer>270</integer>
<key>ypos</key>
<integer>430</integer>
<integer>440</integer>
</dict>
<key>6A03FDC5-89AC-4F9D-9456-3762ACA751FE</key>
<dict>
Expand Down Expand Up @@ -433,7 +439,7 @@
<string>zhiyun_key</string>
</array>
<key>version</key>
<string>2.1.1</string>
<string>2.1.2</string>
<key>webaddress</key>
<string>https://github.com/liszd/whyliam.workflows.youdao</string>
</dict>
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.1
2.1.2
Binary file modified whyliam.workflows.youdao.alfredworkflow
Binary file not shown.
Empty file added workflow/.alfredversionchecked
Empty file.
Empty file modified workflow/Notify.tgz
100644 → 100755
Empty file.
Empty file modified workflow/__init__.py
100644 → 100755
Empty file.
120 changes: 81 additions & 39 deletions workflow/background.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from __future__ import print_function, unicode_literals

import signal
import sys
import os
import subprocess
Expand Down Expand Up @@ -82,6 +83,31 @@ def _process_exists(pid):
return True


def _job_pid(name):
"""Get PID of job or `None` if job does not exist.
Args:
name (str): Name of job.
Returns:
int: PID of job process (or `None` if job doesn't exist).
"""
pidfile = _pid_file(name)
if not os.path.exists(pidfile):
return

with open(pidfile, 'rb') as fp:
pid = int(fp.read())

if _process_exists(pid):
return pid

try:
os.unlink(pidfile)
except Exception: # pragma: no cover
pass


def is_running(name):
"""Test whether task ``name`` is currently running.
Expand All @@ -91,26 +117,18 @@ def is_running(name):
:rtype: bool
"""
pidfile = _pid_file(name)
if not os.path.exists(pidfile):
return False

with open(pidfile, 'rb') as file_obj:
pid = int(file_obj.read().strip())

if _process_exists(pid):
if _job_pid(name) is not None:
return True

elif os.path.exists(pidfile):
os.unlink(pidfile)

return False


def _background(stdin='/dev/null', stdout='/dev/null',
def _background(pidfile, stdin='/dev/null', stdout='/dev/null',
stderr='/dev/null'): # pragma: no cover
"""Fork the current process into a background daemon.
:param pidfile: file to write PID of daemon process to.
:type pidfile: filepath
:param stdin: where to read input
:type stdin: filepath
:param stdout: where to write stdout output
Expand All @@ -119,24 +137,31 @@ def _background(stdin='/dev/null', stdout='/dev/null',
:type stderr: filepath
"""
def _fork_and_exit_parent(errmsg):
def _fork_and_exit_parent(errmsg, wait=False, write=False):
try:
pid = os.fork()
if pid > 0:
if write: # write PID of child process to `pidfile`
tmp = pidfile + '.tmp'
with open(tmp, 'wb') as fp:
fp.write(str(pid))
os.rename(tmp, pidfile)
if wait: # wait for child process to exit
os.waitpid(pid, 0)
os._exit(0)
except OSError as err:
_log().critical('%s: (%d) %s', errmsg, err.errno, err.strerror)
raise err

# Do first fork.
_fork_and_exit_parent('fork #1 failed')
# Do first fork and wait for second fork to finish.
_fork_and_exit_parent('fork #1 failed', wait=True)

# Decouple from parent environment.
os.chdir(wf().workflowdir)
os.setsid()

# Do second fork.
_fork_and_exit_parent('fork #2 failed')
# Do second fork and write PID to pidfile.
_fork_and_exit_parent('fork #2 failed', write=True)

# Now I am a daemon!
# Redirect standard file descriptors.
Expand All @@ -151,10 +176,30 @@ def _fork_and_exit_parent(errmsg):
os.dup2(se.fileno(), sys.stderr.fileno())


def kill(name, sig=signal.SIGTERM):
"""Send a signal to job ``name`` via :func:`os.kill`.
.. versionadded:: 1.29
Args:
name (str): Name of the job
sig (int, optional): Signal to send (default: SIGTERM)
Returns:
bool: `False` if job isn't running, `True` if signal was sent.
"""
pid = _job_pid(name)
if pid is None:
return False

os.kill(pid, sig)
return True


def run_in_background(name, args, **kwargs):
r"""Cache arguments then call this script again via :func:`subprocess.call`.
:param name: name of task
:param name: name of job
:type name: unicode
:param args: arguments passed as first argument to :func:`subprocess.call`
:param \**kwargs: keyword arguments to :func:`subprocess.call`
Expand Down Expand Up @@ -183,18 +228,20 @@ def run_in_background(name, args, **kwargs):
argcache = _arg_cache(name)

# Cache arguments
with open(argcache, 'wb') as file_obj:
pickle.dump({'args': args, 'kwargs': kwargs}, file_obj)
with open(argcache, 'wb') as fp:
pickle.dump({'args': args, 'kwargs': kwargs}, fp)
_log().debug('[%s] command cached: %s', name, argcache)

# Call this script
cmd = ['/usr/bin/python', __file__, name]
_log().debug('[%s] passing job to background runner: %r', name, cmd)
retcode = subprocess.call(cmd)

if retcode: # pragma: no cover
_log().error('[%s] background runner failed with %d', retcode)
_log().error('[%s] background runner failed with %d', name, retcode)
else:
_log().debug('[%s] background job started', name)

return retcode


Expand All @@ -209,12 +256,17 @@ def main(wf): # pragma: no cover
name = wf.args[0]
argcache = _arg_cache(name)
if not os.path.exists(argcache):
log.critical('[%s] command cache not found: %r', name, argcache)
return 1
msg = '[{0}] command cache not found: {1}'.format(name, argcache)
log.critical(msg)
raise IOError(msg)

# Fork to background and run command
pidfile = _pid_file(name)
_background(pidfile)

# Load cached arguments
with open(argcache, 'rb') as file_obj:
data = pickle.load(file_obj)
with open(argcache, 'rb') as fp:
data = pickle.load(fp)

# Cached arguments
args = data['args']
Expand All @@ -223,28 +275,18 @@ def main(wf): # pragma: no cover
# Delete argument cache file
os.unlink(argcache)

pidfile = _pid_file(name)

# Fork to background
_background()

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

# Run the command
try:
# Run the command
log.debug('[%s] running command: %r', name, args)

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

if retcode:
log.error('[%s] command failed with status %d', name, retcode)

finally:
if os.path.exists(pidfile):
os.unlink(pidfile)
log.debug('[%s] job complete', name)
os.unlink(pidfile)

log.debug('[%s] job complete', name)


if __name__ == '__main__': # pragma: no cover
Expand Down
Empty file modified workflow/notify.py
100644 → 100755
Empty file.
Empty file modified workflow/update.py
100644 → 100755
Empty file.
Loading

0 comments on commit 1705fd9

Please sign in to comment.