<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Writing your own synopsis script</title><link rel="stylesheet" href="synopsis.css" type="text/css" /><meta name="generator" content="DocBook XSL Stylesheets V1.73.2" /><link rel="start" href="index.html" title="Synopsis Tutorial" /><link rel="up" href="scripting.html" title="Chapter 3. Scripting And Extending Synopsis" /><link rel="prev" href="pipeline.html" title="Composing A Pipeline" /><link rel="next" href="processors.html" title="Chapter 4. Processor Design" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Writing your own synopsis script</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="pipeline.html">Prev</a> </td><th width="60%" align="center">Chapter 3. Scripting And Extending Synopsis</th><td width="20%" align="right"> <a accesskey="n" href="processors.html">Next</a></td></tr></table></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="script"></a>Writing your own synopsis script</h2></div></div></div><p>The synopsis framework provides a function <code class="function">process</code> that lets you declare and expose processors as commands so they can be used per command line: </p><pre class="programlisting"># # Copyright (C) 2006 Stefan Seefeld # All rights reserved. # Licensed to the public under the terms of the GNU LGPL (>= 2), # see the file COPYING for details. # from Synopsis.process import process from Synopsis.Processor import Processor, Parameter, Composite from Synopsis.Parsers import Cxx from Synopsis.Parsers import Python from Synopsis.Processors import Linker from Synopsis.Processors import Comments from Synopsis.Formatters import HTML from Synopsis.Formatters import Dot from Synopsis.Formatters import Dump class Joker(Processor): parameter = Parameter(':-)', 'a friendly parameter') def process(self, ir, **keywords): # override default parameter values self.set_parameters(keywords) # merge in IR from 'input' parameter if given self.ir = self.merge_input(ir) print 'this processor is harmless...', self.parameter # write to output (if given) and return IR return self.output_and_return_ir() cxx = Cxx.Parser(base_path='../src') ss = Comments.Translator(filter = Comments.SSFilter(), processor = Comments.Grouper()) ssd_prev = Comments.Translator(filter = Comments.SSDFilter(), processor = Composite(Comments.Previous(), Comments.Grouper())) javadoc = Comments.Translator(markup='javadoc', filter = Comments.JavaFilter(), processor = Comments.Grouper()) rst = Comments.Translator(markup='rst', filter = Comments.SSDFilter(), processor = Comments.Grouper()) process(cxx_ss = Composite(cxx, ss), cxx_ssd_prev = Composite(cxx, ssd_prev), cxx_javadoc = Composite(cxx, javadoc), cxx_rst = Composite(cxx, rst), link = Linker(), html = HTML.Formatter(), dot = Dot.Formatter(), joker = Joker(parameter = '(-;')) </pre><p> </p><p>With such a script <code class="filename">synopsis.py</code> it is possible to call </p><pre class="programlisting">python synopsis.py cxx_ssd --output=Bezier.syn Bezier.h </pre><p> to do the same as in <a class="xref" href="using.html" title="Chapter 2. Using the synopsis tool">Chapter 2, <i>Using the synopsis tool</i></a>, but with much more flexibility. Let's have a closer look at how this script works:</p><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="importing"></a>Importing all desired processors</h3></div></div></div><p>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 <code class="function">process</code> function, together with a number of predefined processors. </p></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="composing"></a>Composing new processors</h3></div></div></div><p>As outlined in <a class="xref" href="pipeline.html" title="Composing A Pipeline">the section called “Composing A Pipeline”</a>, processors can be composed into pipelines, which are themselfs new (composite) processors. Synopsis provides a <span class="type">Composite</span> type for convenient pipeline construction. Its constructor takes a list of processors that the process method will iterate over. </p></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="extending"></a>Defining New Processors</h3></div></div></div><p>New processors can be defined by deriving from <span class="type">Processor</span> or any of its subclasses. As outlined in <a class="xref" href="processor.html" title="The Processor class">the section called “The Processor class”</a>, it has only to respect the semantics of the <code class="function">process</code> method.</p></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="process"></a>Exposing The Commands</h3></div></div></div><p>With all these new processrs defined, they need to be made accessible to be called per command line. That is done with the <code class="function">process</code> function. It sets up a dictionary of named processors, with which the script can be invoked as </p><pre class="programlisting">python synopsis.py joker </pre><p> which will invoke the joker's <code class="function">process</code> method with any argument that was provided passed as a named value (keyword). </p></div></div><div class="navfooter"><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="pipeline.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="scripting.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="processors.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Composing A Pipeline </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 4. Processor Design</td></tr></table></div></body></html>