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
|
2016-02-16 05:32:35 +00:00
|
|
|
import errno
|
2016-02-16 05:51:56 +00:00
|
|
|
import collections
|
2016-02-11 03:22:23 +00:00
|
|
|
|
2016-02-17 19:19:48 +00:00
|
|
|
from .utils import logger, statfiles
|
2016-02-12 21:24:21 +00:00
|
|
|
from .typeguesser import guess, knownextensions
|
2016-02-11 03:22:23 +00:00
|
|
|
|
|
|
|
|
2016-02-16 07:52:08 +00:00
|
|
|
class SourceCollection(collections.MutableMapping):
|
2016-02-11 03:22:23 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
2016-02-17 16:35:53 +00:00
|
|
|
return '<%s:(%s docs)>' % (self.__class__.__name__, len(self))
|
2016-02-11 03:22:23 +00:00
|
|
|
|
2016-02-17 07:40:09 +00:00
|
|
|
def __init__(self, args=None):
|
|
|
|
if args is None:
|
|
|
|
return
|
2016-02-16 07:52:08 +00:00
|
|
|
dirs = [os.path.abspath(x) for x in args]
|
|
|
|
results = [os.path.exists(x) for x in dirs]
|
2016-02-16 05:51:56 +00:00
|
|
|
|
2016-02-11 23:16:12 +00:00
|
|
|
if not all(results):
|
2016-02-16 07:52:08 +00:00
|
|
|
for result, sdir in zip(results, dirs):
|
2016-02-16 05:15:29 +00:00
|
|
|
logger.critical("Directory does not exist: " + sdir)
|
2016-02-16 05:32:35 +00:00
|
|
|
raise IOError(errno.ENOENT, os.strerror(errno.ENOENT), sdir)
|
2016-02-11 23:16:12 +00:00
|
|
|
|
2016-02-16 07:52:08 +00:00
|
|
|
for sdir in dirs:
|
2016-02-11 23:16:12 +00:00
|
|
|
for fname in os.listdir(sdir):
|
2016-02-17 07:40:09 +00:00
|
|
|
candidates = list()
|
2016-02-11 23:16:12 +00:00
|
|
|
possible = os.path.join(sdir, fname)
|
|
|
|
if os.path.isfile(possible):
|
2016-02-16 18:44:24 +00:00
|
|
|
candidates.append(SourceDocument(possible))
|
2016-02-17 07:40:09 +00:00
|
|
|
elif os.path.isdir(possible):
|
2016-02-11 23:16:12 +00:00
|
|
|
stem = os.path.basename(fname)
|
|
|
|
for ext in knownextensions:
|
|
|
|
possible = os.path.join(sdir, fname, stem + ext)
|
|
|
|
if os.path.isfile(possible):
|
2016-02-16 18:44:24 +00:00
|
|
|
candidates.append(SourceDocument(possible))
|
|
|
|
else:
|
|
|
|
logger.warning("Skipping non-directory, non-plain file %s",
|
|
|
|
possible)
|
|
|
|
continue
|
|
|
|
for candy in candidates:
|
|
|
|
if candy.stem in self:
|
2016-02-17 07:40:09 +00:00
|
|
|
logger.warning("Ignoring duplicate is %s", candy.filename)
|
|
|
|
logger.warning("Existing dup-entry is %s", self[candy.stem].filename)
|
2016-02-16 18:44:24 +00:00
|
|
|
else:
|
|
|
|
self[candy.stem] = candy
|
2016-02-17 07:40:09 +00:00
|
|
|
logger.debug("Discovered %s documents total", len(self))
|
2016-02-16 05:51:56 +00:00
|
|
|
|
|
|
|
def __delitem__(self, key):
|
|
|
|
del self.__dict__[key]
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
return self.__dict__[key]
|
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
self.__dict__[key] = value
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return iter(self.__dict__)
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.__dict__)
|
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)
|
2016-02-16 05:32:35 +00:00
|
|
|
raise IOError(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-16 08:23:58 +00:00
|
|
|
self.doctype = self._doctype()
|
2016-02-17 08:17:49 +00:00
|
|
|
self.status = 'source'
|
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.resources = False # -- assume no ./images/, ./resources/
|
2016-02-17 07:40:09 +00:00
|
|
|
parentbase = os.path.basename(self.dirname)
|
|
|
|
if parentbase == self.stem:
|
2016-02-17 19:19:48 +00:00
|
|
|
self.statinfo = statfiles(self.dirname, relative=self.dirname)
|
2016-02-17 07:40:09 +00:00
|
|
|
else:
|
2016-02-17 19:19:48 +00:00
|
|
|
self.statinfo = statfiles(self.filename, relative=self.dirname)
|
2016-02-11 03:22:23 +00:00
|
|
|
|
2016-02-16 08:23:58 +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
|
|
|
|
2016-02-16 05:51:56 +00:00
|
|
|
#
|
2016-02-11 03:22:23 +00:00
|
|
|
# -- end of file
|