Skip to content

Commit

Permalink
render args
Browse files Browse the repository at this point in the history
  • Loading branch information
neelasha23 committed Jan 23, 2024
1 parent 9c208d3 commit eac9e78
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/sql/magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,13 +403,15 @@ def interactive_execute_wrapper(**kwargs):
user_ns = self.shell.user_ns.copy()
user_ns.update(local_ns)

line = util.expand_args(line, user_ns)
# line = util.expand_args(line, user_ns)

command = SQLCommand(self, user_ns, line, cell)
# args.line: contains the line after the magic with all options removed

args = command.args

util.expand_args(args, user_ns)

if args.section and args.alias:
raise exceptions.UsageError(
"Cannot use --section with --alias since the section name "
Expand Down
32 changes: 27 additions & 5 deletions src/sql/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,19 +578,41 @@ def enclose_table_with_double_quotations(table, conn):
return _table


def expand_args(line, user_ns):
def expand_args(args, user_ns):
"""
Function to substitute command line arguments
with variables defined by user in the IPython
kernel.
Parameters
----------
line : str,
input text after the magic
args : argparse.Namespace,
object to hold the command line arguments.
user_ns : dict,
User namespace of IPython kernel
"""
return Template(line).render(user_ns)
arguments_to_expand : list
List of arguments to expand
"""

for attribute in vars(args):
value = getattr(args, attribute)
if value:
if isinstance(value, list):
for item in value:
if (
isinstance(item, str)
and item.startswith("{{")
and item.endswith("}}")
and item != "with_"
):
setattr(args, attribute, Template(item).render(user_ns))
else:
if (
isinstance(value, str)
and value.startswith("{{")
and value.endswith("}}")
and value != "with_"
):
setattr(args, attribute, Template(value).render(user_ns))

0 comments on commit eac9e78

Please sign in to comment.