# Copyright (c) 2016 Comcast Cable Communications Management, LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Generate .siphon source fragments for later processing

import json
import logging
import os
import re

"""List of (regexp, siphon_name) tuples for matching the start of C
   initializer blocks in source files. Each siphon class registers
   themselves on this list."""
siphon_patterns = []

class Generate(object):
    """Matches a siphon comment block start"""
    siphon_block_start = re.compile("^\s*/\*\?\s*(.*)$")

    """Matches a siphon comment block stop"""
    siphon_block_stop = re.compile("^(.*)\s*\?\*/\s*$")

    """Siphon block directive delimiter"""
    siphon_block_delimiter = "%%"

    """Matches a siphon block directive such as
       '%clicmd:group_label Debug CLI%'"""
    siphon_block_directive = re.compile("(%s)\s*([a-zA-Z0-9_:]+)\s+(.*)\s*(%s)" % \
            (siphon_block_delimiter, siphon_block_delimiter))

    """Matches the start of an initializer block"""
    siphon_initializer = re.compile("\s*=")

    """Collated output for each siphon"""
    output = None

    """Directory prefix to strip from input filenames to keep things tidy."""
    input_prefix = None

    """List of known siphons"""
    known_siphons = None

    """Logging handler"""
    log = None


    def __init__(self, output_directory, input_prefix):
        super(Generate, self).__init__()
        self.log = logging.getLogger("siphon.generate")

        # Build a list of known siphons
        self.known_siphons = []
        for item in siphon_patterns:
            siphon = item[1]
            if siphon not in self.known_siphons:
                self.known_siphons.append(siphon)

        # Setup information for siphons we know about
        self.output = {}
        for siphon in self.known_siphons:
            self.output[siphon] = {
                    "file": "%s/%s.siphon" % (output_directory, siphon),
                    "global": {},
                    "items": [],
                }

        self.input_prefix = input_prefix


    """
    count open and close braces in str
    return (0, index) when braces were found and count becomes 0.
    index indicates the position at which the last closing brace was
    found.
    return (-1, -1) if a closing brace is found before any opening one.
    return (count, -1) if not all opening braces are closed, count is the
    current depth
    """
    def count_braces(self, str, count=0, found=False):
        for index in range(0, len(str)):
            if str[index] == '{':
                count += 1;
                found = True
            elif str[index] == '}':
                if count == 0:
                    # means we never found an open brace
                    return (-1, -1)
                count -= 1;

            if count == 0 and found:
                return (count, index)

        return (count, -1)

    def parse(self, filename):
        # Strip the current directory off the start of the
        # filename for brevity
        if filename[0:len(self.input_prefix)] == self.input_prefix:
            filename = filename[len(self.input_prefix):]
            if filename[0] == "/":
                filename = filename[1:]

        # Work out the abbreviated directory name
        directory = os.path.dirname(filename)
        if directory[0:2] == "./":
            directory = directory[2:]
        elif directory[0:len(self.input_prefix)] == self.input_prefix:
            directory = directory[len(self.input_prefix):]
        if directory[0] == "/":
            directory = directory[1:]

        # Open the file and explore its contents...
        self.log.info("Siphoning from %s." % filename)
        directives = {}
        with open(filename) as fd:
            siphon = None
            close_siphon = None
            siphon_block = ""
            in_block = False
            line_num = 0
            siphon_line = 0

            for line in fd:
                line_num += 1
                str = line[:-1] # filter \n

                """See if there is a block directive and if so extract it"""
                def process_block_directive(str, directives):
                    m = self.siphon_block_directive.search(str)
                    if m is not None:
                        k = m.group(2)
                        v = m.group(3).strip()
                        directives[k] = v
                        # Return only the parts we did not match
                        return str[0:m.start(1)] + str[m.end(4):]

                    return str

                def process_block_prefix(str):
                    if str.startswith(" * "):
                        str = str[3:]
                    elif str == " *":
                        str = ""
                    return str

                if not in_block:
                    # See if the line contains the start of a siphon doc block
                    m = self.siphon_block_start.search(str)
                    if m is not None:
                        in_block = True
                        t = m.group(1)

                        # Now check if the block closes on the same line
                        m = self.siphon_block_stop.search(t)
                        if m is not None:
                            t = m.group(1)
                            in_block = False

                        # Check for directives
                        t = process_block_directive(t, directives)

                        # Filter for normal comment prefixes
                        t = process_block_prefix(t)

                        # Add what is left
                        siphon_block += t

                        # Skip to next line
                        continue

                else:
                    # Check to see if we have an end block marker
                    m = self.siphon_block_stop.search(str)
                    if m is not None:
                        in_block = False
                        t = m.group(1)
                    else:
                        t = str

                    # Check for directives
                    t = process_block_directive(t, directives)

                    # Filter for normal comment prefixes
                    t = process_block_prefix(t)

                    # Add what is left
                    siphon_block += t + "\n"

                    # Skip to next line
                    continue


                if siphon is None:
                    # Look for blocks we need to siphon
                    for p in siphon_patterns:
                        if p[0].match(str):
                            siphon = [ p[1], str + "\n", 0 ]
                            siphon_line = line_num

                            # see if we have an initializer
                            m = self.siphon_initializer.search(str)
                            if m is not None:
                                # count the braces on this line
                                (count, index) = \
                                    self.count_braces(str[m.start():])
                                siphon[2] = count
                                # TODO - it's possible we have the
                                # initializer all on the first line
                                # we should check for it, but also
                                # account for the possibility that
                                # the open brace is on the next line
                                #if count == 0:
                                #    # braces balanced
                                #    close_siphon = siphon
                                #    siphon = None
                            else:
                                # no initializer: close the siphon right now
                                close_siphon = siphon
                                siphon = None
                else:
                    # See if we should end the siphon here - do we have
                    # balanced braces?
                    (count, index) = self.count_braces(str,
                            count=siphon[2], found=True)
                    if count == 0:
                        # braces balanced - add the substring and
                        # close the siphon
                        siphon[1] += str[:index+1] + ";\n"
                        close_siphon = siphon
                        siphon = None
                    else:
                        # add the whole string, move on
                        siphon[2] = count
                        siphon[1] += str + "\n"

                if close_siphon is not None:
                    # Write the siphoned contents to the right place
                    siphon_name = close_siphon[0]

                    # Copy directives for the file
                    details = {}
                    for key in directives:
                        if ":" in key:
                            (sn, label) = key.split(":")
                            if sn == siphon_name:
                                details[label] = directives[key]
                        else:
                            details[key] = directives[key]

                    # Copy details for this block
                    details['file'] = filename
                    details['directory'] = directory
                    details['line_start'] = siphon_line
                    details['line_end'] = line_num
                    details['siphon_block'] = siphon_block.strip()
                    details["block"] = close_siphon[1]

                    # Store the item
                    self.output[siphon_name]['items'].append(details)

                    # All done
                    close_siphon = None
                    siphon_block = ""

            # Update globals
            for key in directives.keys():
                if ':' not in key:
                    continue

                if filename.endswith("/dir.dox"):
                    # very special! use the parent directory name
                    l = directory
                else:
                    l = filename

                (sn, label) = key.split(":")

                if sn not in self.output:
                    self.output[sn] = {}
                if 'global' not in self.output[sn]:
                    self.output[sn]['global'] = {}
                if l not in self.output[sn]['global']:
                    self.output[sn]['global'][l] = {}

                self.output[sn]['global'][l][label] = directives[key]

    def deliver(self):
        # Write out the data
        for siphon in self.output.keys():
            self.log.info("Saving siphon data %s." % siphon)
            s = self.output[siphon]
            with open(s['file'], "a") as fp:
                json.dump(s, fp,
                    separators=(',', ': '), indent=4, sort_keys=True)

