1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Writing your own synopsis script</title><link rel="stylesheet" href="synopsis.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.65.1"><link rel="home" href="index.html" title="Synopsis Tutorial"><link rel="up" href="scripting.html" title="Chapter3.Scripting and extending synopsis"><link rel="previous" href="pipeline.html" title="Composing a pipeline"><link rel="next" href="ch04.html" title="Chapter4.Processor design"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><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">Chapter3.Scripting and extending synopsis</th><td width="20%" align="right"><a accesskey="n" href="ch04.html">Next</a></td></tr></table></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="script"></a>Writing your own synopsis script</h2></div></div><div></div></div><p>The synopsis framework provides a function <tt class="function">process</tt>
that lets you declare and expose processors as commands so they can be
used per command line:
</p><pre class="programlisting">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 = '(-;'))
</pre><p>
</p><p>With such a script <tt class="filename">synopsis.py</tt> 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 href="using.html" title="Chapter2.Using the synopsis tool">Chapter2, <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"><div class="titlepage"><div><div><h3 class="title"><a name="importing"></a>Importing all desired processors</h3></div></div><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 <tt class="function">process</tt> function, together
with a number of predefined processors.
</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="composing"></a>Composing new processors</h3></div></div><div></div></div><p>As outlined in <a 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"><div class="titlepage"><div><div><h3 class="title"><a name="extending"></a>Defining new processors</h3></div></div><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 href="processor.html" title="The Processor class">the section called “The Processor class”</a>,
it has only to respect the semantics of the <tt class="function">process</tt>
method.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="process"></a>Exposing the commands</h3></div></div><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 <tt class="function">process</tt>
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 <tt class="function">process</tt> 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="ch04.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">Chapter4.Processor design</td></tr></table></div></body></html>
|