-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* first step for cached generated assets * set up individual asset generating functions, except latex_image * finish individual asset generation functions which manage generated cache * implement generate assets with cache * format and add to changelog * add options for generate and build commands * add asset_type to hashes to avoid collision * fix tests and subset build hash table
- Loading branch information
1 parent
b5257f8
commit 7db949b
Showing
13 changed files
with
428 additions
and
320 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,142 @@ | ||
import typing as t | ||
import logging | ||
import hashlib | ||
from pathlib import Path | ||
import shutil | ||
from .. import core | ||
|
||
|
||
log = logging.getLogger("ptxlogger") | ||
|
||
|
||
# The individual asset type generation functions | ||
|
||
|
||
def individual_asymptote( | ||
asydiagram: str, | ||
outformat: str, | ||
method: str, | ||
asy_cli: t.List[str], | ||
asyversion: str, | ||
alberta: str, | ||
dest_dir: Path, | ||
cache_dir: Path, | ||
skip_cache: bool = False, | ||
) -> None: | ||
""" | ||
Checks whether a cached version of the diagram in the correct outformat exists. If it does, copies it to the dest_dir and returns. If it does not, calls the core.individual_asymptote_conversion function to generate the diagram in the correct outformat and then copies it to the dest_dir. In the latter case, also makes a copy to the cached version in the cache_dir. | ||
- outformat will be a file extension. | ||
""" | ||
log.debug("Using the CLI's individual_asymptote function") | ||
asset_file = Path(asydiagram).resolve() | ||
cache_file = cache_asset_filename(asset_file, outformat, "asymptote", cache_dir) | ||
output_file = dest_dir / asset_file.with_suffix(f".{outformat}").name | ||
if cache_file.exists() and not skip_cache: | ||
log.debug(f"Copying cached asymptote diagram {cache_file} to {output_file}") | ||
shutil.copy2(cache_file, output_file) | ||
else: | ||
core.individual_asymptote_conversion( | ||
asydiagram, outformat, method, asy_cli, asyversion, alberta, dest_dir | ||
) | ||
if output_file.exists(): | ||
log.debug( | ||
f"Created asymptote diagram {output_file}; saving a copy to cache as {cache_file}" | ||
) | ||
shutil.copy2(output_file, cache_file) | ||
log.debug("Finished individual_asymptote function") | ||
|
||
|
||
def individual_sage( | ||
sageplot: str, | ||
outformat: str, | ||
dest_dir: Path, | ||
sage_executable_cmd: t.List[str], | ||
cache_dir: Path, | ||
skip_cache: bool = False, | ||
) -> None: | ||
""" | ||
Checks whether a cached version of the diagram in the correct outformat exists. If it does, copies it to the dest_dir and returns. If it does not, calls the core.individual_asymptote_conversion function to generate the diagram in the correct outformat and then copies it to the dest_dir. In the latter case, also makes a copy to the cached version in the cache_dir. | ||
- outformat will be a file extension. | ||
""" | ||
|
||
log.debug("Using the CLI's individual_sage function") | ||
asset_file = Path(sageplot).resolve() | ||
cache_file = cache_asset_filename( | ||
asset_file, | ||
outformat, | ||
"sageplot", | ||
cache_dir, | ||
) | ||
output_file = dest_dir / asset_file.with_suffix(f".{outformat}").name | ||
if cache_file.exists() and not skip_cache: | ||
log.debug(f"Copying cached sageplot diagram {cache_file} to {output_file}") | ||
shutil.copy2(cache_file, output_file) | ||
else: | ||
core.individual_sage_conversion( | ||
sageplot, outformat, dest_dir, sage_executable_cmd | ||
) | ||
if output_file.exists(): | ||
log.debug( | ||
f"Created sageplot diagram {output_file}; saving a copy to cache as {cache_file}" | ||
) | ||
shutil.copy2(output_file, cache_file) | ||
log.debug("Finished individual_sage function") | ||
|
||
|
||
def individual_latex_image( | ||
latex_image: str, | ||
outformat: str, | ||
dest_dir: Path, | ||
method: str, | ||
cache_dir: Path, | ||
skip_cache: bool = False, | ||
) -> None: | ||
""" | ||
Checks whether a cached version of the diagram in the correct outformat exists. If it does, copies it to the dest_dir and returns. If it does not, calls the core.individual_latex_image_conversion function to generate the diagram in the correct outformat and then copies it to the dest_dir. In the latter case, also makes a copy to the cached version in the cache_dir. | ||
- outformat will be 'all' or a file extension. | ||
""" | ||
log.debug("Using the CLI's individual_latex function") | ||
asset_file = Path(latex_image).resolve() | ||
outformats = ["png", "pdf", "svg", "eps"] if outformat == "all" else [outformat] | ||
cache_files = { | ||
ext: cache_asset_filename(asset_file, ext, "latex_image", cache_dir) | ||
for ext in outformats | ||
} | ||
output_files = { | ||
ext: dest_dir / asset_file.with_suffix(f".{ext}").name for ext in outformats | ||
} | ||
# In case outformat was "all", we check whether all the desired outformats are cached. If not, we generate all of them (since it is only the first that is time-intensive) | ||
all_cached = True | ||
for ext in outformats: | ||
if not cache_files[ext].exists(): | ||
all_cached = False | ||
break | ||
if all_cached and not skip_cache: | ||
for ext in outformats: | ||
log.debug( | ||
f"Copying cached latex-image {cache_files[ext]} to {output_files[ext]}" | ||
) | ||
shutil.copy2(cache_files[ext], output_files[ext]) | ||
else: | ||
core.individual_latex_image_conversion(latex_image, outformat, dest_dir, method) | ||
for ext in outformats: | ||
if output_files[ext].exists(): | ||
log.debug( | ||
f"Created latex-image {output_files[ext]}; saving a copy to cache as {cache_files[ext]}" | ||
) | ||
shutil.copy2(output_files[ext], cache_files[ext]) | ||
log.debug("Finished individual_latex function") | ||
|
||
|
||
def cache_asset_filename( | ||
asset_file: Path, extension: str, asset_type: str, cache_dir: Path | ||
) -> Path: | ||
asset_content = asset_file.read_bytes() | ||
hash = hashlib.md5() | ||
# hash the asset file | ||
hash.update(asset_content) | ||
# include the asset_type in hash | ||
hash.update(asset_type.encode()) | ||
asset_hash = hash.hexdigest() | ||
# create the cache file name | ||
return cache_dir / f"{asset_hash}.{extension}" |
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 |
---|---|---|
@@ -1 +1 @@ | ||
{"2.11.5": {"project.ptx": "20b8cd8099dc3b21a04bcf94d9386446f07e7b11eb1bc3247875546a173b0b3c", "codechat_config.yaml": "d1314aefecb11bf4dee0775ba095ad90bd508c2023c8be1a4dc02ed71406afee", ".gitignore": "56a9ffb6b221bea906348ab3ccb4af37a42d89aeeaaff1e8abf46c06217fd05c", ".devcontainer.json": "4a3c939ffe2fdae8670da5254984419107a794013ef0487e2cbc84db465d0371", "pretext-cli.yml": "acc8fa861bba25478048c1d731a0e2057a746dff9542ee007766111886660f8a"}} | ||
{"2.11.5": {"project.ptx": "20b8cd8099dc3b21a04bcf94d9386446f07e7b11eb1bc3247875546a173b0b3c", "codechat_config.yaml": "d1314aefecb11bf4dee0775ba095ad90bd508c2023c8be1a4dc02ed71406afee", ".gitignore": "56a9ffb6b221bea906348ab3ccb4af37a42d89aeeaaff1e8abf46c06217fd05c", ".devcontainer.json": "4a3c939ffe2fdae8670da5254984419107a794013ef0487e2cbc84db465d0371", "pretext-cli.yml": "acc8fa861bba25478048c1d731a0e2057a746dff9542ee007766111886660f8a"}, "2.12.1": {"project.ptx": "8772864348ecfad7eff81240ff2463f0956a605dfb0cb8eeb0edaf7943991ca8", "codechat_config.yaml": "8fbb8c5e888da3b49e070dd189195becc104b082f3d3c3dfbd0de0f9652a1c7e", ".gitignore": "d0a7e5ca8ec411488d2b2e4cc5c0871313dcacdbfb74710c43ae935608bc8b7e", ".devcontainer.json": "7113274fe6b005b742e68381d477cee3376b4bff8f673eade0b6300a88b2a8eb", "pretext-cli.yml": "a01524273301bfb48b1d72ec0c968f51e63f81a299007b2604e53cb53509ff3b"}} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import typing as t | ||
|
||
# AssetTable is a dictionary of asset types mapped to dictionaries of xml:ids to hashes of the source of that xml:id. | ||
AssetTable = t.Dict[str, t.Dict[str, bytes]] | ||
AssetTable = t.Dict[str, str] |
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
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
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