1#!/usr/bin/python
2
3import sys, time, xml.dom.minidom
4
5# templates
6
7MANUAL="SOPE Dynamic Element Reference"
8COPYRIGHT="SKYRIX Software AG"
9
10HEADER=""".TH %(element)s %(section)i "%(month)s %(year)s" "SOPE" "%(manual)s"
11.\\" DO NOT EDIT: this file got autogenerated using woapi2man from:
12.\\"   %(file)s
13.\\"
14.\\" Copyright (C) %(year)s %(copy)s. All rights reserved.
15.\\" ====================================================================
16.\\"
17.\\" Copyright (C) %(year)s %(copy)s. All rights reserved.
18.\\"
19.\\" Check the COPYING file for further information.
20.\\"
21.\\" Created with the help of:
22.\\"   http://www.schweikhardt.net/man_page_howto.html
23.\\"
24"""
25
26BUGS="""
27.SH BUGS
28SOPE related bugs are collected in the OpenGroupware.org Bugzilla:
29  http://bugzilla.opengroupware.org/
30"""
31
32AUTHOR="""
33.SH AUTHOR
34The SOPE community <developer at opengroupware.org>.
35"""
36
37SEEALSO="""
38.SH SEE ALSO
39.BR sope-ngobjweb-defaults
40"""
41
42FOOTER="\n"
43
44PASSTHROUGHTEXT="This binding is a pass-through binding.\n"
45
46# Note: texts may not start with a single quote: '
47DEFAULTSTEXT="Kind: %s\n"
48KINDTOTEXT={
49    'Page Names': "The value of '%(name)s' will be used to lookup a WOComponent page.\n",
50    'Actions':    "The '%(name)s' binding is evaluated as an action (a method which returns a WOComponent or other WOActionResults object).\n",
51    'Resources':  "The value of '%(name)s' refers to a resource which will be looked up using the WOResourceManager.\n",
52    'YES/NO':     "The value of '%(name)s' will be evaluated in a boolean context.\n",
53    'Frameworks': "The value of '%(name)s' must be the name of a framework or bundle to be used for resource lookups.\n",
54
55#    'Direct Actions': "The value of '%(name)s' refers to the name of a direct action method.\n",
56#    'Direct Action Classes': "The value of '%(name)s' refers to the name of a direct action class.\n"
57}
58
59# processing
60
61class DOMToManPage:
62
63    def __init__(self, _path, _dom):
64        self.path = _path
65        self.dom  = _dom
66        self.out  = sys.stdout
67
68    def clear(self):
69        self.path = None
70        self.dom.unlink()
71        self.dom = None
72
73    def printManPageHeader(self, woroot):
74        self.out.write(HEADER % {
75            'file':    self.path,
76            'element': woroot.getAttribute('class'),
77            'section': 3,
78            'month':   time.strftime("%B"),
79            'year':    time.strftime("%Y"),
80            'manual':  MANUAL,
81            'copy':    COPYRIGHT
82            })
83
84    def printManPageName(self, woroot):
85        self.out.write("\n.SH NAME\n%s\n" % woroot.getAttribute('class'))
86
87    def printManPageBugs(self, woroot):
88        self.out.write(BUGS)
89
90    def printManPageAuthor(self, woroot):
91        self.out.write(AUTHOR)
92
93    def printManPageSeeAlso(self, woroot):
94        self.out.write(SEEALSO)
95
96    def printManPageFooter(self, woroot):
97        self.out.write(FOOTER)
98
99    def printSynopsis(self, woroot):
100        self.out.write("\n.SH SYNOPSIS\n")
101        self.out.write(".B %s\n" % woroot.getAttribute('class'))
102        self.out.write("{")
103        for binding in woroot.getElementsByTagName("binding"):
104            self.out.write(" %s; " % binding.getAttribute('name'))
105        self.out.write("}\n")
106
107    def escapeText(self, text):
108        if text[0] == "'":
109            return "\\" + text
110        return text
111
112    def getTextForBindingKind(self, binding, kind):
113        if kind is None:   return
114        if len(kind) == 0: return
115        if KINDTOTEXT.has_key(kind):
116            info = { 'kind': kind,
117                     'name': binding.getAttribute('name') }
118            s = KINDTOTEXT[kind]
119            s = s % info
120            return s
121
122        return DEFAULTSTEXT % ( kind, )
123
124    def printBindings(self, woroot):
125        self.out.write("\n.SH BINDINGS\n")
126        for binding in woroot.getElementsByTagName("binding"):
127            self.out.write(".IP %s\n" % binding.getAttribute('name'))
128            s = binding.getAttribute('passthrough')
129            if (s and s == 'YES'):
130                self.out.write(PASSTHROUGHTEXT)
131            s = binding.getAttribute('defaults')
132            if (s and len(s) > 0):
133                self.out.write(self.getTextForBindingKind(binding, s))
134
135    def printValidation(self, woroot):
136        vals = woroot.getElementsByTagName("validation")
137        if (vals.length == 0): return
138        self.out.write("\n.SH VALIDATION\n")
139        for val in vals:
140            m = val.getAttribute('message')
141            if not m: continue
142            if len(m) == 0: continue
143            self.out.write("%s\n" % self.escapeText(m.capitalize()))
144
145    def processDOM(self):
146        woroot = self.dom.getElementsByTagName("wo")[0]
147        self.printManPageHeader(woroot)
148        self.printManPageName(woroot)
149
150        self.printSynopsis(woroot)
151        self.printValidation(woroot)
152        self.printBindings(woroot)
153
154        self.printManPageBugs(woroot)
155        self.printManPageAuthor(woroot)
156        self.printManPageSeeAlso(woroot)
157        self.printManPageFooter(woroot)
158
159    def processBinding(self, element):
160        print "  check binding", element.getAttribute('name')
161
162    def processValidation(self, element):
163        print "  check validation:", element.getAttribute('message')
164
165    def processWOTag(self, element):
166        print "check element class", element.getAttribute('class')
167
168        for binding in dom.getElementsByTagName("binding"):
169            self.processBinding(binding)
170        for subelem in dom.getElementsByTagName("validation"):
171            self.processValidation(subelem)
172
173#class DOMToManPage
174
175# main function
176
177path = sys.argv[1]
178try:
179  dom = xml.dom.minidom.parse(path)
180except IOError, e:
181        sys.stderr.write("%s:0: %s\n" % ( path, e ))
182except xml.parsers.expat.ExpatError, xmle:
183        sys.stderr.write("%s:%i: %s\n" % ( path, xmle.lineno, xmle ))
184if dom is None:
185    sys.exit(1)
186
187cpu = DOMToManPage(path, dom)
188cpu.processDOM()
189cpu.clear()
190