Skip to content

Commit 592feeb

Browse files
committed
Refactor
1 parent 819c8be commit 592feeb

File tree

4 files changed

+50
-55
lines changed

4 files changed

+50
-55
lines changed

afw.py

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
from ualfred import Workflow3
1313

1414
WORKFLOW_BASE_FILES = ["info.plist", "workflow_main.py", "__init__.py"]
15-
AFW_ENTRY_POINT_FILE = "afw_entry_point.py"
16-
AFW_REQUIREMENTS = []
15+
# AFW_ENTRY_POINT_FILE = "afw_entry_point.py"
16+
AFW_ENTRY_POINT_FILE = "afw.py"
17+
AFW_REQUIREMENTS = ["click"]
1718

1819

1920
@click.group()
@@ -87,6 +88,49 @@ def build(workflow_path: str):
8788
print(f"Create Alfred workflow[{package_path}] finished.")
8889

8990

91+
def main(workflow):
92+
# The Workflow3 instance will be passed to the function
93+
# you call from `Workflow3.run`.
94+
# Not super useful, as the `wf` object created in
95+
# the `if __name__ ...` clause below is global...
96+
#
97+
# Your imports go here if you want to catch import errors, which
98+
# is not a bad idea, or if the modules/packages are in a directory
99+
# added via `Workflow3(libraries=...)`
100+
# import somemodule
101+
# import anothermodule
102+
103+
# Get args from Workflow3, already in normalized Unicode.
104+
# This is also necessary for "magic" arguments to work.
105+
# args = wf.args
106+
107+
# Do stuff here ...
108+
from workflow_main import call_workflow
109+
110+
feedback = call_workflow(workflow)
111+
112+
# Add an item to Alfred feedback
113+
# wf.add_item(u'Item title', u'Item subtitle')
114+
for item in feedback:
115+
workflow.add_item(**item)
116+
117+
# Send output to Alfred. You can only call this once.
118+
# Well, you *can* call it multiple times, but subsequent calls
119+
# are ignored (otherwise the JSON sent to Alfred would be invalid).
120+
workflow.send_feedback()
121+
122+
123+
@cli.command()
124+
@click.argument("query")
125+
def call(query):
126+
# Create a global `Workflow3` object
127+
wf = Workflow3()
128+
# Call your entry function via `Workflow3.run()` to enable its
129+
# helper functions, like exception catching, ARGV normalization,
130+
# magic arguments etc.
131+
sys.exit(wf.run(main))
132+
133+
90134
@cli.command()
91135
@click.argument("workflow_path")
92136
@click.argument("query")

afw_entry_point.py

Lines changed: 0 additions & 47 deletions
This file was deleted.

afw_time_converter/info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
<key>runningsubtext</key>
6161
<string></string>
6262
<key>script</key>
63-
<string>python3 afw_entry_point.py "{query}"</string>
63+
<string>python3 afw.py call "{query}"</string>
6464
<key>scriptargtype</key>
6565
<integer>0</integer>
6666
<key>scriptfile</key>

afw_time_converter/workflow_main.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
#!/usr/bin/env python
2-
3-
41
import re
52

63
import arrow
@@ -75,11 +72,12 @@ def query(self, value):
7572
self._query = value.strip(" ")
7673

7774
def do_parser(self):
78-
self.wf.logger.debug(f"query string:{type(self.wf.args[0])} {self.wf.args[0]}")
75+
query = self.wf.args[1]
76+
self.wf.logger.debug(f"query string:{type(query)} {query}")
7977

8078
try:
8179
# self.query = self.wf.args[0].encode("utf8")
82-
self.query = self.wf.args[0]
80+
self.query = query
8381
except IndexError:
8482
self.wf.logger.debug("parser workflow args failed.")
8583
return False

0 commit comments

Comments
 (0)