Skip to content

Commit

Permalink
Allow specifying exclude-edges in CLI.
Browse files Browse the repository at this point in the history
This involves adding the current directory to the import path, which
might possibly cause problems down the line...
  • Loading branch information
MrCurtis committed Feb 1, 2024
1 parent 6fc78da commit 09e6ba0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project]
name = "fk-graph"
packages = ["src/fk_graph"]
version = "0.0.14"
version = "0.0.15"
authors = [
{ name="Andrew Curtis", email="[email protected]" },
{ name="John C Thomas" },
Expand Down
21 changes: 20 additions & 1 deletion src/fk_graph/cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from argparse import ArgumentParser, ArgumentTypeError
from importlib import import_module
from json import JSONDecodeError, loads
from os import getcwd
from sys import path

from sqlalchemy import create_engine

Expand All @@ -17,7 +20,9 @@ def main():
engine,
args.table,
args.primary_key,
only_tables=args.only_tables)
only_tables=args.only_tables,
exclude_edge=args.exclude_edge,
)
run_app(graph)

def _parse_args():
Expand All @@ -39,6 +44,14 @@ def _parse_args():
type=_to_list,
help="A list-like JSON string of tables to include.",
)
parser.add_argument(
"--exclude-edge",
type=_to_function,
help=(
"Function to exclude edges. This should be in the form"
" 'path.to.module.function'"
),
)
args = parser.parse_args()
if (
(not args.demo and args.connection_string is None)
Expand All @@ -61,3 +74,9 @@ def _to_list(json_input):
if not isinstance(parsed, list):
raise argument_type_error
return parsed

def _to_function(function_name):
path.append(getcwd())
module, function = ".".join(function_name.split(".")[:-1]), function_name.split(".")[-1]
return getattr(import_module(module), function)

3 changes: 3 additions & 0 deletions tests/fixtures/test_excluders.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from fk_graph.edge_excluders import input_row_is_in_tables

input_row_is_in_table_a = input_row_is_in_tables(["table_a"])
22 changes: 22 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from pathlib import Path
from shlex import split as shlex_split
from subprocess import PIPE, Popen, run
from unittest import TestCase
Expand Down Expand Up @@ -119,3 +120,24 @@ def test_errors_if_only_tables_is_not_listlike_json(self):
"The --only-tables argument should be a list-like JSON string.",
completed_process.stderr
)

def test_can_pass_in_function_to_exclude_edges(self):
path = Path(".") / "tests" / "fixtures"
command = (
"fk-graph"
" --demo"
" --table=table_a"
" --primary-key=1"
" --exclude-edge='test_excluders.input_row_is_in_table_a'"
)
# PIPE just stops stdout from printing to screen.
process = Popen(
shlex_split(command),
stdout=PIPE,
cwd=path
)
self.addCleanup(process.terminate)
response = self.session.get("http://localhost:8050")
# Because of the javascripty-nature of the app, we can
# only really inspect the status code.
self.assertEqual(response.status_code, 200)

0 comments on commit 09e6ba0

Please sign in to comment.