1from __future__ import print_function
2
3from zzipdoc.match import Match
4
5class TextFileHeader:
6    """ scan for a comment block at the source file start and fill the
7    inner text into self.comment - additionally scan for the first
8    #include statement and put the includename into self.mainheader
9    (TextFileHeader re-exports all => TextFile methods for processing)"""
10    def __init__(self, textfile = None):
11        self.textfile = textfile # TextFile
12        self.comment = ""    # src'style
13        self.mainheader = ""     # src'style
14    def parse(self, textfile = None):
15        if textfile is not None:
16            self.textfile = textfile
17        if self.textfile is None:
18            return False
19        x = Match()
20        text = self.textfile.get_src_text()
21        if not text:
22            print("nonexistent file: " + self.textfile.get_filename())
23            return False
24        if text & x(r"(?s)[/][*]+(\s(?:.(?!\*\/))*.)\*\/"
25                    r"(?:\s*\#(?:define|ifdef|endif)[ ]*\S*[ ]*\S*)*"
26                    r"(\s*\#include\s*<[^<>]*>(?:\s*//[^\n]*)?)"):
27            self.comment = x[1]
28            self.mainheader = x[2].strip()
29        elif text & x(r"(?s)[/][*]+(\s(?:.(?!\*\/))*.)\*\/"):
30            self.comment = x[1]
31        elif text & x(r"(?s)(?:\s*\#(?:define|ifdef|endif)[ ]*\S*[ ]*\S*)*"
32                      r"(\s*\#include\s*<[^<>]*>(?:\s*//[^\n]*)?)"):
33            self.mainheader = x[1].strip()
34        return True
35    def src_mainheader(self):
36        return self.mainheader
37    def src_filecomment(self):
38        return self.comment
39    # re-export textfile functions - allows textfileheader to be used instead
40    def get_filename(self):
41        return self.textfile.get_filename()
42    def get_src_text(self):
43        return self.textfile.get_src_text()
44    def get_xml_text(self):
45        return self.textfile.get_src_text()
46    def line_src__text(self, offset):
47        return self.textfile.line_src_text(offset)
48    def line_xml__text(self, offset):
49        return self.textfile.line_xml_text(offset)
50