Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lean/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from os import environ
from copy import copy

if environ.get("_LEAN_COMPLETE", "").startswith("powershell_"):
from lean.components.util.click_shell_autocomplete import register_shell_autocomplete
register_shell_autocomplete()

from lean.commands.lean import lean
from lean.commands.autocomplete import autocomplete
from lean.commands.backtest import backtest
from lean.commands.build import build
from lean.commands.cloud import cloud
Expand All @@ -36,6 +44,11 @@
from lean.commands.private_cloud import private_cloud

lean.add_command(config)
lean.add_command(autocomplete)
completion = copy(autocomplete)
completion.name = "completion"
completion.hidden = True
lean.add_command(completion)
lean.add_command(cloud)
lean.add_command(data)
lean.add_command(decrypt)
Expand Down
100 changes: 100 additions & 0 deletions lean/commands/autocomplete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
# Lean CLI v1.0. Copyright 2021 QuantConnect Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Optional

from click import Choice, ClickException, Context, echo, group, option, pass_context

from lean.components.util.click_aliased_command_group import AliasedCommandGroup


SHELL_OPTION = option("--shell",
"-s",
type=Choice(["powershell", "bash", "zsh", "fish"], case_sensitive=False),
default=None,
help="Target shell. Auto-detected if not specified.")


def _profile_permission_error(exception: PermissionError) -> ClickException:
path = exception.filename or "the shell profile"
return ClickException(
f"Unable to update {path}. "
"The current PowerShell session is still disabled if Lean autocomplete was loaded there. "
"To remove it permanently, close any editor or terminal using that profile, or remove the Lean autocomplete block manually."
)


@group(cls=AliasedCommandGroup, invoke_without_command=True)
@SHELL_OPTION
@pass_context
def autocomplete(ctx: Context, shell: Optional[str]) -> None:
"""Print the native shell autocomplete script for your shell.

\b
PowerShell (current session):
lean autocomplete --shell powershell | Out-String | Invoke-Expression

\b
Bash or Zsh (current session):
eval "$(lean autocomplete --shell bash)"

\b
Fish (current session):
lean autocomplete --shell fish | source
"""
if ctx.invoked_subcommand is None:
from lean.components.util.click_shell_autocomplete import get_autocomplete_script
echo(get_autocomplete_script(shell))


@autocomplete.command(name="show", help="Print the native shell autocomplete script for your shell")
@SHELL_OPTION
def show(shell: Optional[str]) -> None:
from lean.components.util.click_shell_autocomplete import get_autocomplete_script
echo(get_autocomplete_script(shell))


@autocomplete.command(name="on", help="Enable shell autocomplete in your shell profile")
@SHELL_OPTION
def on(shell: Optional[str]) -> None:
from lean.components.util.click_shell_autocomplete import install_autocomplete
try:
profile_path = install_autocomplete(shell)
except PermissionError as exception:
raise _profile_permission_error(exception)

echo(f"Enabled shell autocomplete in {profile_path}")
echo("Open a new terminal session for the change to take effect.")


@autocomplete.command(name="off", help="Disable shell autocomplete in your shell profile")
@option("--current-session", is_flag=True, help="Print a script that disables autocomplete in the current shell session.")
@SHELL_OPTION
def off(shell: Optional[str], current_session: bool) -> None:
from lean.components.util.click_shell_autocomplete import get_autocomplete_cleanup_script, uninstall_autocomplete

if current_session:
echo(get_autocomplete_cleanup_script(shell))
return

try:
profile_path, removed = uninstall_autocomplete(shell)
except PermissionError as exception:
raise _profile_permission_error(exception)

if removed:
echo(f"Disabled shell autocomplete in {profile_path}")
echo("Open a new terminal session for the change to take effect.")
echo("To disable it in this PowerShell session, run `lean autocomplete off` again after reloading the updated script.")
else:
echo(f"Shell autocomplete was not enabled in {profile_path}")
3 changes: 2 additions & 1 deletion lean/commands/cloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# limitations under the License.

from click import group
from lean.components.util.click_aliased_command_group import AliasedCommandGroup

from lean.commands.cloud.backtest import backtest
from lean.commands.cloud.live.live import live
Expand All @@ -21,7 +22,7 @@
from lean.commands.cloud.status import status
from lean.commands.cloud.object_store import object_store

@group()
@group(cls=AliasedCommandGroup)
def cloud() -> None:
"""Interact with the QuantConnect cloud."""
# This method is intentionally empty
Expand Down
3 changes: 2 additions & 1 deletion lean/commands/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
# limitations under the License.

from click import group
from lean.components.util.click_aliased_command_group import AliasedCommandGroup

from lean.commands.config.get import get
from lean.commands.config.list import list
from lean.commands.config.set import set
from lean.commands.config.unset import unset


@group()
@group(cls=AliasedCommandGroup)
def config() -> None:
"""Configure Lean CLI options."""
# This method is intentionally empty
Expand Down
3 changes: 2 additions & 1 deletion lean/commands/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
# limitations under the License.

from click import group
from lean.components.util.click_aliased_command_group import AliasedCommandGroup

