#! /usr/bin/python
# -*- coding: utf8 -*-

from __future__ import absolute_import, division, print_function

import os
from ..utils import logger


class SignatureChecker(object):

    @classmethod
    def signatureLocation(cls, f):
        f.seek(0)
        buf = f.read(1024).lower()
        for sig in cls.signatures:
            try:
                sindex = buf.index(sig.lower())
                logger.debug("Found signature %s in %s at %s; doctype %s.",
                             sig, f.name, sindex, cls)
                return sindex
            except ValueError:
                logger.debug("Signature %s not found in %s for type %s",
                             sig, f.name, cls.__name__)
        return None


class BaseDoctype(object):

    def __init__(self, *args, **kwargs):
        self.source = kwargs.get('source', None)
        self.output = kwargs.get('output', None)
        self.config = kwargs.get('config', None)
        assert None not in (self.source, self.output, self.config)

    def generate(self):
        self.output.prebuild_hook()
        os.chdir(self.output.dirname)
        vector = [self.config_check(),
                  self.create_htmls(),
                  self.create_pdf(),
                  self.create_txt(),
                  self.create_html(),
                  ]
        result = all(vector)
        if result:
            self.output.build_success_hook()
        else:
            self.output.build_failure_hook()
        return all(vector)

#
# -- end of file

@@ -1026,24 +1026,71 @@
 rate(self):%0A
+        def last(l): %0A            return l%5B-1%5D%0A
         self
@@ -1163,38 +1163,70 @@
     
-vector = %5Bself.config_
+command = list()%0A        command.append(self.build_pre
 check()
-,
+)
 %0A   
@@ -1226,34 +1226,93 @@
 ())%0A        
-          
+if not last(command):%0A           return False%0A        command.append(
 self.create_
@@ -1318,17 +1318,17 @@
 _htmls()
-,
+)
 %0A       
@@ -1324,34 +1324,39 @@
 ())%0A        
-          
+command.append(
 self.create_
@@ -1360,17 +1360,17 @@
 te_pdf()
-,
+)
 %0A       
@@ -1366,34 +1366,39 @@
 ())%0A        
-          
+command.append(
 self.create_
@@ -1402,31 +1402,26 @@
 te_txt()
-,%0A     
+)%0A
         
      sel
@@ -1408,29 +1408,39 @@
 ())%0A        
-     
+command.append(
 self.create_
@@ -1449,29 +1449,10 @@
 ml()
-,%0A                  %5D
+)%0A
 %0A   
@@ -1469,22 +1469,23 @@
 t = all(
-vector
+command
 )%0A      
@@ -1620,19 +1620,14 @@
 urn 
-all(vector)
+result
 %0A%0A#%0A

rework tracking of success vector