Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: assumption that py process runs in project dir #514

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@


class FileGenerator:
def __init__(self, fast_mode: bool, input_handler: InputHandler):
def __init__(self, project_path: str, fast_mode: bool, input_handler: InputHandler):
self.fast_mode = fast_mode
self.project_path = project_path
self.yaml = YAML()
self.yaml.preserve_quotes = True
self.yaml.indent(mapping=2, sequence=4, offset=2)
Expand Down Expand Up @@ -90,7 +91,7 @@ def create_pb_project(self, entity_name, id_types, connection_name, id_graph_mod
"id_types": [{"name": id_type} for id_type in id_types],
}

with open(CONFIG_FILE_PATH, "w") as f:
with open(os.path.join(self.project_path, CONFIG_FILE_PATH), "w") as f:
self.yaml.dump(pb_project, f)

def create_inputs_yaml(self, id_mappings):
Expand All @@ -109,7 +110,7 @@ def create_inputs_yaml(self, id_mappings):
input_entry["app_defaults"]["ids"].append(id_info)
inputs["inputs"].append(input_entry)

with open(INPUTS_FILE_PATH, "w") as f:
with open(os.path.join(self.project_path, INPUTS_FILE_PATH), "w") as f:
self.yaml.dump(inputs, f)

def create_profiles_yaml(self, entity_name, tables, model_name):
Expand All @@ -132,13 +133,13 @@ def create_profiles_yaml(self, entity_name, tables, model_name):
]
}

with open(PROFILES_FILE_PATH, "w") as f:
with open(os.path.join(self.project_path, PROFILES_FILE_PATH), "w") as f:
self.yaml.dump(profiles, f)

def validate_shopify_store_id_is_removed(self):
with open(CONFIG_FILE_PATH, "r") as file:
with open(os.path.join(self.project_path, CONFIG_FILE_PATH), "r") as file:
pb_project = yaml.safe_load(file)
with open(INPUTS_FILE_PATH, "r") as file:
with open(os.path.join(self.project_path, INPUTS_FILE_PATH), "r") as file:
inputs = yaml.safe_load(file)
if "shopify_store_id" in pb_project["entities"][0][
"id_types"
Expand All @@ -156,10 +157,10 @@ def validate_shopify_store_id_is_removed(self):

def update_bad_anons_filter(self):
regex_pattern = "(c8bc33a0-7cb7-47f9-b24f-73e077346142|f0ed91a9-e1a9-46a5-9257-d590f45612fe|cbe0ea73-4878-4892-ac82-b9ad42797000|f4690568-e9e7-4182-abc6-6ea2791daba3|b369d6f5-c17a-457c-ab86-5649c1b53883)"
with open(CONFIG_FILE_PATH, "r") as file:
with open(os.path.join(self.project_path, CONFIG_FILE_PATH), "r") as file:
pb_project = yaml.safe_load(file)
for id_type in pb_project["id_types"]:
if id_type["name"] == "anon_id":
id_type["filters"] = [{"type": "exclude", "regex": regex_pattern}]
with open(CONFIG_FILE_PATH, "w") as file:
with open(os.path.join(self.project_path, CONFIG_FILE_PATH), "w") as file:
yaml.dump(pb_project, file)
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def validate(self) -> tuple[bool, str]:
class TutorialRecipe(PyNativeRecipe):
def __init__(self, fast_mode: bool) -> None:
super().__init__()
self.profile_builder = ProfileBuilder(self.reader, fast_mode)
self.fast_mode = fast_mode
self.logger = Logger("TutorialRecipe")

def describe(self, this: WhtMaterial) -> Tuple[str, str]:
Expand All @@ -69,18 +69,23 @@ def register_dependencies(self, this: WhtMaterial):
pass

def execute(self, this: WhtMaterial):
project_path = this.base_wht_project.project_path()
if not os.path.exists("sample_data"):
self.logger.info("unzipping sample data...")
unzip_sample_data()
self.profile_builder.run()
unzip_sample_data(project_path, self.logger)
profile_builder = ProfileBuilder(project_path, self.reader, self.fast_mode)
profile_builder.run()


class ProfileBuilder:
def __init__(self, reader, fast_mode: bool):
def __init__(self, project_path: str, reader, fast_mode: bool):
self.project_path = project_path
self.config = {}
self.db_manager = None
self.input_handler = InputHandler(reader, fast_mode)
self.file_generator = FileGenerator(fast_mode, self.input_handler)
self.file_generator = FileGenerator(
self.project_path, fast_mode, self.input_handler
)
self.fast_mode = fast_mode

def run(self):
Expand Down Expand Up @@ -990,18 +995,19 @@ def get_sample_data_path():
)


def unzip_sample_data():
def unzip_sample_data(project_path: str, logger: Logger):
zip_file_path = get_sample_data_path()
# Ensure the zip file exists
if not os.path.exists(zip_file_path):
raise Exception(f"Error: {zip_file_path} not found.")

# Unzip the file
try:
with zipfile.ZipFile(zip_file_path, "r") as zip_ref:
for file_info in zip_ref.infolist():
if not file_info.filename.startswith("__MACOSX"):
zip_ref.extract(file_info, ".")
print(f"Successfully extracted {zip_file_path} to the current directory")
logger.info(f"Successfully extracted {zip_file_path} to {project_path}")
except Exception as e:
raise Exception(f"An error occurred while extracting: {str(e)}")

Expand Down
3 changes: 3 additions & 0 deletions src/predictions/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,7 @@
],
install_requires=install_requires,
include_package_data=True,
package_data={
shubhammehra4 marked this conversation as resolved.
Show resolved Hide resolved
"profiles_mlcorelib": ["py_native/profiles_tutorial/sample_data.zip"]
},
)
Loading