from lean.commands.data.download import download
from lean.commands.data.generate import generate


@group()
@group(cls=AliasedCommandGroup)
def data() -> None:
"""Download or generate data for local use."""
# This method is intentionally empty
Expand Down
1 change: 1 addition & 0 deletions lean/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ def init(organization: Optional[str], language: Optional[str]) -> None:
- Synchronizing projects with the cloud: https://www.lean.io/docs/v2/lean-cli/projects/cloud-synchronization

Here are some commands to get you going:
- Run `lean autocomplete --shell powershell | Out-String | Invoke-Expression` to enable PowerShell autocomplete in the current session
- Run `lean create-project "My Project"` to create a new project with starter code
- Run `lean cloud pull` to download all your QuantConnect projects to your local drive
- Run `lean backtest "My Project"` to backtest a project locally with the data in {DEFAULT_DATA_DIRECTORY_NAME}/
Expand Down
3 changes: 2 additions & 1 deletion lean/commands/library/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
# limitations under the License.

from click import group
from lean.components.util.click_aliased_command_group import AliasedCommandGroup

from lean.commands.library.add import add
from lean.commands.library.remove import remove


@group()
@group(cls=AliasedCommandGroup)
def library() -> None:
"""Manage custom libraries in a project."""
# This method is intentionally empty
Expand Down
3 changes: 2 additions & 1 deletion lean/commands/private_cloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
# limitations under the License.

from click import group
from lean.components.util.click_aliased_command_group import AliasedCommandGroup

from lean.commands.private_cloud.start import start
from lean.commands.private_cloud.stop import stop
from lean.commands.private_cloud.add_compute import add_compute


@group()
@group(cls=AliasedCommandGroup)
def private_cloud() -> None:
"""Interact with a QuantConnect private cloud."""
# This method is intentionally empty
Expand Down
77 changes: 64 additions & 13 deletions lean/components/util/click_aliased_command_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,86 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from click import Group
from typing import Any, Callable, Optional, Union, overload

from click import Command, Context, Group


CommandCallback = Callable[..., Any]
CommandDecorator = Callable[[CommandCallback], Command]


class AliasedCommandGroup(Group):
"""A click.Group wrapper that implements command aliasing."""
"""A click.Group wrapper that implements command aliasing and autocomplete prefix matching."""

def get_command(self, ctx: Context, cmd_name: str) -> Optional[Command]:
rv = super().get_command(ctx, cmd_name)
if rv is not None:
return rv

matches = []
for name in self.list_commands(ctx):
command = super().get_command(ctx, name)
if command is not None and not command.hidden and name.startswith(cmd_name):
matches.append(name)

if not matches:
return None
elif len(matches) == 1:
return super().get_command(ctx, matches[0])

ctx.fail(f"Too many matches: {', '.join(sorted(matches))}")
Comment thread
shreejaykurhade marked this conversation as resolved.

def command(self, *args, **kwargs):
@overload
def command(self, __func: CommandCallback) -> Command:
...

@overload
def command(self, *args: Any, **kwargs: Any) -> CommandDecorator:
...

def command(self, *args: Any, **kwargs: Any) -> Union[CommandDecorator, Command]:
aliases = kwargs.pop('aliases', [])

if not args:
cmd_name = kwargs.pop("name", "")
else:
cmd_name = args[0]
args = args[1:]
if not aliases:
return super().command(*args, **kwargs)

func = None
if args and callable(args[0]):
assert len(args) == 1, "Use 'command(**kwargs)(callable)' to provide arguments."
func = args[0]
args = ()

alias_help = f"Alias for '{cmd_name}'"
def _decorator(f: CommandCallback) -> Command:
cmd_kwargs = dict(kwargs)
cmd_name = cmd_kwargs.pop("name", None)

if args:
if cmd_name is None:
cmd_name = args[0]
cmd_args = args[1:]
else:
cmd_args = args
else:
cmd_name = cmd_name or f.__name__.lower().replace("_", "-")
cmd_args = ()

alias_help = f"Alias for '{cmd_name}'"

def _decorator(f):
# Add the main command
cmd = super(AliasedCommandGroup, self).command(name=cmd_name, *args, **kwargs)(f)
cmd = super(AliasedCommandGroup, self).command(*cmd_args, name=cmd_name, **cmd_kwargs)(f)

# Add a command to the group for each alias with the same callback but using the alias as name
for alias in aliases:
alias_cmd = super(AliasedCommandGroup, self).command(name=alias,
short_help=alias_help,
*args,
**kwargs)(f)
*cmd_args,
**cmd_kwargs)(f)
alias_cmd.params = cmd.params

return cmd

if func is not None:
return _decorator(func)

return _decorator
4 changes: 2 additions & 2 deletions lean/components/util/click_group_default_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from click import Group
from lean.components.util.click_aliased_command_group import AliasedCommandGroup

class DefaultCommandGroup(Group):
class DefaultCommandGroup(AliasedCommandGroup):
"""allow a default command for a group"""

def command(self, *args, **kwargs):
Expand Down
Loading