Skip to content

Commit

Permalink
Add option to write/build files in a specified directory (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
ixjlyons authored Jul 12, 2021
1 parent e6385d4 commit 6c63115
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
26 changes: 17 additions & 9 deletions pyxdsm/XDSM.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ def _compose_optional_package_list(self):

return optional_packages_str

def write(self, file_name, build=True, cleanup=True, quiet=False):
def write(self, file_name, build=True, cleanup=True, quiet=False, outdir="."):
"""
Write output files for the XDSM diagram. This produces the following:
Expand All @@ -510,10 +510,13 @@ def write(self, file_name, build=True, cleanup=True, quiet=False):
build : bool
Flag that determines whether the standalone PDF of the XDSM will be compiled.
Default is True.
cleanup: bool
cleanup : bool
Flag that determines if pdflatex build files will be deleted after build is complete
quiet: bool
quiet : bool
Set to True to suppress output from pdflatex.
outdir : str
Path to an existing directory in which to place output files. If a relative
path is given, it is interpreted relative to the current working directory.
"""
nodes = self._build_node_grid()
edges = self._build_edges()
Expand All @@ -534,7 +537,8 @@ def write(self, file_name, build=True, cleanup=True, quiet=False):
optional_packages=optional_packages_str,
)

with open(file_name + ".tikz", "w") as f:
base_output_fp = os.path.join(outdir, file_name)
with open(base_output_fp + ".tikz", "w") as f:
f.write(tikzpicture_str)

tex_str = tex_template.format(
Expand All @@ -546,19 +550,23 @@ def write(self, file_name, build=True, cleanup=True, quiet=False):
version=pyxdsm_version,
)

if file_name:
with open(file_name + ".tex", "w") as f:
f.write(tex_str)
with open(base_output_fp + ".tex", "w") as f:
f.write(tex_str)

if build:
command = ["pdflatex", "-halt-on-error", "-interaction=nonstopmode"]
command = [
"pdflatex",
"-halt-on-error",
"-interaction=nonstopmode",
"-output-directory={}".format(outdir),
]
if quiet:
command += ["-interaction=batchmode", "-halt-on-error"]
command += [f"{file_name}.tex"]
subprocess.run(command, check=True)
if cleanup:
for ext in ["aux", "fdb_latexmk", "fls", "log"]:
f_name = "{}.{}".format(file_name, ext)
f_name = "{}.{}".format(base_output_fp, ext)
if os.path.exists(f_name):
os.remove(f_name)

Expand Down
17 changes: 17 additions & 0 deletions tests/test_xdsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,23 @@ def test_tikz_content(self):
# To be sure, check the length, otherwise a missing last line could get unnoticed because of using zip
self.assertEqual(len(new_lines), len(sample_lines))

def test_write_outdir(self):
fname = "test"

for abspath in [True, False]:
subdir = tempfile.mkdtemp(dir=self.tempdir)
outdir = subdir if abspath else os.path.basename(subdir)

x = XDSM()
x.add_system("x", FUNC, "x")
x.write(fname, outdir=outdir)

for ext in [".tex", ".tikz", ".pdf"]:
self.assertTrue(os.path.isfile(os.path.join(subdir, fname + ext)))

# no files outside the subdirs
self.assertFalse(any(os.path.isfile(fp) for fp in os.listdir(self.tempdir)))


if __name__ == "__main__":
unittest.main()

0 comments on commit 6c63115

Please sign in to comment.