2016-02-11 03:22:23 +00:00
|
|
|
#! /usr/bin/python
|
|
|
|
|
2016-02-11 19:28:38 +00:00
|
|
|
from __future__ import absolute_import, division, print_function
|
2016-02-11 03:22:23 +00:00
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
from .utils import logger
|
2016-02-12 21:24:21 +00:00
|
|
|
from .typeguesser import guess, knownextensions
|
2016-02-11 03:22:23 +00:00
|
|
|
|
|
|
|
|
2016-02-16 04:58:44 +00:00
|
|
|
class SourceDirs(object):
|
2016-02-11 03:22:23 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
2016-02-11 23:16:12 +00:00
|
|
|
return '<%s:(%s docs)>' % \
|
|
|
|
(self.__class__.__name__, len(self.docs))
|
2016-02-11 03:22:23 +00:00
|
|
|
|
2016-02-11 23:16:12 +00:00
|
|
|
def __init__(self, args):
|
|
|
|
self.sourcedirs = [os.path.abspath(x) for x in args]
|
2016-02-11 03:22:23 +00:00
|
|
|
self.docs = list()
|
2016-02-11 23:16:12 +00:00
|
|
|
self.validateDirs()
|
2016-02-11 03:22:23 +00:00
|
|
|
self.enumerateDocuments()
|
2016-02-12 20:53:30 +00:00
|
|
|
self.docs.sort(key=lambda x: x.stem.lower())
|
2016-02-11 03:22:23 +00:00
|
|
|
|
2016-02-11 23:16:12 +00:00
|
|
|
def validateDirs(self):
|
|
|
|
results = [os.path.exists(x) for x in self.sourcedirs]
|
|
|
|
if not all(results):
|
2016-02-13 07:49:04 +00:00
|
|
|
for result, sdir in zip(results, self.sourcedirs):
|
2016-02-16 05:15:29 +00:00
|
|
|
logger.critical("Directory does not exist: " + sdir)
|
|
|
|
raise OSError(errno.ENOENT, os.strerror(errno.ENOENT), sdir)
|
2016-02-11 23:16:12 +00:00
|
|
|
|
2016-02-11 03:22:23 +00:00
|
|
|
def enumerateDocuments(self):
|
2016-02-11 23:16:12 +00:00
|
|
|
for sdir in self.sourcedirs:
|
|
|
|
docs = list()
|
|
|
|
for fname in os.listdir(sdir):
|
|
|
|
possible = os.path.join(sdir, fname)
|
|
|
|
if os.path.isfile(possible):
|
|
|
|
docs.append(SourceDocument(possible))
|
|
|
|
elif os.path.isdir(fname):
|
|
|
|
stem = os.path.basename(fname)
|
|
|
|
for ext in knownextensions:
|
|
|
|
possible = os.path.join(sdir, fname, stem + ext)
|
|
|
|
if os.path.isfile(possible):
|
|
|
|
docs.append(SourceDocument(possible))
|
|
|
|
logger.debug("Discovered %s documents in %s", len(docs), sdir)
|
|
|
|
self.docs.extend(docs)
|
|
|
|
logger.info("Discovered %s documents total", len(self.docs))
|
2016-02-11 03:22:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SourceDocument(object):
|
|
|
|
|
|
|
|
def __repr__(self):
|
2016-02-11 16:12:16 +00:00
|
|
|
return '<%s:%s (%s)>' % \
|
|
|
|
(self.__class__.__name__, self.filename, self.doctype)
|
2016-02-11 03:22:23 +00:00
|
|
|
|
|
|
|
def __init__(self, filename):
|
|
|
|
# -- canonicalize the pathname we are given.
|
|
|
|
self.filename = os.path.abspath(filename)
|
|
|
|
if not os.path.exists(self.filename):
|
2016-02-16 05:15:29 +00:00
|
|
|
logger.critical("Missing source document: %s", self.filename)
|
|
|
|
raise OSError(errno.ENOENT, os.strerror(errno.ENOENT), self.filename)
|
2016-02-13 07:59:13 +00:00
|
|
|
if not os.path.isfile(self.filename):
|
2016-02-16 05:15:29 +00:00
|
|
|
logger.critical("Source document is not a plain file: %s", self.filename)
|
|
|
|
raise TypeError("Wrong type, not a plain file: " + self.filename)
|
2016-02-11 03:22:23 +00:00
|
|
|
|
2016-02-11 16:17:04 +00:00
|
|
|
logger.debug("Found existing %s", self.filename)
|
2016-02-11 23:16:12 +00:00
|
|
|
self.dirname, self.basename = os.path.split(self.filename)
|
2016-02-11 03:22:23 +00:00
|
|
|
self.stem, self.ext = os.path.splitext(self.basename)
|
|
|
|
self.stat = os.stat(self.filename)
|
|
|
|
|
|
|
|
self.resources = False # -- assume no ./images/, ./resources/
|
|
|
|
self.singlefile = True # -- assume only one file
|
2016-02-11 23:16:12 +00:00
|
|
|
parentdir = os.path.basename(self.dirname)
|
2016-02-11 03:22:23 +00:00
|
|
|
if parentdir == self.stem:
|
|
|
|
self.singlefile = False
|
|
|
|
for rdir in ('resources', 'images'):
|
2016-02-11 23:16:12 +00:00
|
|
|
if os.path.exists(os.path.join(self.dirname, rdir)):
|
2016-02-11 03:22:23 +00:00
|
|
|
self.resources = True
|
|
|
|
|
2016-02-11 16:12:16 +00:00
|
|
|
@property
|
2016-02-11 03:22:23 +00:00
|
|
|
def doctype(self):
|
2016-02-11 16:12:16 +00:00
|
|
|
return guess(self.filename)
|
2016-02-11 03:22:23 +00:00
|
|
|
|
|
|
|
# -- end of file
|