Skip to content

Commit 43e1875

Browse files
authored
Add deprecation mechanism (#84)
* Add deprecation mechanisms. * Fix up tests. * Improve Unicode support in displaying help. * Update gitignore and project files. * Code review feedback and deprecation tests. * Troubleshoot CI * Code review feedback. * Code review feedback. * Add example. * Use distutils.version for performance. * Review feedback.
1 parent c5f9af5 commit 43e1875

18 files changed

+1702
-489
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,6 @@ $RECYCLE.BIN/
6969
.vscode/cSpell.json
7070
.project
7171
.pydevproject
72+
.pytest_cache/
73+
env3/
74+
env2/

VS2015.sln

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.26730.16
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{888888A0-9F3D-457C-B088-3A5042F75D52}") = "knack", "knack.pyproj", "{27802D2F-7F88-44E9-9818-C960569098A6}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{27802D2F-7F88-44E9-9818-C960569098A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{27802D2F-7F88-44E9-9818-C960569098A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
16+
EndGlobalSection
17+
GlobalSection(SolutionProperties) = preSolution
18+
HideSolutionNode = FALSE
19+
EndGlobalSection
20+
GlobalSection(ExtensibilityGlobals) = postSolution
21+
SolutionGuid = {87C87E54-C86A-44F2-96F7-D282A01692A9}
22+
EndGlobalSection
23+
EndGlobal

VS2017.sln

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.27004.2002
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{888888A0-9F3D-457C-B088-3A5042F75D52}") = "knack", "knack.pyproj", "{27802D2F-7F88-44E9-9818-C960569098A6}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{27802D2F-7F88-44E9-9818-C960569098A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{27802D2F-7F88-44E9-9818-C960569098A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
16+
EndGlobalSection
17+
GlobalSection(SolutionProperties) = preSolution
18+
HideSolutionNode = FALSE
19+
EndGlobalSection
20+
GlobalSection(ExtensibilityGlobals) = postSolution
21+
SolutionGuid = {87C87E54-C86A-44F2-96F7-D282A01692A9}
22+
EndGlobalSection
23+
EndGlobal

examples/exapp2

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env python
2+
3+
""" User registers commands with CommandGroups """
4+
5+
import os
6+
import sys
7+
from collections import OrderedDict
8+
9+
from knack import CLI
10+
from knack.commands import CLICommandsLoader, CommandGroup
11+
from knack.arguments import ArgumentsContext
12+
from knack.help import CLIHelp
13+
14+
from knack.help_files import helps
15+
16+
cli_name = os.path.basename(__file__)
17+
18+
helps['abc'] = """
19+
type: group
20+
short-summary: Manage the alphabet of words.
21+
"""
22+
23+
helps['abc list'] = """
24+
type: command
25+
short-summary: List the alphabet.
26+
examples:
27+
- name: It's pretty straightforward.
28+
text: {cli_name} abc list
29+
""".format(cli_name=cli_name)
30+
31+
def a_test_command_handler():
32+
return [{'a': 1, 'b': 1234}, {'a': 3, 'b': 4}]
33+
34+
35+
def abc_list_command_handler():
36+
import string
37+
return list(string.ascii_lowercase)
38+
39+
def hello_command_handler(myarg=None, abc=None):
40+
return ['hello', 'world', myarg, abc]
41+
42+
WELCOME_MESSAGE = r"""
43+
_____ _ _____
44+
/ ____| | |_ _|
45+
| | | | | |
46+
| | | | | |
47+
| |____| |____ _| |_
48+
\_____|______|_____|
49+
50+
51+
Welcome to the cool new CLI!
52+
"""
53+
54+
class MyCLIHelp(CLIHelp):
55+
56+
def __init__(self, cli_ctx=None):
57+
super(MyCLIHelp, self).__init__(cli_ctx=cli_ctx,
58+
privacy_statement='My privacy statement.',
59+
welcome_message=WELCOME_MESSAGE)
60+
61+
class MyCommandsLoader(CLICommandsLoader):
62+
63+
def load_command_table(self, args):
64+
with CommandGroup(self, 'hello', '__main__#{}') as g:
65+
g.command('world', 'hello_command_handler', confirmation=True)
66+
with CommandGroup(self, 'abc', '__main__#{}') as g:
67+
g.command('list', 'abc_list_command_handler')
68+
g.command('show', 'a_test_command_handler')
69+
g.command('get', 'a_test_command_handler', deprecate_info=g.deprecate(redirect='show', hide='0.1.0'))
70+
return super(MyCommandsLoader, self).load_command_table(args)
71+
72+
def load_arguments(self, command):
73+
with ArgumentsContext(self, 'hello world') as ac:
74+
ac.argument('myarg', type=int, default=100)
75+
super(MyCommandsLoader, self).load_arguments(command)
76+
77+
78+
class MyCLI(CLI):
79+
80+
def get_cli_version(self):
81+
return '0.1.0'
82+
83+
84+
mycli = MyCLI(cli_name=cli_name,
85+
config_dir=os.path.join('~', '.{}'.format(cli_name)),
86+
config_env_var_prefix=cli_name,
87+
commands_loader_cls=MyCommandsLoader,
88+
help_cls=MyCLIHelp)
89+
exit_code = mycli.invoke(sys.argv[1:])
90+
sys.exit(exit_code)

knack.pyproj

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<SchemaVersion>2.0</SchemaVersion>
6+
<ProjectGuid>{27802d2f-7f88-44e9-9818-c960569098a6}</ProjectGuid>
7+
<ProjectHome />
8+
<StartupFile>knack\__init__.py</StartupFile>
9+
<SearchPath />
10+
<WorkingDirectory>.</WorkingDirectory>
11+
<OutputPath>.</OutputPath>
12+
<ProjectTypeGuids>{888888a0-9f3d-457c-b088-3a5042f75d52}</ProjectTypeGuids>
13+
<LaunchProvider>Standard Python launcher</LaunchProvider>
14+
<InterpreterId>MSBuild|env2|$(MSBuildProjectFullPath)</InterpreterId>
15+
</PropertyGroup>
16+
<PropertyGroup Condition="'$(Configuration)' == 'Debug'" />
17+
<PropertyGroup Condition="'$(Configuration)' == 'Release'" />
18+
<PropertyGroup>
19+
<VisualStudioVersion Condition=" '$(VisualStudioVersion)' == '' ">10.0</VisualStudioVersion>
20+
</PropertyGroup>
21+
<ItemGroup>
22+
<Content Include="examples\exapp" />
23+
<Content Include="examples\exapp2" />
24+
<Content Include="examples\test_exapp" />
25+
<Content Include="requirements.txt" />
26+
<Content Include="tox.ini" />
27+
</ItemGroup>
28+
<ItemGroup>
29+
<Compile Include="knack\arguments.py" />
30+
<Compile Include="knack\cli.py" />
31+
<Compile Include="knack\commands.py" />
32+
<Compile Include="knack\completion.py" />
33+
<Compile Include="knack\config.py" />
34+
<Compile Include="knack\deprecation.py" />
35+
<Compile Include="knack\events.py" />
36+
<Compile Include="knack\help.py" />
37+
<Compile Include="knack\help_files.py" />
38+
<Compile Include="knack\introspection.py" />
39+
<Compile Include="knack\invocation.py" />
40+
<Compile Include="knack\log.py" />
41+
<Compile Include="knack\output.py" />
42+
<Compile Include="knack\parser.py" />
43+
<Compile Include="knack\prompting.py" />
44+
<Compile Include="knack\query.py" />
45+
<Compile Include="knack\testsdk\base.py" />
46+
<Compile Include="knack\testsdk\checkers.py" />
47+
<Compile Include="knack\testsdk\const.py" />
48+
<Compile Include="knack\testsdk\decorators.py" />
49+
<Compile Include="knack\testsdk\exceptions.py" />
50+
<Compile Include="knack\testsdk\patches.py" />
51+
<Compile Include="knack\testsdk\recording_processors.py" />
52+
<Compile Include="knack\testsdk\util.py" />
53+
<Compile Include="knack\testsdk\__init__.py" />
54+
<Compile Include="knack\util.py" />
55+
<Compile Include="knack\__init__.py" />
56+
<Compile Include="scripts\license_verify.py" />
57+
<Compile Include="setup.py" />
58+
<Compile Include="tests\test_cli_scenarios.py" />
59+
<Compile Include="tests\test_command_registration.py" />
60+
<Compile Include="tests\test_completion.py" />
61+
<Compile Include="tests\test_config.py" />
62+
<Compile Include="tests\test_deprecation.py">
63+
<SubType>Code</SubType>
64+
</Compile>
65+
<Compile Include="tests\test_help.py" />
66+
<Compile Include="tests\test_introspection.py" />
67+
<Compile Include="tests\test_log.py" />
68+
<Compile Include="tests\test_output.py" />
69+
<Compile Include="tests\test_parser.py" />
70+
<Compile Include="tests\test_prompting.py" />
71+
<Compile Include="tests\test_query.py" />
72+
<Compile Include="tests\test_util.py" />
73+
<Compile Include="tests\util.py" />
74+
<Compile Include="tests\__init__.py" />
75+
</ItemGroup>
76+
<ItemGroup>
77+
<Folder Include="examples\" />
78+
<Folder Include="knack" />
79+
<Folder Include="knack\testsdk" />
80+
<Folder Include="scripts" />
81+
<Folder Include="tests" />
82+
</ItemGroup>
83+
<ItemGroup>
84+
<Interpreter Include="env2\">
85+
<Id>env2</Id>
86+
<Version>2.7</Version>
87+
<Description>env2 (Python 2.7 (32-bit))</Description>
88+
<InterpreterPath>Scripts\python.exe</InterpreterPath>
89+
<WindowsInterpreterPath>Scripts\pythonw.exe</WindowsInterpreterPath>
90+
<PathEnvironmentVariable>PYTHONPATH</PathEnvironmentVariable>
91+
<Architecture>X86</Architecture>
92+
</Interpreter>
93+
<Interpreter Include="env2\">
94+
<Id>{52196499-2eb9-4ba7-924a-ca67b294886b}</Id>
95+
<Version>2.7</Version>
96+
<Description>env2 (Python 2.7)</Description>
97+
<InterpreterPath>Scripts\python.exe</InterpreterPath>
98+
<WindowsInterpreterPath>Scripts\pythonw.exe</WindowsInterpreterPath>
99+
<LibraryPath>Lib\</LibraryPath>
100+
<PathEnvironmentVariable>PYTHONPATH</PathEnvironmentVariable>
101+
<Architecture>X86</Architecture>
102+
</Interpreter>
103+
<Interpreter Include="env3\">
104+
<Id>env3</Id>
105+
<Version>3.6</Version>
106+
<Description>env3 (Python 3.6 (64-bit))</Description>
107+
<InterpreterPath>Scripts\python.exe</InterpreterPath>
108+
<WindowsInterpreterPath>Scripts\pythonw.exe</WindowsInterpreterPath>
109+
<PathEnvironmentVariable>PYTHONPATH</PathEnvironmentVariable>
110+
<Architecture>X64</Architecture>
111+
</Interpreter>
112+
</ItemGroup>
113+
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Python Tools\Microsoft.PythonTools.targets" />
114+
</Project>

knack/__init__.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
# Licensed under the MIT License. See License.txt in the project root for license information.
44
# --------------------------------------------------------------------------------------------
55

6-
from .cli import CLI
7-
from .commands import CLICommandsLoader, CLICommand
8-
from .arguments import ArgumentsContext
9-
from .help import CLIHelp
6+
import sys
7+
8+
from knack.cli import CLI
9+
from knack.commands import CLICommandsLoader, CLICommand
10+
from knack.arguments import ArgumentsContext
11+
from knack.help import CLIHelp
1012

1113
__all__ = ['CLI', 'CLICommandsLoader', 'CLICommand', 'CLIHelp', 'ArgumentsContext']

0 commit comments

Comments
 (0)