Skip to content

Commit

Permalink
refactoring changes
Browse files Browse the repository at this point in the history
  • Loading branch information
AnirudhVIyer committed Jun 23, 2023
1 parent 8e5a27f commit cd84baa
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 118 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

## 0.7.10dev

* [Fix] Refactored `magic_cmd.py` by assigning each command logic to a separate file [#518]
* [Feature] Support flexible spacing `myvar=<<` operator ([#525](https://github.com/ploomber/jupysql/issues/525))
* [Doc] Modified integrations content to ensure they're all consistent (#523)
* [Doc] Document --persist-replace in API section (#539)
Expand Down
16 changes: 14 additions & 2 deletions src/sql/cmd/columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@
from sql.cmd.cmd_utils import CmdParser


def execute_columns_command(others):
def columns(others):
"""
Execution logic for the columns command
Implementation of `%sqlcmd columns`
This function takes in a string containing command line arguments,
parses them to extract the name of the table and the schema, and returns
a list of columns for the specified table.
Parameters
----------
others : str,
A string containing the command line arguments.
Returns
-------
columns: list
information of the columns in the specified table
"""
parser = CmdParser()

Expand Down
12 changes: 10 additions & 2 deletions src/sql/cmd/explore.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@
from sql.cmd.cmd_utils import CmdParser


def execute_expolore_command(others):
def explore(others):
"""
Execution logic for the explore command
Implementation of `%sqlcmd explore`
This function takes in a string containing command line arguments,
parses them to extract the name of the table, and displays an interactive
widget for exploring the contents of the specified table.
Parameters
----------
others : str,
A string containing the command line arguments.
"""
parser = CmdParser()
Expand Down
20 changes: 17 additions & 3 deletions src/sql/cmd/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,24 @@
from sql.cmd.cmd_utils import CmdParser


def execute_profile_command(others):
def profile(others):
"""
Execution logic for the profile command
Implementation of `%sqlcmd profile`
This function takes in a string containing command line arguments,
parses them to extract the name of the table, the schema, and the output location.
It then retrieves statistical information about the specified table and either
returns the report or writes it to the specified location.
Parameters
----------
others : str,
A string containing the command line arguments.
Returns
-------
report: PrettyTable
statistics of the table
"""
parser = CmdParser()
parser.add_argument("-t", "--table", type=str, help="Table name", required=True)
Expand Down
93 changes: 89 additions & 4 deletions src/sql/cmd/snippets.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,94 @@
from sql.sqlcmd import sqlcmd_snippets # noqa
from sql.cmd.cmd_utils import CmdParser
from sql import util
from sql.exceptions import UsageError


def execute_snippets_command(others):
def _modify_display_msg(key, remaining_keys, dependent_keys=None):
"""
Execution logic for the snippets command
Parameters
----------
key : str,
deleted stored snippet
remaining_keys: list
snippets remaining after key is deleted
dependent_keys: list
snippets dependent on key
Returns
-------
msg: str
Formatted message
"""
msg = f"{key} has been deleted.\n"
if dependent_keys:
msg = f"{msg}{', '.join(dependent_keys)} depend on {key}\n"
if remaining_keys:
msg = f"{msg}Stored snippets : {', '.join(remaining_keys)}"
else:
msg = f"{msg}There are no stored snippets"
return msg


def snippets(others):
"""
Implementation of `%sqlcmd snippets`
This function handles all the arguments related to %sqlcmd snippets, namely
listing stored snippets, and delete/ force delete/ force delete a snippet and
all its dependent snippets.
Parameters
----------
others : str,
A string containing the command line arguments.
"""
return sqlcmd_snippets(others)
parser = CmdParser()
parser.add_argument(
"-d", "--delete", type=str, help="Delete stored snippet", required=False
)
parser.add_argument(
"-D",
"--delete-force",
type=str,
help="Force delete stored snippet",
required=False,
)
parser.add_argument(
"-A",
"--delete-force-all",
type=str,
help="Force delete all stored snippets",
required=False,
)
args = parser.parse_args(others)
SNIPPET_ARGS = [args.delete, args.delete_force, args.delete_force_all]
if SNIPPET_ARGS.count(None) == len(SNIPPET_ARGS):
return ", ".join(util.get_all_keys())
if args.delete:
deps = util.get_key_dependents(args.delete)
if deps:
deps = ", ".join(deps)
raise UsageError(
f"The following tables are dependent on {args.delete}: {deps}.\n"
f"Pass --delete-force to only delete {args.delete}.\n"
f"Pass --delete-force-all to delete {deps} and {args.delete}"
)
else:
key = args.delete
remaining_keys = util.del_saved_key(key)
return _modify_display_msg(key, remaining_keys)

elif args.delete_force:
key = args.delete_force
deps = util.get_key_dependents(key)
remaining_keys = util.del_saved_key(key)
return _modify_display_msg(key, remaining_keys, deps)

elif args.delete_force_all:
deps = util.get_key_dependents(args.delete_force_all)
deps.append(args.delete_force_all)
for key in deps:
remaining_keys = util.del_saved_key(key)
return _modify_display_msg(", ".join(deps), remaining_keys)
18 changes: 16 additions & 2 deletions src/sql/cmd/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,23 @@
from sql.cmd.cmd_utils import CmdParser


def execute_tables_command(others):
def tables(others):
"""
Execution logic for the tables command
Implementation of `%sqlcmd tables`
This function takes in a string containing command line arguments,
parses them to extract the schema name, and returns a list of table names
present in the specified schema or in the default schema if none is specified.
Parameters
----------
others : str,
A string containing the command line arguments.
Returns
-------
table_names: list
list of tables in the schema
"""
parser = CmdParser()
Expand Down
22 changes: 20 additions & 2 deletions src/sql/cmd/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,27 @@ def run_each_individually(args, conn):
return storage


def execute_test_command(others):
def test(others):
"""
Execution logic for the test command
Implementation of `%sqlcmd test`
This function takes in a string containing command line arguments,
parses them to extract the table name, column name, and conditions
to return if those conditions are satisfied in that table
Parameters
----------
others : str,
A string containing the command line arguments.
Returns
-------
result: bool
Result of the test
table: PrettyTable
table with rows because of which the test fails
"""
parser = CmdParser()
Expand Down
24 changes: 12 additions & 12 deletions src/sql/magic_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
from IPython.core.magic import Magics, line_magic, magics_class
from IPython.core.magic_arguments import argument, magic_arguments
from sql import util
from sql.cmd.tables import execute_tables_command
from sql.cmd.columns import execute_columns_command
from sql.cmd.test import execute_test_command
from sql.cmd.profile import execute_profile_command
from sql.cmd.explore import execute_expolore_command
from sql.cmd.snippets import execute_snippets_command
from sql.cmd.tables import tables
from sql.cmd.columns import columns
from sql.cmd.test import test
from sql.cmd.profile import profile
from sql.cmd.explore import explore
from sql.cmd.snippets import snippets

try:
from traitlets.config.configurable import Configurable
Expand Down Expand Up @@ -82,12 +82,12 @@ def execute(self, cmd_name="", others="", cell="", local_ns=None):
"""

router = {
"tables": execute_tables_command,
"columns": execute_columns_command,
"test": execute_test_command,
"profile": execute_profile_command,
"explore": execute_expolore_command,
"snippets": execute_snippets_command,
"tables": tables,
"columns": columns,
"test": test,
"profile": profile,
"explore": explore,
"snippets": snippets,
}

cmd = router.get(cmd_name)
Expand Down
90 changes: 0 additions & 90 deletions src/sql/sqlcmd.py

This file was deleted.

0 comments on commit cd84baa

Please sign in to comment.