-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add method for resetting globs for clean test runs. Should really be cleaned after each test, so this implementation is not pretty.
- Loading branch information
Showing
7 changed files
with
140 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# import pkgutil | ||
import inspect | ||
import os | ||
import sys | ||
import traceback | ||
|
||
__all__ = ['exec_code', 'run'] | ||
|
||
|
||
def exec_code(code, globals_dict): | ||
"""Execute the recipe code and handle error conditions. | ||
Args: | ||
code (str): The code to be executed. | ||
globals_dict (dict): globals_dict as used by exec(). | ||
Returns: | ||
Output of python exec() function. | ||
""" | ||
e = False | ||
try: | ||
ret = exec(code, globals_dict) | ||
except SyntaxError as err: | ||
error_class = err.__class__.__name__ | ||
# detail = err.args[0] | ||
lineno = err.lineno | ||
e = err | ||
except Exception as err: | ||
error_class = err.__class__.__name__ | ||
# detail = err.args[0] | ||
cl, exc, tb = sys.exc_info() | ||
lineno = traceback.extract_tb(tb)[-1][1] | ||
e = err | ||
if e: | ||
lines = code.splitlines() | ||
lenlines = len(lines) | ||
if lineno < lenlines: | ||
print("Recipe execution", | ||
error_class, | ||
"at recipe line:\n", | ||
lines[lineno-1]) | ||
else: | ||
print("Recipe execution", | ||
error_class) | ||
raise e | ||
return ret | ||
|
||
|
||
def run(root_mod, recipe, storage, replacements=[]): | ||
"""Obtain a dataframe. It will adjust to whatever | ||
storage the environment has set. Currently storage is supported in | ||
file, memory or dataiku (HDFS). | ||
Args: | ||
root_mod (module): The root module on which to base the path. | ||
recipe (str): Relative path to the recipe file from the module dir. | ||
storage (str): Storage type e.g. DATAIKU, FILE | ||
replacements (list): List of text replacements to enable recipe | ||
debugging. Example on how to limit data amount: | ||
[ | ||
{ | ||
"old": "dataframe.get(sql_context, ds_foo.name)", | ||
"new": "dataframe.get(sql_context, ds_foo.name).limit(10)" | ||
} | ||
] | ||
Returns: | ||
Output of python exec() function. | ||
""" | ||
if storage not in ["DATAIKU", "FILE"]: | ||
raise ValueError(f"Storage unknown {storage}") | ||
mod_path = os.path.dirname(inspect.getfile(root_mod)) | ||
recipe_path = f"{mod_path}/{recipe}" | ||
with open(recipe_path) as f: | ||
code = prepare_code(f.read(), recipe, storage, replacements) | ||
globals_dict = { | ||
'BIRGITTA_DATASET_STORAGE': storage | ||
} | ||
return exec_code(code, globals_dict) | ||
|
||
|
||
def prepare_code(code, recipe, storage, replacements): | ||
for replacement in replacements: | ||
code = code.replace(replacement["old"], replacement["new"]) | ||
glob_stmts = f"""import glob | ||
from birgitta import glob | ||
glob.set("BIRGITTA_DATASET_STORAGE", "{storage}")""" | ||
completed = f""" | ||
print("=== Recipe {recipe} complete ===")""" | ||
return glob_stmts + code + completed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import pytest | ||
# from birgitta import spark | ||
from birgitta import glob | ||
from birgitta.recipe import runner | ||
from examples.organizations.newsltd.projects import tribune | ||
|
||
|
||
def test_run(): | ||
glob.reset() | ||
with pytest.raises(TypeError): | ||
runner.run(tribune, "recipes/compute_filtered_contracts.py", "FILE") | ||
|
||
|
||
def test_syntax_error(): | ||
glob.reset() | ||
replacements = [ | ||
{ | ||
"old": "sql_context = ", | ||
"new": "sql_contesdaf@_0=~> = " | ||
} | ||
] | ||
with pytest.raises(SyntaxError): | ||
runner.run(tribune, | ||
"recipes/compute_filtered_contracts.py", | ||
"FILE", | ||
replacements) |