pytex/PyTeX/format/docstrip_formatter.py

57 lines
1.7 KiB
Python
Raw Normal View History

2022-02-18 15:56:57 +01:00
import tempfile
from .enums import TeXType
from .formatting_config import FormattingConfig
from .pytex_formatter import PyTeXFormatter
from pathlib import Path
import subprocess
import shutil
from typing import List, Tuple
class DocStripFormatter(PyTeXFormatter):
2022-02-18 16:27:03 +01:00
@property
2022-02-18 15:56:57 +01:00
def output_files(self) -> List[str]:
if self.config.tex_out_type == TeXType.TeXClass:
return self.name + '.cls'
elif self.config.tex_out_type == TeXType.TeXClass:
return self.name + '.sty'
else:
raise NotImplementedError
def format(self, build_dir: Path, overwrite: bool = False) -> List[Tuple[str, FormattingConfig]]:
tmp_dir: Path = Path(tempfile.mkdtemp())
shutil.copy(
self.input_file,
tmp_dir
)
if self.input_file.with_suffix('.ins').exists():
result = subprocess.run(
['tex', self.input_file.with_suffix('.ins').name],
cwd=tmp_dir,
stderr=subprocess.STDOUT
)
if not result.returncode == 0:
raise NotImplementedError
else:
result = subprocess.run(
['pdflatex', self.input_file.name],
cwd=tmp_dir,
stderr=subprocess.STDOUT
)
if not result.returncode == 0:
raise NotImplementedError
2022-02-18 16:27:03 +01:00
for file in self.output_files:
2022-02-18 15:56:57 +01:00
outfile = tmp_dir / file
if not outfile.exists():
raise NotImplementedError
shutil.copy(outfile, build_dir)
shutil.rmtree(tmp_dir)
return [] # No future config
2022-02-18 16:27:03 +01:00
@property
2022-02-18 15:56:57 +01:00
def dependencies(self) -> List[str]:
return [] # TODO