Skip to content

Commit

Permalink
Merge pull request #1876 from rsteube/env-add-python
Browse files Browse the repository at this point in the history
env: added python
  • Loading branch information
rsteube authored Sep 27, 2023
2 parents 052252f + 074a2d3 commit 3a9b36e
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 4 deletions.
7 changes: 4 additions & 3 deletions completers/python_completer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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")
Expand All @@ -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:
Expand All @@ -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(
Expand Down
76 changes: 76 additions & 0 deletions pkg/actions/env/python.go
Original file line number Diff line number Diff line change
@@ -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"),
},
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package action
package python

import (
"strings"
Expand All @@ -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")
Expand Down
41 changes: 41 additions & 0 deletions pkg/actions/tools/python/warning.go
Original file line number Diff line number Diff line change
@@ -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()
}
})
}

0 comments on commit 3a9b36e

Please sign in to comment.