Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
dogversioning committed Jan 31, 2024
1 parent 0ebabdb commit 6507006
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
10 changes: 3 additions & 7 deletions cumulus_library/base_table_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,10 @@ def comment_queries(self, doc_str=None):
commented_queries.pop()
self.queries = commented_queries

def write_queries(
self, filename: str = "output.sql", dir_path: pathlib.Path = None
):
def write_queries(self, path: pathlib.Path = pathlib.Path.cwd() / "output.sql"):
path.parents[0].mkdir(parents=True, exist_ok=True)
"""writes all queries constructed by prepare_queries to disk"""
if dir_path:
dir_path.mkdir(exist_ok=True)
filename = dir_path / filename
with open(filename, "w", encoding="utf-8") as file:
with open(path, "w", encoding="utf-8") as file:
for query in self.queries:
file.write(query)
file.write("\n")
25 changes: 12 additions & 13 deletions cumulus_library/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,6 @@ def get_studies_by_manifest_path(path: pathlib.Path) -> dict:

def run_cli(args: Dict):
"""Controls which library tasks are run based on CLI arguments"""
print(args)
if args["action"] == "create":
create_template(args["create_dir"])

Expand All @@ -308,11 +307,11 @@ def run_cli(args: Dict):
else:
db_backend = databases.create_db_backend(args)
try:
builder = StudyRunner(db_backend, data_path=args.get("data_path"))
if "verbose" in args and args["verbose"]:
builder.verbose = True
runner = StudyRunner(db_backend, data_path=args.get("data_path"))
if args.get("verbose"):
runner.verbose = True
print("Testing connection to database...")
builder.cursor.execute("SHOW DATABASES")
runner.cursor.execute("SHOW DATABASES")

study_dict = get_study_dict(args["study_dir"])
if "prefix" not in args.keys():
Expand All @@ -326,42 +325,42 @@ def run_cli(args: Dict):
"you include `-s path/to/study/dir` as an arugment."
)
if args["action"] == "clean":
builder.clean_study(
runner.clean_study(
args["target"],
study_dict,
stats_clean=args["stats_clean"],
prefix=args["prefix"],
)
elif args["action"] == "build":
if "all" in args["target"]:
builder.clean_and_build_all(study_dict, args["stats_build"])
runner.clean_and_build_all(study_dict, args["stats_build"])
else:
for target in args["target"]:
if args["builder"]:
builder.run_single_table_builder(
runner.run_single_table_builder(
study_dict[target], args["builder"]
)
else:
builder.clean_and_build_study(
runner.clean_and_build_study(
study_dict[target],
stats_build=args["stats_build"],
continue_from=args["continue_from"],
)

elif args["action"] == "export":
if "all" in args["target"]:
builder.export_all(study_dict, args["data_path"])
runner.export_all(study_dict, args["data_path"])
else:
for target in args["target"]:
builder.export_study(study_dict[target], args["data_path"])
runner.export_study(study_dict[target], args["data_path"])

elif args["action"] == "generate-sql":
if "all" in args["target"]:
for target in study_dict.keys():
builder.generate_all_sql(study_dict[target])
runner.generate_all_sql(study_dict[target])
else:
for target in args["target"]:
builder.generate_study_sql(study_dict[target])
runner.generate_study_sql(study_dict[target])
finally:
db_backend.close()

Expand Down
17 changes: 11 additions & 6 deletions cumulus_library/study_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def _load_and_execute_builder(
verbose: bool = False,
drop_table: bool = False,
parser: databases.DatabaseParser = None,
generate_sql: bool = False,
write_reference_sql: bool = False,
doc_str: str = None,
) -> None:
"""Loads a table builder from a file.
Expand Down Expand Up @@ -371,12 +371,12 @@ def _load_and_execute_builder(
# execute, since the subclass would otherwise hang around.
table_builder_class = table_builder_subclasses[0]
table_builder = table_builder_class()
if generate_sql:
if write_reference_sql:
table_builder.prepare_queries(cursor, schema, parser=parser)
table_builder.comment_queries(doc_str=doc_str)
new_filename = pathlib.Path(f"{filename}").stem + ".sql"
table_builder.write_queries(
filename=f"{filename.split('.')[0]}.sql",
dir_path=pathlib.Path(f"{self._study_path}/reference_sql"),
path=pathlib.Path(f"{self._study_path}/reference_sql/" + new_filename)
)
else:
table_builder.execute_queries(
Expand Down Expand Up @@ -525,7 +525,7 @@ def run_generate_sql(
parser: databases.DatabaseParser = None,
**kwargs,
) -> None:
"""Loads modules from a manifest and executes code via BaseTableBuilder
"""Generates reference SQL from all BaseTableBuilder-derived classes in the manifest
:param cursor: A DatabaseCursor object
:param schema: The name of the schema to write tables to
Expand All @@ -544,7 +544,12 @@ def run_generate_sql(
)
for file in all_generators:
self._load_and_execute_builder(
file, cursor, schema, parser=parser, generate_sql=True, doc_str=doc_str
file,
cursor,
schema,
parser=parser,
write_reference_sql=True,
doc_str=doc_str,
)

def build_study(
Expand Down

0 comments on commit 6507006

Please sign in to comment.