Skip to content

Commit

Permalink
Use different method for propagating ctx.obj
Browse files Browse the repository at this point in the history
  • Loading branch information
untitaker committed Aug 21, 2014
1 parent c5d4bef commit f9dd0d1
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions click/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,10 @@ def __init__(self, command, parent=None, info_name=None, obj=None,
self.params = {}
#: the leftover arguments.
self.args = []
#: subcontexts whose objects need to be updated.
self.subcontexts = []
if obj is None and parent is not None:
obj = parent.obj
parent.subcontexts.append(self)
#: the user object stored.
self.obj = obj
#: A dictionary (-like object) with defaults for parameters.
Expand Down Expand Up @@ -254,6 +256,18 @@ def __init__(self, command, parent=None, info_name=None, obj=None,
self._close_callbacks = []
self._depth = 0

_obj = None

def _get_obj(self):
return self._obj
def _set_obj(self, obj):
self._obj = obj
for ctx in self.subcontexts:
if ctx.obj is None:
ctx.obj = obj
obj = property(_get_obj, _set_obj)
del _get_obj, _set_obj

def __enter__(self):
self._depth += 1
return self
Expand Down Expand Up @@ -940,33 +954,33 @@ def _process_result(value):
# single command but we also inform the current context about the
# name of the command to invoke.
if not self.chain:
sub_ctx = self.handle_subcommand(ctx, args)
ctx.invoked_subcommands = [sub_ctx.info_name]

# Make sure the context is entered so we do not clean up
# resources until the result processor has worked.
with ctx:
Command.invoke(self, ctx)
sub_ctx = self.handle_subcommand(ctx, args)
ctx.invoked_subcommands = [sub_ctx.info_name]
with sub_ctx:
return _process_result(sub_ctx.command.invoke(sub_ctx))

# Otherwise we make every single context and invoke them in a
# chain. In that case the return value to the result processor
# is the list of all invoked subcommand's results.
contexts = []
while args:
sub_ctx = self.handle_subcommand(ctx, args, allow_extra_args=True,
allow_interspersed_args=False)
contexts.append(sub_ctx)
args = sub_ctx.args

# Now that we have all contexts, we can invoke them.
ctx.invoked_subcommands = [x.info_name for x in contexts]

# Make sure the context is entered so we do not clean up
# resources until the result processor has worked.
with ctx:
Command.invoke(self, ctx)

# Otherwise we make every single context and invoke them in a
# chain. In that case the return value to the result processor
# is the list of all invoked subcommand's results.
contexts = []
while args:
sub_ctx = self.handle_subcommand(ctx, args, allow_extra_args=True,
allow_interspersed_args=False)
contexts.append(sub_ctx)
args = sub_ctx.args

# Now that we have all contexts, we can invoke them.
ctx.invoked_subcommands = [x.info_name for x in contexts]

rv = []
for sub_ctx in contexts:
with sub_ctx:
Expand Down

0 comments on commit f9dd0d1

Please sign in to comment.