Skip to content

Commit 62b80c3

Browse files
committed
Added support for the latest Python "Click" package (CLI Builder) // Resolve platformio#349
1 parent 7687a0a commit 62b80c3

File tree

11 files changed

+75
-58
lines changed

11 files changed

+75
-58
lines changed

HISTORY.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ PlatformIO 4.0
1414
* Include external configuration files with `extra_configs <http://docs.platformio.org/page/projectconf/section_platformio.html#extra-configs>`__ option
1515
(`issue #1590 <https://github.com/platformio/platformio-core/issues/1590>`_)
1616
* Override default source and include directories for a library via `library.json <http://docs.platformio.org/page/librarymanager/config.html>`__ manifest using ``includeDir`` and ``srcDir`` fields
17+
* Added support for the latest Python "Click" package (CLI Builder)
18+
(`issue #349 <https://github.com/platformio/platformio-core/issues/349>`_)
1719

1820
PlatformIO 3.0
1921
--------------

platformio/__main__.py

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,49 +14,13 @@
1414

1515
import os
1616
import sys
17-
from os.path import join
1817
from platform import system
1918
from traceback import format_exc
2019

2120
import click
2221

2322
from platformio import __version__, exception, maintenance
24-
from platformio.util import get_source_dir
25-
26-
27-
class PlatformioCLI(click.MultiCommand): # pylint: disable=R0904
28-
29-
def list_commands(self, ctx):
30-
cmds = []
31-
for filename in os.listdir(join(get_source_dir(), "commands")):
32-
if filename.startswith("__init__"):
33-
continue
34-
if filename.endswith(".py"):
35-
cmds.append(filename[:-3])
36-
cmds.sort()
37-
return cmds
38-
39-
def get_command(self, ctx, cmd_name):
40-
mod = None
41-
try:
42-
mod = __import__("platformio.commands." + cmd_name, None, None,
43-
["cli"])
44-
except ImportError:
45-
try:
46-
return self._handle_obsolate_command(cmd_name)
47-
except AttributeError:
48-
raise click.UsageError('No such command "%s"' % cmd_name, ctx)
49-
return mod.cli
50-
51-
@staticmethod
52-
def _handle_obsolate_command(name):
53-
if name == "platforms":
54-
from platformio.commands import platform
55-
return platform.cli
56-
if name == "serialports":
57-
from platformio.commands import device
58-
return device.cli
59-
raise AttributeError()
23+
from platformio.commands import PlatformioCLI
6024

6125

6226
@click.command(

platformio/commands/__init__.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,51 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
15+
import os
16+
from os.path import dirname, join
17+
18+
import click
19+
20+
21+
class PlatformioCLI(click.MultiCommand):
22+
23+
leftover_args = []
24+
25+
def invoke(self, ctx):
26+
PlatformioCLI.leftover_args = ctx.args
27+
if hasattr(ctx, "protected_args"):
28+
PlatformioCLI.leftover_args = ctx.protected_args + ctx.args
29+
return super(PlatformioCLI, self).invoke(ctx)
30+
31+
def list_commands(self, ctx):
32+
cmds = []
33+
for filename in os.listdir(join(dirname(__file__), "commands")):
34+
if filename.startswith("__init__"):
35+
continue
36+
if filename.endswith(".py"):
37+
cmds.append(filename[:-3])
38+
cmds.sort()
39+
return cmds
40+
41+
def get_command(self, ctx, cmd_name):
42+
mod = None
43+
try:
44+
mod = __import__("platformio.commands." + cmd_name, None, None,
45+
["cli"])
46+
except ImportError:
47+
try:
48+
return self._handle_obsolate_command(cmd_name)
49+
except AttributeError:
50+
raise click.UsageError('No such command "%s"' % cmd_name, ctx)
51+
return mod.cli
52+
53+
@staticmethod
54+
def _handle_obsolate_command(name):
55+
if name == "platforms":
56+
from platformio.commands import platform
57+
return platform.cli
58+
if name == "serialports":
59+
from platformio.commands import device
60+
return device.cli
61+
raise AttributeError()

platformio/commands/lib.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import click
2222

2323
from platformio import exception, util
24+
from platformio.commands import PlatformioCLI
2425
from platformio.managers.lib import LibraryManager, get_builtin_libs
2526
from platformio.project.helpers import (
2627
get_project_dir, get_projectlibdeps_dir, is_platformio_project)
@@ -78,7 +79,7 @@ def cli(ctx, **options):
7879
ctx.invoked_subcommand)
7980

8081
ctx.obj = LibraryManager(storage_dir)
81-
if "--json-output" not in ctx.args:
82+
if "--json-output" not in PlatformioCLI.leftover_args:
8283
click.echo("Library Storage: " + storage_dir)
8384

