Generate files from the current directory instead of the output directory.

This commit is contained in:
Mikaël Capelle 2018-03-19 09:56:44 +01:00
parent 8a8cb4d177
commit 47944b3dd2

View File

@ -8,21 +8,6 @@ import os
import subprocess import subprocess
class change_directory:
"""Context manager for changing the current working directory"""
def __init__(self, newPath):
self.newPath = os.path.expanduser(newPath)
def __enter__(self):
self.savedPath = os.getcwd()
os.chdir(self.newPath)
def __exit__(self, etype, value, traceback):
os.chdir(self.savedPath)
class CompileResult(enum.Enum): class CompileResult(enum.Enum):
ALREADY_EXISTS = 1 ALREADY_EXISTS = 1
@ -48,7 +33,7 @@ class documentclass(element):
} }
output_aux_folder = '.pyltk' output_aux_folder = '.pyltk'
auto_tex_folder = '{}/tex2pdf'.format(output_aux_folder) auto_tex_folder_name = 'tex2pdf'
def __init__(self, classname, childrens=[], options=[], def __init__(self, classname, childrens=[], options=[],
packages=[], preamble=[]): packages=[], preamble=[]):
@ -128,22 +113,26 @@ class documentclass(element):
res = CompileResult.ERROR res = CompileResult.ERROR
with change_directory(outdir): output_aux_folder = os.path.join(outdir, self.output_aux_folder)
auto_tex_folder = os.path.join(output_aux_folder,
self.auto_tex_folder_name)
os.makedirs(self.output_aux_folder, exist_ok=True) # Make aux directory
os.makedirs(output_aux_folder, exist_ok=True)
# Remove extension if exists
if outfile.endswith('.pdf'): if outfile.endswith('.pdf'):
outfile = outfile[:-4] outfile = outfile[:-4]
to_delete = False to_delete = False
if infile == 'auto': if infile == 'auto':
os.makedirs(self.auto_tex_folder, exist_ok=True) os.makedirs(auto_tex_folder, exist_ok=True)
infile = '{}/{}.tex'.format(self.auto_tex_folder, outfile) infile = '{}/{}.tex'.format(auto_tex_folder, outfile)
if infile is None: if infile is None:
to_delete = True to_delete = True
infile = '.pyltk-{}.tex'.format(outfile) infile = os.path.join(outdir, '.pyltk-{}.tex'.format(outfile))
to_create = True to_create = True
new_content = self.content() new_content = self.content()
@ -164,12 +153,13 @@ class documentclass(element):
with open(infile, 'w') as infd: with open(infile, 'w') as infd:
infd.write(self.content()) infd.write(self.content())
call = ['pdflatex', '-halt-on-error', call = ['pdflatex', '-halt-on-error',
'-output-directory', self.output_aux_folder, '-output-directory', output_aux_folder,
'-jobname', outfile, infile] '-jobname', outfile, infile]
if subprocess.call(call, stdout=outlog) == 0: if subprocess.call(call, stdout=outlog) == 0:
os.rename('{}/{}.pdf'.format( os.rename(
self.output_aux_folder, outfile), os.path.join(
'{}.pdf'.format(outfile)) output_aux_folder, outfile + '.pdf'),
os.path.join(outdir, outfile + '.pdf'))
res = CompileResult.SUCCESS res = CompileResult.SUCCESS
else: else:
res = CompileResult.ERROR res = CompileResult.ERROR