Writing your own synopsis script

The synopsis framework provides a function process that lets you declare and expose processors as commands so they can be used per command line:

from Synopsis.process import process
from Synopsis.Processor import Processor, Parameter, Composite
from Synopsis.Parsers import Cxx
from Synopsis.Processors import Linker
from Synopsis.Processors.Comments import SSFilter
from Synopsis.Processors.Comments import SSDFilter
from Synopsis.Processors.Comments import JavaFilter
from Synopsis.Processors.Comments import Previous
from Synopsis.Processors.Comments import JavaTags
from Synopsis.Processors.Comments import Grouper1
from Synopsis.Formatters import HTML
from Synopsis.Formatters.HTML import Comments
from Synopsis.Formatters import Dot

cxx = Cxx.Parser(base_path='../src')

cxx_ssd = Composite(cxx, SSDFilter())

html = HTML.Formatter(comment_formatters = [Comments.QuoteHTML(),
                                            Comments.Section(),
                                            Comments.Javadoc()])

class Joker(Processor):
    
   parameter = Parameter(':-)', 'a friendly parameter')

   def process(self, ast, **keywords):
      # override default parameter values
      self.set_parameters(keywords)
      # merge in ast from 'input' parameter if given
      self.ast = self.merge_input(ast)

      print 'this processor is harmless...', self.parameter
      
      # write to output (if given) and return ast
      return self.output_and_return_ast()
        

process(cxx_ssd = cxx_ssd,
        cxx_ss = Composite(cxx, SSFilter()),
        cxx_ssd_prev = Composite(cxx, SSDFilter(), Previous()),
        cxx_javadoc = Composite(cxx, JavaFilter(), JavaTags()),
        link = Linker(Grouper1()),
        html = html,
        dot = Dot.Formatter(),
        joker = Joker(parameter = '(-;'))

        

With such a script synopsis.py it is possible to call

python synopsis.py cxx_ssd --output=Bezier.syn Bezier.h
        

to do the same as in Chapter 2, Using the synopsis tool, but with much more flexibility. Let's have a closer look at how this script works:

Importing all desired processors

As every conventional python script, the first thing to do is to pull in all the definitions that are used later on, in our case the definition of the process function, together with a number of predefined processors.

Composing new processors

As outlined in the section called “Composing a pipeline”, processors can be composed into pipelines, which are themselfs new (composite) processors. Synopsis provides a Composite type for convenient pipeline construction. Its constructor takes a list of processors that the process method will iterate over.

Defining new processors

New processors can be defined by deriving from Processor or any of its subclasses. As outlined in the section called “The Processor class”, it has only to respect the semantics of the process method.

Exposing the commands

With all these new processrs defined, they need to be made accessible to be called per command line. That is done with the process function. It sets up a dictionary of named processors, with which the script can be invoked as

python synopsis.py joker
          

which will invoke the joker's process method with any argument that was provided passed as a named value (keyword).