8485

platformio/maintenance.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import semantic_version
2323

2424
from platformio import __version__, app, exception, telemetry, util
25+
from platformio.commands import PlatformioCLI
2526
from platformio.commands.lib import lib_update as cmd_lib_update
2627
from platformio.commands.platform import \
2728
platform_install as cmd_platform_install
@@ -40,12 +41,12 @@ def on_platformio_start(ctx, force, caller):
4041
set_caller(caller)
4142
telemetry.on_command()
4243

43-
if not in_silence(ctx):
44+
if not in_silence():
4445
after_upgrade(ctx)
4546

4647

47-
def on_platformio_end(ctx, result): # pylint: disable=W0613
48-
if in_silence(ctx):
48+
def on_platformio_end(ctx, result): # pylint: disable=unused-argument
49+
if in_silence():
4950
return
5051

5152
try:
@@ -64,14 +65,11 @@ def on_platformio_exception(e):
6465
telemetry.on_exception(e)
6566

6667

67-
def in_silence(ctx=None):
68-
ctx = ctx or app.get_session_var("command_ctx")
69-
if not ctx:
70-
return True
71-
return ctx.args and any([
72-
ctx.args[0] == "debug" and "--interpreter" in " ".join(ctx.args),
73-
ctx.args[0] == "upgrade", "--json-output" in ctx.args,
74-
"--version" in ctx.args
68+
def in_silence():
69+
args = PlatformioCLI.leftover_args
70+
return args and any([
71+
args[0] == "debug" and "--interpreter" in " ".join(args),
72+
args[0] == "upgrade", "--json-output" in args, "--version" in args
7573
])
7674

7775

platformio/managers/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"contrib-piohome": "^2.0.1",
2828
"contrib-pysite":
2929
"~2.%d%d.190418" % (sys.version_info[0], sys.version_info[1]),
30-
"tool-pioplus": "^2.1.4",
30+
"tool-pioplus": "^2.1.5",
3131
"tool-unity": "~1.20403.0",
3232
"tool-scons": "~2.20501.7" if util.PY2 else "~3.30005.0"
3333
}

platformio/telemetry.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import requests
2828

2929
from platformio import __version__, app, exception, util
30+
from platformio.commands import PlatformioCLI
3031

3132
try:
3233
import queue
@@ -133,10 +134,10 @@ def _first_arg_from_list(args_, list_):
133134
return _arg
134135
return None
135136

136-
if not app.get_session_var("command_ctx"):
137-
return
138-
ctx_args = app.get_session_var("command_ctx").args
139-
args = [str(s).lower() for s in ctx_args if not str(s).startswith("-")]
137+
args = [
138+
str(arg).lower() for arg in PlatformioCLI.leftover_args
139+
if not str(arg).startswith("-")
140+
]
140141
if not args:
141142
return
142143
cmd_path = args[:1]

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
install_requires = [
2121
"bottle<0.13",
22-
"click>=5,<6",
22+
"click>=5,<8",
2323
"colorama",
2424
"pyserial>=3,<4,!=3.3",
2525
"requests>=2.4.0,<3",

tests/commands/test_init.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def test_init_duplicated_boards(clirunner, validate_cliresult, tmpdir):
5757
def test_init_ide_without_board(clirunner, tmpdir):
5858
with tmpdir.as_cwd():
5959
result = clirunner.invoke(cmd_init, ["--ide", "atom"])
60-
assert result.exit_code == -1
60+
assert result.exit_code != 0
6161
assert isinstance(result.exception, exception.ProjectEnvsNotAvailable)
6262

6363

tests/commands/test_lib.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
import re
1717

1818
from platformio import exception
19+
from platformio.commands import PlatformioCLI
1920
from platformio.commands.lib import cli as cmd_lib
2021

22+
PlatformioCLI.leftover_args = ["--json-output"] # hook for click
23+
2124

2225
def test_search(clirunner, validate_cliresult):
2326
result = clirunner.invoke(cmd_lib, ["search", "DHT22"])

tests/commands/test_platform.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ def test_search_raw_output(clirunner, validate_cliresult):
3838
def test_install_unknown_version(clirunner):
3939
result = clirunner.invoke(cli_platform.platform_install,
4040
41-
assert result.exit_code == -1
41+
assert result.exit_code != 0
4242
assert isinstance(result.exception, exception.UndefinedPackageVersion)
4343

4444

4545
def test_install_unknown_from_registry(clirunner):
4646
result = clirunner.invoke(cli_platform.platform_install,
4747
["unknown-platform"])
48-
assert result.exit_code == -1
48+
assert result.exit_code != 0
4949
assert isinstance(result.exception, exception.UnknownPackage)
5050

5151

0 commit comments

Comments
 (0)