Skip to content

refactor: parameterize debug #920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
18 changes: 9 additions & 9 deletions invoke/completion/complete.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,35 @@ def complete(
# Strip out program name (scripts give us full command line)
# TODO: this may not handle path/to/script though?
invocation = re.sub(r"^({}) ".format("|".join(names)), "", core.remainder)
debug("Completing for invocation: {!r}".format(invocation))
debug("Completing for invocation: %r", invocation)
# Tokenize (shlex will have to do)
tokens = shlex.split(invocation)
# Handle flags (partial or otherwise)
if tokens and tokens[-1].startswith("-"):
tail = tokens[-1]
debug("Invocation's tail {!r} is flag-like".format(tail))
debug("Invocation's tail %r is flag-like", tail)
# Gently parse invocation to obtain 'current' context.
# Use last seen context in case of failure (required for
# otherwise-invalid partial invocations being completed).

contexts: List[ParserContext]
try:
debug("Seeking context name in tokens: {!r}".format(tokens))
debug("Seeking context name in tokens: %r", tokens)
contexts = parser.parse_argv(tokens)
except ParseError as e:
msg = "Got parser error ({!r}), grabbing its last-seen context {!r}" # noqa
debug(msg.format(e, e.context))
msg = "Got parser error (%r), grabbing its last-seen context %r" # noqa
debug(msg, e, e.context)
contexts = [e.context] if e.context is not None else []
# Fall back to core context if no context seen.
debug("Parsed invocation, contexts: {!r}".format(contexts))
debug("Parsed invocation, contexts: %r", contexts)
if not contexts or not contexts[-1]:
context = initial_context
else:
context = contexts[-1]
debug("Selected context: {!r}".format(context))
debug("Selected context: %r", context)
# Unknown flags (could be e.g. only partially typed out; could be
# wholly invalid; doesn't matter) complete with flags.
debug("Looking for {!r} in {!r}".format(tail, context.flags))
debug("Looking for %r in %r", tail, context.flags)
if tail not in context.flags:
debug("Not found, completing with flag names")
# Long flags - partial or just the dashes - complete w/ long flags
Expand Down Expand Up @@ -119,7 +119,7 @@ def print_completion_script(shell: str, names: List[str]) -> None:
except KeyError:
err = 'Completion for shell "{}" not supported (options are: {}).'
raise ParseError(err.format(shell, ", ".join(sorted(completions))))
debug("Printing completion script from {}".format(path))
debug("Printing completion script from %s", path)
# Choose one arbitrary program name for script's own internal invocation
# (also used to construct completion function names when necessary)
binary = names[0]
Expand Down
21 changes: 10 additions & 11 deletions invoke/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -893,8 +893,7 @@ def _load_file(
# Typically means 'no such file', so just note & skip past.
except IOError as e:
if e.errno == 2:
err = "Didn't see any {}, skipping."
debug(err.format(filepath))
debug("Didn't see any %s, skipping.", filepath)
else:
raise
# Still None -> no suffixed paths were found, record this fact
Expand Down Expand Up @@ -941,21 +940,21 @@ def merge(self) -> None:
"""
debug("Merging config sources in order onto new empty _config...")
self._set(_config={})
debug("Defaults: {!r}".format(self._defaults))
debug("Defaults: %r", self._defaults)
merge_dicts(self._config, self._defaults)
debug("Collection-driven: {!r}".format(self._collection))
debug("Collection-driven: %r", self._collection)
merge_dicts(self._config, self._collection)
self._merge_file("system", "System-wide")
self._merge_file("user", "Per-user")
self._merge_file("project", "Per-project")
debug("Environment variable config: {!r}".format(self._env))
debug("Environment variable config: %r", self._env)
merge_dicts(self._config, self._env)
self._merge_file("runtime", "Runtime")
debug("Overrides: {!r}".format(self._overrides))
debug("Overrides: %r", self._overrides)
merge_dicts(self._config, self._overrides)
debug("Modifications: {!r}".format(self._modifications))
debug("Modifications: %r", self._modifications)
merge_dicts(self._config, self._modifications)
debug("Deletions: {!r}".format(self._deletions))
debug("Deletions: %r", self._deletions)
obliterate(self._config, self._deletions)

def _merge_file(self, name: str, desc: str) -> None:
Expand All @@ -966,16 +965,16 @@ def _merge_file(self, name: str, desc: str) -> None:
data = getattr(self, "_{}".format(name))
# None -> no loading occurred yet
if found is None:
debug("{} has not been loaded yet, skipping".format(desc))
debug("%s has not been loaded yet, skipping", desc)
# True -> hooray
elif found:
debug("{} ({}): {!r}".format(desc, path, data))
debug("%s (%s): %r", desc, path, data)
merge_dicts(self._config, data)
# False -> did try, did not succeed
else:
# TODO: how to preserve what was tried for each case but only for
# the negative? Just a branch here based on 'name'?
debug("{} not found, skipping".format(desc))
debug("%s not found, skipping", desc)

def clone(self, into: Optional[Type["Config"]] = None) -> "Config":
"""
Expand Down
6 changes: 3 additions & 3 deletions invoke/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ def load(self) -> Dict[str, Any]:
"""
# Obtain allowed env var -> existing value map
env_vars = self._crawl(key_path=[], env_vars={})
m = "Scanning for env vars according to prefix: {!r}, mapping: {!r}"
debug(m.format(self._prefix, env_vars))
m = "Scanning for env vars according to prefix: %r, mapping: %r"
debug(m, self._prefix, env_vars)
# Check for actual env var (honoring prefix) and try to set
for env_var, key_path in env_vars.items():
real_var = (self._prefix or "") + env_var
if real_var in os.environ:
self._path_set(key_path, os.environ[real_var])
debug("Obtained env var config: {!r}".format(self.data))
debug("Obtained env var config: %r", self.data)
return self.data

def _crawl(
Expand Down
12 changes: 6 additions & 6 deletions invoke/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ def execute(
.. versionadded:: 1.0
"""
# Normalize input
debug("Examining top level tasks {!r}".format([x for x in tasks]))
debug("Examining top level tasks %r", [x for x in tasks])
calls = self.normalize(tasks)
debug("Tasks (now Calls) with kwargs: {!r}".format(calls))
debug("Tasks (now Calls) with kwargs: %r", calls)
# Obtain copy of directly-given tasks since they should sometimes
# behave differently
direct = list(calls)
Expand All @@ -120,7 +120,7 @@ def execute(
# moment...
for call in calls:
autoprint = call in direct and call.autoprint
debug("Executing {!r}".format(call))
debug("Executing %r", call)
# Hand in reference to our config, which will preserve user
# modifications across the lifetime of the session.
config = self.config
Expand Down Expand Up @@ -189,10 +189,10 @@ def dedupe(self, calls: List["Call"]) -> List["Call"]:
debug("Deduplicating tasks...")
for call in calls:
if call not in deduped:
debug("{!r}: no duplicates found, ok".format(call))
debug("%r: no duplicates found, ok", call)
deduped.append(call)
else:
debug("{!r}: found in list already, skipping".format(call))
debug("%r: found in list already, skipping", call)
return deduped

def expand_calls(self, calls: List["Call"]) -> List["Call"]:
Expand All @@ -214,7 +214,7 @@ def expand_calls(self, calls: List["Call"]) -> List["Call"]:
# task lists, which may contain 'raw' Task objects)
if isinstance(call, Task):
call = Call(call)
debug("Expanding task-call {!r}".format(call))
debug("Expanding task-call %r", call)
# TODO: this is where we _used_ to call Executor.config_for(call,
# config)...
# TODO: now we may need to preserve more info like where the call
Expand Down
8 changes: 4 additions & 4 deletions invoke/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def start(self) -> str:
def find(self, name: str) -> Tuple[IO, str, Tuple[str, str, int]]:
# Accumulate all parent directories
start = self.start
debug("FilesystemLoader find starting at {!r}".format(start))
debug("FilesystemLoader find starting at %r", start)
parents = [os.path.abspath(start)]
parents.append(os.path.dirname(parents[-1]))
while parents[-1] != parents[-2]:
Expand All @@ -129,9 +129,9 @@ def find(self, name: str) -> Tuple[IO, str, Tuple[str, str, int]]:
# we turn it into a more obvious exception class.
try:
tup = imp.find_module(name, parents)
debug("Found module: {!r}".format(tup[1]))
debug("Found module: %r", tup[1])
return tup
except ImportError:
msg = "ImportError loading {!r}, raising CollectionNotFound"
debug(msg.format(name))
msg = "ImportError loading %r, raising CollectionNotFound"
debug(msg, name)
raise CollectionNotFound(name=name, start=start)
Loading