Skip to content

Commit

Permalink
Small code change to save memory and speed up a code path
Browse files Browse the repository at this point in the history
  • Loading branch information
linearcombination committed Dec 7, 2024
1 parent ac872f3 commit d30279e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
6 changes: 3 additions & 3 deletions backend/document/domain/document_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ def fetch_usfm_book_content_units(
for dto in found_usfm_resource_lookup_dtos
]
for resource_dir, dto in zip(resource_dirs, found_usfm_resource_lookup_dtos):
resource_lookup.provision_asset_files(dto, resource_dir)
resource_lookup.provision_asset_files(dto.url, resource_dir)
t1 = time.time()
logger.debug(
"Time to provision USFM asset files (acquire and write to disk) for TW resource: %s",
Expand Down Expand Up @@ -1003,7 +1003,7 @@ def generate_document(
for dto in found_resource_lookup_dtos
]
for resource_dir, dto in zip(resource_dirs, found_resource_lookup_dtos):
resource_lookup.provision_asset_files(dto, resource_dir)
resource_lookup.provision_asset_files(dto.url, resource_dir)
t1 = time.time()
logger.debug(
"Time to provision asset files (acquire and write to disk): %s", t1 - t0
Expand Down Expand Up @@ -1178,7 +1178,7 @@ def generate_docx_document(
for dto in found_resource_lookup_dtos
]
for resource_dir, dto in zip(resource_dirs, found_resource_lookup_dtos):
resource_lookup.provision_asset_files(dto, resource_dir)
resource_lookup.provision_asset_files(dto.url, resource_dir)
t1 = time.time()
logger.debug(
"Time to provision asset files (acquire and write to disk): %s", t1 - t0
Expand Down
10 changes: 5 additions & 5 deletions backend/document/domain/resource_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ def resource_types(


# Used by some tests
@lru_cache(maxsize=100)
def usfm_resource_types_and_book_tuples(
lang_code: str,
book_codes_str: str,
Expand Down Expand Up @@ -532,7 +533,7 @@ def usfm_resource_types_and_book_tuples(
)
logger.debug("dto: %s", dto)
resource_filepath = prepare_resource_filepath(dto)
provision_asset_files(dto, resource_filepath)
provision_asset_files(dto.url, resource_filepath)
content_file = parsing.usfm_asset_file(
dto,
resource_filepath,
Expand Down Expand Up @@ -935,20 +936,19 @@ def resource_lookup_dto(


def provision_asset_files(
resource_lookup_dto: ResourceLookupDto,
url: Optional[str],
resource_filepath: str,
) -> None:
if (
resource_lookup_dto.url is not None
url is not None
): # We know that resource_url is not None because of how we got here, but mypy isn't convinced. Let's convince mypy.
clone_git_repo(resource_lookup_dto.url, resource_filepath)
clone_git_repo(url, resource_filepath)


def prepare_resource_filepath(
resource_lookup_dto: ResourceLookupDto,
working_dir: str = settings.RESOURCE_ASSETS_DIR,
) -> str:

resource_filepath = ""
if (
resource_lookup_dto.url is not None
Expand Down
8 changes: 6 additions & 2 deletions backend/document/stet/stet.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,9 @@ def generate_docx_document(
lang0_resource_dir = prepare_resource_filepath(
lang0_resource_lookup_dto_
)
provision_asset_files(lang0_resource_lookup_dto_, lang0_resource_dir)
provision_asset_files(
lang0_resource_lookup_dto_.url, lang0_resource_dir
)
current_task.update_state(state="Parsing asset files")
source_usfm_book = usfm_book_content(
lang0_resource_lookup_dto_,
Expand All @@ -373,7 +375,9 @@ def generate_docx_document(
lang1_resource_dir = prepare_resource_filepath(
lang1_resource_lookup_dto_
)
provision_asset_files(lang1_resource_lookup_dto_, lang1_resource_dir)
provision_asset_files(
lang1_resource_lookup_dto_.url, lang1_resource_dir
)
target_usfm_book = usfm_book_content(
lang1_resource_lookup_dto_,
lang1_resource_dir,
Expand Down

0 comments on commit d30279e

Please sign in to comment.