#! /usr/bin/env python

from Synopsis import config
from Synopsis.process import process
from Synopsis.Processor import *
from Synopsis.Processors import *
from Synopsis.Parsers import Python
from Synopsis.Parsers import Cxx
from Synopsis.Formatters import HTML
from Synopsis.Formatters.HTML.Views import *
from Synopsis.Formatters import SXR
from Synopsis.Formatters import Texinfo
from Synopsis.Formatters import DocBook
from Synopsis.Formatters import Dump

from distutils import sysconfig
import sys, os, os.path

srcdir = '@srcdir@'
topdir = '@topdir@'
python = Python.Parser(base_path = topdir + os.sep,
                       sxr_prefix='sxr')

cxx = Cxx.Parser(base_path = topdir + os.sep,
                 cppflags = ['-I%s'%(topdir + '/include'),
                             '-I%s'%(sysconfig.get_python_inc())],
                 sxr_prefix='sxr')

html = HTML.Formatter(sxr_prefix='sxr')

sxr = SXR.Formatter(url = '/sxr',
                    src_dir = '@topdir@',
                    exclude = ['build', 'dist', 'doc', 'share', 'debian',
                               '.svn', 'autom4te.cache',
                               '*.pyc', '*.so', '*~'],
                    sxr_prefix='sxr',
                    sxr_template = os.path.join(srcdir, 'sxr-template.html'))

class MyLinker(Processor):
   """Build a processor that collects ASGs from multiple scopes."""

   scopes = Parameter('', 'A comma-separated list of scopes')

   def process(self, ir, **kwds):

      self.set_parameters(kwds)
      ir = self.merge_input(ir)
      processor = ScopeStripper(scope='Synopsis')
      # strip off package prefix
      ir = processor.process(ir)
      self.ir = IR.IR(files = ir.files, asg = ASG.ASG(types = ir.asg.types), sxr = ir.sxr)
      for s in self.scopes.split(','):
         package, module = s.rsplit('.', 1)
         # filter out unwated modules
         self.ir.asg.declarations.extend([d for d in ir.asg.declarations if d.name == (module,)])
      self.output_and_return_ir()

process(python = python,
        cxx = cxx,
        link_python = Linker(sxr_prefix='sxr'),
        archive = Processor(),
        link = MyLinker(),
        strip = ScopeStripper(),
        html = html,
        sxr = sxr,
        dump = Dump.Formatter(),
        texi = Texinfo.Formatter(),
        docbook = DocBook.Formatter())
