1#generate *.html pages from atlas.atlas
2
3#Copyright (C) 2000 Aloril
4#Copyright (C) 2002 by AIR-IX SUUNNITTELU/Ahiplan Oy
5
6#This library is free software; you can redistribute it and/or
7#modify it under the terms of the GNU Lesser General Public
8#License as published by the Free Software Foundation; either
9#version 2.1 of the License, or (at your option) any later version.
10
11#This library is distributed in the hope that it will be useful,
12#but WITHOUT ANY WARRANTY; without even the implied warranty of
13#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14#Lesser General Public License for more details.
15
16#You should have received a copy of the GNU Lesser General Public
17#License along with this library; if not, write to the Free Software
18#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20
21import init
22
23from atlas import *
24from atlas.typemap import *
25from htmlPARSER.parserlib import baseParser
26from atlas.transport.file import read_file_as_dict
27
28
29class atlas_spec_parser(baseParser):
30    def __init__(self, objects):
31        baseParser.__init__(self)
32        self.objects=objects
33
34    def text2html(self, text):
35        sl=[]
36        sl.append("    <PRE>")
37        text=string.replace(text,"&","&amp;")
38        sl.append(string.replace(text,"<","&lt;"))
39        sl.append("    </PRE>")
40        return string.join(sl,"\n")
41
42    def parse(self, input, output):
43        self.output=open(output,"w")
44        self.output.write('<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">\n')
45        baseParser.parse(self,input)
46        self.output.close()
47
48    def out(self,s):
49        """ custom output function (can be overridden)
50            output is only done when "no" is inside
51            self.outAllowed.
52        """
53        if not "no" in self.outAllowed.values():
54            self.output.write(s)
55
56    def do_obj(self,attrs):
57        for name,value in attrs:
58            if name=="id":
59                obj=self.objects.get(value)
60                if not obj:
61                    raise SyntaxError,"id "+value+" not defined!"
62                self.output_obj(obj)
63
64    def do_objindex(self, attrs):
65        for name,value in attrs:
66            if name=="id":
67                self.output_objindex([value])
68
69    def do_objtree(self, attrs):
70        for name,value in attrs:
71            if name=="id":
72                self.output_objtree([value])
73
74    def output_obj(self, obj):
75        lines=[]
76        add_line = lines.append
77        add_line('    <a name="%s"><h2>%s</h2></a>' % (obj.id, obj.id))
78        add_line('    Attribute list:')
79        add_line('    <ul>')
80        for (name, value) in obj.items(all=1):
81            s='      <li><b><a href="type.html#%s">%s</a> ' % (name, name)
82            attribute_def_obj = obj.attribute_definition(name)
83            if attribute_def_obj!=obj:
84                s=s+'(inherited from %s) ' % attribute_def_obj.id
85            if has_parent(name, "html", self.objects):
86                value='<br>\n'+value+'<br>\n'
87            elif name=="example":
88                value_list=["<br>"]
89                for example_obj in value:
90                    if get_atlas_type(example_obj)=="string":
91                        value_list.append(example_obj)
92                    else:
93                        bach=str(example_obj)
94                        value_list.append("<pre>")
95                        value_list.append(self.text2html(bach))
96                        value_list.append("</pre>")
97                value_list.append("<br>")
98                value=string.join(value_list,"\n")
99            elif get_atlas_type(value)=="map":
100                value = `value`
101            s=s+'(encoding:<a href="type.html#%s">%s</a>):</b> Value: <font color="green">%s</font> ' % \
102               (get_atlas_type(value), get_atlas_type(value), value)
103            s = s + self.objects[name].description
104            add_line(s)
105        add_line('    </ul>')
106        add_line('')
107        self.out(string.join(lines,"\n"))
108        self.out("    Bach version:<br>\n")
109        bach=str(obj)
110        self.out(self.text2html(bach))
111
112    def output_objindex(self, id_list, indent='    '):
113        self.out(indent+"<ul>\n")
114        for id in id_list:
115            obj=self.objects.get(id)
116            if not obj:
117                raise SyntaxError,'id "'+id+'" not defined!'
118            desc = obj.description
119            if desc:
120                if not obj.has_key('description'):
121                    desc='-"-'
122            spec=obj.specification
123            if spec=="example":
124                desc='<font color="green">' + desc + '</font>'
125            self.out(indent+'  <li><a href="#%s">%s</a>: %s\n' % (id,id,desc))
126            children = obj.get_plain_attribute("children")
127            if children:
128                self.output_objindex(children, indent+'    ')
129        self.out(indent+"</ul>\n")
130    def output_objtree(self, id_list):
131        for id in id_list:
132            obj=self.objects.get(id)
133            if not obj:
134                raise SyntaxError,"id "+id+" not defined!"
135            self.output_obj(obj)
136            children = obj.get_plain_attribute("children")
137            if children:
138                self.output_objtree(children)
139
140
141if __name__=="__main__":
142    filelist=["root","entity","operation","type","interface"]
143    objects = read_file_as_dict("atlas.atlas")
144    find_parents_children_objects(objects)
145    p=atlas_spec_parser(objects)
146    for file in filelist:
147        p.parse(file+"_in.html",file+".html")
148