Skip to content

Commit 819c8be

Browse files
committed
Refactor
1 parent f5bdd6e commit 819c8be

File tree

5 files changed

+24
-41
lines changed

5 files changed

+24
-41
lines changed

afw.py

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,19 @@
11
#!/usr/bin/env python
22

3+
import importlib
34
import json
45
import subprocess
6+
import sys
57
from pathlib import Path
68
from uuid import uuid4
79
from zipfile import ZipFile
810

911
import click
12+
from ualfred import Workflow3
1013

11-
FILENAME_LIST = (
12-
"info.plist",
13-
"entry_point.py",
14-
"core.py",
15-
)
16-
17-
PATH_LIST = (
18-
"arrow",
19-
"backports",
20-
"dateutil",
21-
"ualfred",
22-
)
23-
24-
25-
# def main():
26-
# with open("version") as f:
27-
# version = f.readline().rstrip("\n\r ")
28-
#
29-
# zip_filename = f"time-converter.{version}.alfredworkflow"
30-
# z = ZipFile(zip_filename, mode="w")
31-
#
32-
# for filename in FILENAME_LIST:
33-
# z.write(filename)
34-
#
35-
# for pathname in PATH_LIST:
36-
# for root, dirs, files in walk(pathname):
37-
# for filename in files:
38-
# if filename.rfind(".pyc") == -1:
39-
# z.write(join(root, filename))
40-
#
41-
# z.close()
42-
#
43-
# print(f"Create Alfred workflow({zip_filename}) finished.")
14+
WORKFLOW_BASE_FILES = ["info.plist", "workflow_main.py", "__init__.py"]
15+
AFW_ENTRY_POINT_FILE = "afw_entry_point.py"
16+
AFW_REQUIREMENTS = []
4417

4518

4619
@click.group()
@@ -76,6 +49,7 @@ def build(workflow_path: str):
7649

7750
requirements.append(line)
7851

52+
requirements += AFW_REQUIREMENTS
7953
for requirement in requirements:
8054
subprocess.run(
8155
["pip", "install", "-U", f"--target={str(build_path)}", requirement],
@@ -99,22 +73,31 @@ def build(workflow_path: str):
9973
package_file.write(file, arcname=arc_name)
10074

10175
print("Add workflow files...")
102-
workflow_files: list[str] = ["info.plist", "main.py", "__init__.py"]
76+
workflow_files: list[str] = WORKFLOW_BASE_FILES
10377
icon_file = package_info.get("icon")
10478
if icon_file:
10579
workflow_files.append(icon_file)
10680

10781
for file in workflow_files:
10882
package_file.write(workflow_path.joinpath(file), arcname=file)
10983

110-
package_file.write("entry_point.py")
84+
package_file.write(AFW_ENTRY_POINT_FILE)
11185

11286
package_file.close()
11387
print(f"Create Alfred workflow[{package_path}] finished.")
11488

11589

11690
@cli.command()
117-
def test():
91+
@click.argument("workflow_path")
92+
@click.argument("query")
93+
def test(workflow_path: str, query: str):
94+
try:
95+
module = importlib.import_module(workflow_path)
96+
except ImportError as e:
97+
print(e)
98+
pass
99+
100+
print(module.call_workflow(wf=Workflow3()))
118101
click.echo("test...todo")
119102

120103

entry_point.py renamed to afw_entry_point.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
import sys
44

5-
from main import do_workflow
6-
75
# Workflow3 supports Alfred 3's new features. The `Workflow` class
86
# is also compatible with Alfred 2.
97
from ualfred import Workflow3
8+
from workflow_main import call_workflow
109

1110

1211
def main(workflow):
@@ -26,7 +25,7 @@ def main(workflow):
2625
# args = wf.args
2726

2827
# Do stuff here ...
29-
feedback = do_workflow(workflow)
28+
feedback = call_workflow(workflow)
3029

3130
# Add an item to Alfred feedback
3231
# wf.add_item(u'Item title', u'Item subtitle')

afw_time_converter/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .workflow_main import call_workflow

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 entry_point.py "{query}"</string>
63+
<string>python3 afw_entry_point.py "{query}"</string>
6464
<key>scriptargtype</key>
6565
<integer>0</integer>
6666
<key>scriptfile</key>

afw_time_converter/main.py renamed to afw_time_converter/workflow_main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def get_feedback(self):
203203
return f
204204

205205

206-
def do_workflow(wf):
206+
def call_workflow(wf):
207207
time = Time(wf)
208208
time.do_parser()
209209

0 commit comments

Comments
 (0)