From 074a2d3c8b1c114a8a07bb12dcf1104e0c0d6c1e Mon Sep 17 00:00:00 2001 From: rsteube Date: Mon, 25 Sep 2023 23:46:35 +0200 Subject: [PATCH] env: added python --- completers/python_completer/cmd/root.go | 7 +- pkg/actions/env/python.go | 76 +++++++++++++++++++ .../actions/tools/python}/module.go | 6 +- pkg/actions/tools/python/warning.go | 41 ++++++++++ 4 files changed, 126 insertions(+), 4 deletions(-) create mode 100644 pkg/actions/env/python.go rename {completers/python_completer/cmd/action => pkg/actions/tools/python}/module.go (84%) create mode 100644 pkg/actions/tools/python/warning.go diff --git a/completers/python_completer/cmd/root.go b/completers/python_completer/cmd/root.go index 0dfaa24f56..6e156ce666 100644 --- a/completers/python_completer/cmd/root.go +++ b/completers/python_completer/cmd/root.go @@ -4,8 +4,8 @@ import ( "os" "github.com/rsteube/carapace" - "github.com/rsteube/carapace-bin/completers/python_completer/cmd/action" "github.com/rsteube/carapace-bin/completers/python_completer/cmd/module" + "github.com/rsteube/carapace-bin/pkg/actions/tools/python" "github.com/rsteube/carapace/pkg/style" "github.com/spf13/cobra" ) @@ -45,7 +45,7 @@ func init() { rootCmd.Flags().BoolSliceS("O", "O", []bool{}, "remove assert and __debug__-dependent statements") rootCmd.Flags().BoolS("S", "S", false, "don't imply 'import site' on initialization") rootCmd.Flags().BoolSliceS("V", "V", []bool{}, "print the Python version number and exit") - rootCmd.Flags().StringS("W", "W", "", "warning control") + rootCmd.Flags().StringSliceS("W", "W", []string{}, "warning control") rootCmd.Flags().StringArrayS("X", "X", []string{}, "set implementation-specific option") rootCmd.Flags().BoolS("b", "b", false, "issue warnings about str(bytes_instance), str(bytearray_instance) and comparing bytes/bytearray with str") rootCmd.Flags().StringS("c", "c", "", "program passed in as string") @@ -61,6 +61,7 @@ func init() { rootCmd.Flags().BoolS("x", "x", false, "skip first line of source") carapace.Gen(rootCmd).FlagCompletion(carapace.ActionMap{ + "W": python.ActionWarningControls(), "X": carapace.ActionMultiParts("=", func(c carapace.Context) carapace.Action { switch len(c.Parts) { case 0: @@ -86,7 +87,7 @@ func init() { } }), "check-hash-based-pycs": carapace.ActionValues("always", "default", "never").StyleF(style.ForKeyword), - "m": action.ActionModules(), + "m": python.ActionModules(), }) carapace.Gen(rootCmd).PositionalCompletion( diff --git a/pkg/actions/env/python.go b/pkg/actions/env/python.go new file mode 100644 index 0000000000..c1bf575ca1 --- /dev/null +++ b/pkg/actions/env/python.go @@ -0,0 +1,76 @@ +package env + +import ( + "os" + + "github.com/rsteube/carapace" + "github.com/rsteube/carapace-bin/pkg/actions/tools/python" + "github.com/rsteube/carapace-bin/pkg/conditions" +) + +func init() { + knownVariables["python"] = variables{ + Condition: conditions.ConditionPath("python"), + Variables: map[string]string{ + "PYTHONSAFEPATH": "prepend a potentially unsafe path to sys.path such as the current directory", + "PYTHONHOME": "Change the location of the standard Python libraries", + "PYTHONPATH": "Augments the default search path for module files", + "PYTHONPLATLIBDIR": "Override sys.platlibdir", + "PYTHONSTARTUP": "Python commands in this file are executed before the first prompt in interactive mode", + "PYTHONOPTIMIZE": "Remove assert statements and any code conditional on the value of __debug_", + "PYTHONDEBUG": "Turn on parser debugging output", + "PYTHONDONTWRITEBYTECODE": "Don't write .pyc files on import", + "PYTHONINSPECT": "enter interactive mode after executing the script or the command", + "PYTHONIOENCODING": "overrides the encoding used for stdin/stdout/stderr", + "PYTHONNOUSERSITE": "Don't add user site directory to sys.path", + "PYTHONUNBUFFERED": "Force the stdout and stderr streams to be unbuffered", + "PYTHONVERBOSE": "Print a message each time a module is initialized", + "PYTHONWARNINGS": "Warning control", + "PYTHONHASHSEED": "value used to seed the hashes of str and bytes objects", + "PYTHONINTMAXSTRDIGITS": "Limit the maximum digit characters in an int value when converting from a string", + "PYTHONMALLOC": "Set the Python memory allocators and/or install debug hooks", + "PYTHONMALLOCSTATS": "Python will print statistics of the pymalloc memory allocator", + "PYTHONASYNCIODEBUG": "enable the debug mode of the asyncio module", + "PYTHONTRACEMALLOC": "start tracing Python memory allocations using the tracemalloc module", + "PYTHONFAULTHANDLER": "call faulthandler.enable() at startup", + "PYTHONEXECUTABLE": "set sys.argv[0] to this value instead of the value got through the C runtime", + "PYTHONUSERBASE": "user base directory", + "PYTHONPROFILEIMPORTTIME": "show how long each import takes", + "PYTHONBREAKPOINT": "set debugger callable", + "PYTHONTHREADDEBUG": "print threading debug info", + "PYTHONDUMPREFS": "dump objects and reference counts still alive after shutting down the interpreter", + }, + VariableCompletion: map[string]carapace.Action{ + "PYTHONSAFEPATH": carapace.ActionDirectories(), + "PYTHONHOME": carapace.ActionDirectories(), + "PYTHONPATH": carapace.ActionDirectories().List(string(os.PathListSeparator)), + "PYTHONPLATLIBDIR": carapace.ActionDirectories(), + "PYTHONSTARTUP": carapace.ActionValues(), // TODO + "PYTHONOPTIMIZE": carapace.ActionValuesDescribed( + "1", "Remove assert statements and any code conditional on the value of __debug_", + "2", "Like '1' but also discard docstrings", + ), + "PYTHONDEBUG": carapace.ActionValues("1"), + "PYTHONDONTWRITEBYTECODE": carapace.ActionValues("1"), + "PYTHONINSPECT": carapace.ActionValues("1"), + "PYTHONIOENCODING": carapace.ActionValues(), // TODO + "PYTHONNOUSERSITE": carapace.ActionValues("1"), + "PYTHONUNBUFFERED": carapace.ActionValues("1"), + "PYTHONVERBOSE": carapace.ActionValues("1", "2"), + "PYTHONWARNINGS": python.ActionWarningControls().List(","), + "PYTHONHASHSEED": carapace.ActionValues(), + "PYTHONINTMAXSTRDIGITS": carapace.ActionValues(), + "PYTHONMALLOC": carapace.ActionValues("malloc", "pymalloc", "debug", "malloc_debug", "pymalloc_debug"), // TODO verify + "PYTHONMALLOCSTATS": carapace.ActionValues("1"), + "PYTHONASYNCIODEBUG": carapace.ActionValues("1"), + "PYTHONTRACEMALLOC": carapace.ActionValues("1", "5", "10", "25", "50", "100"), + "PYTHONFAULTHANDLER": carapace.ActionValues("1"), + "PYTHONEXECUTABLE": carapace.ActionValues(), + "PYTHONUSERBASE": carapace.ActionDirectories(), + "PYTHONPROFILEIMPORTTIME": carapace.ActionValues("1"), + "PYTHONBREAKPOINT": carapace.ActionValuesDescribed("0", "disable"), // TODO debuggers + "PYTHONTHREADDEBUG": carapace.ActionValues("1"), + "PYTHONDUMPREFS": carapace.ActionValues("1"), + }, + } +} diff --git a/completers/python_completer/cmd/action/module.go b/pkg/actions/tools/python/module.go similarity index 84% rename from completers/python_completer/cmd/action/module.go rename to pkg/actions/tools/python/module.go index 90afb01bba..bfd42e21a1 100644 --- a/completers/python_completer/cmd/action/module.go +++ b/pkg/actions/tools/python/module.go @@ -1,4 +1,4 @@ -package action +package python import ( "strings" @@ -7,6 +7,10 @@ import ( "github.com/rsteube/carapace" ) +// ActionModules completes modules +// +// Cython +// DistUtilsExtra func ActionModules() carapace.Action { return carapace.ActionExecCommand("python", "-c", "help('modules')")(func(output []byte) carapace.Action { lines := strings.Split(string(output), "\n") diff --git a/pkg/actions/tools/python/warning.go b/pkg/actions/tools/python/warning.go new file mode 100644 index 0000000000..10be852744 --- /dev/null +++ b/pkg/actions/tools/python/warning.go @@ -0,0 +1,41 @@ +package python + +import "github.com/rsteube/carapace" + +// ActionWarnings completes warnings +// +// default (Warn once per call location) +// error (Convert to exceptions) +func ActionWarnings() carapace.Action { + return carapace.ActionValuesDescribed( + "default", "Warn once per call location", + "error", "Convert to exceptions", + "always", "Warn every time", + "module", "Warn once per calling module", + "once", "Warn once per Python process", + "ignore", "Never warn", + ) +} + +// ActionWarningControls completes warning controls +// +// action:message:category:module:lineno +func ActionWarningControls() carapace.Action { + return carapace.ActionMultiParts(":", func(c carapace.Context) carapace.Action { + // TODO The full form of argument is: action:message:category:module:lineno + switch len(c.Parts) { + case 0: + return ActionWarnings() + case 1: + return carapace.ActionValues().Usage("message") + case 2: + return carapace.ActionValues().Usage("category") + case 3: + return carapace.ActionValues().Usage("module") + case 4: + return carapace.ActionValues().Usage("lineno") + default: + return carapace.ActionValues() + } + }) +}