Skip to content

Commit c71f164

Browse files
author
Maurizio Branca
committed
Add command handlers
1 parent fc9dccd commit c71f164

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

arduino-cli

22.1 MB
Binary file not shown.

arduino-cli.py

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# encoding: utf-8
2+
3+
import argparse
4+
import json
5+
import sys
6+
7+
from workflow import Workflow, ICON_WEB, web
8+
9+
log = None
10+
11+
12+
def run_command(cmd):
13+
from invoke import run
14+
15+
cmd = "./arduino-cli {cmd} --format json".format(cmd=cmd)
16+
17+
result = run(cmd, hide=True, warn=True)
18+
if not result.ok:
19+
raise Exception(result.stdout)
20+
21+
return json.loads(result.stdout)
22+
23+
24+
def search_key_for_board(board):
25+
elements = []
26+
elements.append(board["name"])
27+
elements.append(board["FQBN"])
28+
return u" ".join(elements)
29+
30+
31+
class Handler:
32+
33+
def __init__(self, args, wf):
34+
self.args = args
35+
self.wf = wf
36+
37+
def run(self):
38+
39+
name = "handle_{command}_{subcommand}".format(command=self.args.command,
40+
subcommand=self.args.subcommand)
41+
command = getattr(self, name, None)
42+
43+
if not callable(command):
44+
raise Exception("Command not supported")
45+
46+
command()
47+
48+
def handle_core_list(self):
49+
50+
def wrapper():
51+
return run_command("core list")
52+
53+
cores = self.wf.cached_data('cores', wrapper, max_age=120)
54+
55+
if self.args.query:
56+
cores = self.wf.filter(self.args.query,
57+
boards,
58+
key=search_key_for_core,
59+
min_score=20)
60+
61+
for core in cores:
62+
self.wf.add_item(title=core['ID'] + "@" + core['Installed'],
63+
subtitle=core['Name'])
64+
65+
# Send the results to Alfred as XML
66+
self.wf.send_feedback()
67+
68+
def handle_board_listall(self):
69+
70+
def wrapper():
71+
return run_command("board listall").get("boards", [])
72+
73+
boards = self.wf.cached_data('boards', wrapper, max_age=120)
74+
75+
if self.args.query:
76+
boards = self.wf.filter(self.args.query,
77+
boards,
78+
key=search_key_for_board,
79+
min_score=20)
80+
81+
for board in boards:
82+
self.wf.add_item(title=board['name'],
83+
subtitle=board['FQBN'],
84+
icon=ICON_WEB)
85+
86+
# Send the results to Alfred as XML
87+
self.wf.send_feedback()
88+
89+
def handle_version_none(self):
90+
91+
version = run_command("version")
92+
93+
self.wf.add_item(title=version["VersionString"],
94+
subtitle='version')
95+
self.wf.add_item(title=version["Commit"],
96+
subtitle='Commit')
97+
self.wf.add_item(title=version["Status"],
98+
subtitle='Status')
99+
100+
101+
# Send the results to Alfred as XML
102+
self.wf.send_feedback()
103+
104+
105+
def main(wf):
106+
107+
# build argument parser to parse script args and collect their
108+
# values
109+
parser = argparse.ArgumentParser()
110+
111+
# add a required command and save it to 'command'
112+
parser.add_argument('command')
113+
# add a required sub command and save it to 'sub_command'
114+
parser.add_argument('subcommand', nargs='?', default='none')
115+
# add an optional query and save it to 'query'
116+
parser.add_argument('query', nargs='?', default=None)
117+
118+
# parse the script's arguments
119+
args = parser.parse_args(wf.args)
120+
121+
Handler(args, wf).run()
122+
123+
124+
if __name__ == u"__main__":
125+
wf = Workflow(libraries=["./libs"])
126+
log = wf.logger
127+
sys.exit(wf.run(main))

0 commit comments

Comments
 (0)