mirror of https://github.com/tLDP/python-tldp
create OutputDirectory and OutputTree
an OutputTree must exist already, contains all of the OutputDocuments an OutputDocument determines the stem from the dirname and depends on the OutputNamingConvention for choosing names for the primary files of each generated output type
This commit is contained in:
parent
f1c40c1e8c
commit
b6076fec82
|
@ -3,40 +3,82 @@
|
||||||
from __future__ import absolute_import, division, print_function
|
from __future__ import absolute_import, division, print_function
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import errno
|
||||||
|
|
||||||
from .utils import logger
|
from .utils import logger
|
||||||
|
|
||||||
|
|
||||||
class OutputDir(object):
|
class OutputNamingConvention(object):
|
||||||
|
|
||||||
|
expected = ['name_txt', 'name_pdf', 'name_htmls', 'name_html']
|
||||||
|
|
||||||
|
def __init__(self, stem, dirname):
|
||||||
|
self.stem = stem
|
||||||
|
self.dirname = dirname
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name_txt(self):
|
||||||
|
return os.path.join(self.dirname, self.stem + '.txt')
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name_pdf(self):
|
||||||
|
return os.path.join(self.dirname, self.stem + '.pdf')
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name_html(self):
|
||||||
|
return os.path.join(self.dirname, self.stem + '.html')
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name_htmls(self):
|
||||||
|
return os.path.join(self.dirname, self.stem + '-single.html')
|
||||||
|
|
||||||
|
|
||||||
|
class OutputDirectory(OutputNamingConvention):
|
||||||
|
|
||||||
|
def __init__(self, dirname):
|
||||||
|
self.dirname = os.path.abspath(dirname)
|
||||||
|
self.stem = os.path.basename(self.dirname)
|
||||||
|
parent = os.path.dirname(self.dirname)
|
||||||
|
if not os.path.isdir(parent):
|
||||||
|
logger.critical("Missing output tree %s.", parent)
|
||||||
|
raise OSError(errno.ENOENT, os.strerror(errno.ENOENT), parent)
|
||||||
|
if not os.path.isdir(self.dirname):
|
||||||
|
logger.info("Creating output directory %s.", dirname)
|
||||||
|
os.mkdir(dirname)
|
||||||
|
super(OutputDirectory, self).__init__(self.stem, self.dirname)
|
||||||
|
|
||||||
|
def clear(self):
|
||||||
|
logger.info("%s clearing directory", self.stem, self.dirname)
|
||||||
|
for oformat in self.expected:
|
||||||
|
name = getattr(self, oformat, None)
|
||||||
|
assert name is not None
|
||||||
|
if os.path.exists(name):
|
||||||
|
logger.info("%s removing file %s", self.stem, name)
|
||||||
|
os.unlink(name)
|
||||||
|
|
||||||
|
|
||||||
|
class OutputTree(object):
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<%s:%s>' % (self.__class__.__name__, self.outputdir)
|
return '<%s:(%s docs)>' % \
|
||||||
|
(self.__class__.__name__, len(self.docs))
|
||||||
|
|
||||||
def __init__(self, outputdir):
|
def __init__(self, dirname):
|
||||||
self.outputdir = os.path.abspath(outputdir)
|
if not os.path.isdir(dirname):
|
||||||
self.stem = os.path.basename(outputdir)
|
logger.critical("Directory %s must already exist.", dirname)
|
||||||
self.parent = os.path.dirname(self.outputdir)
|
raise OSError(errno.ENOENT, os.strerror(errno.ENOENT), dirname)
|
||||||
|
self.dirname = dirname
|
||||||
|
self.docs = list()
|
||||||
|
self.enumerateDocuments()
|
||||||
|
|
||||||
def mkdir(self):
|
def enumerateDocuments(self):
|
||||||
if not os.path.exists(self.parent):
|
for fname in os.listdir(self.dirname):
|
||||||
raise OSError("Missing parent directory: " + self.parent)
|
name = os.path.join(self.dirname, fname)
|
||||||
os.mkdir(self.outputdir)
|
if not os.path.isdir(name):
|
||||||
|
logger.warning("Skipping non-directory %s (in %s)",
|
||||||
@property
|
name, self.dirname)
|
||||||
def txt_name(self):
|
self.docs.append(OutputDirectory(name))
|
||||||
return os.path.join(self.outputdir, self.stem, '.txt')
|
|
||||||
|
|
||||||
@property
|
|
||||||
def pdf_name(self):
|
|
||||||
return os.path.join(self.outputdir, self.stem, '.pdf')
|
|
||||||
|
|
||||||
@property
|
|
||||||
def html_name(self):
|
|
||||||
return os.path.join(self.outputdir, self.stem, '.html')
|
|
||||||
|
|
||||||
@property
|
|
||||||
def htmls_name(self):
|
|
||||||
return os.path.join(self.outputdir, self.stem, '-single.html')
|
|
||||||
|
|
||||||
|
|
||||||
|
#
|
||||||
# -- end of file
|
# -- end of file
|
||||||
|
|
Loading…
Reference in New Issue