annotate src/bridge/bridge_constructor/bridge_contructor.py @ 363:54c77a56b22f

bridge: bridge_constuctor's frontend template update with quiz answer method
author Goffi <goffi@goffi.org>
date Sun, 12 Jun 2011 23:43:36 +0200
parents eb9d33ba4e36
children 3ea41a199b36
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
2 #-*- coding: utf-8 -*-
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
3
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
5 SAT: a jabber client
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
6 Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
7
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
12
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU General Public License for more details.
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
17
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
21
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
22
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
23 #consts
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
24 NAME = u"bridge_constructor"
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
25 VERSION="0.1.0"
271
0288f97334f2 bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents: 267
diff changeset
26 DEST_DIR="generated"
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
27 ABOUT = NAME+u""" v%s (c) Jérôme Poisson (aka Goffi) 2011
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
28
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
29 ---
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
30 """+NAME+u""" Copyright (C) 2011 Jérôme Poisson (aka Goffi)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
31 This program comes with ABSOLUTELY NO WARRANTY;
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
32 This is free software, and you are welcome to redistribute it
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
33 under certain conditions.
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
34 ---
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
35
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
36 This script construct a SàT bridge using the given protocol
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
37 """
273
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
38 MANAGED_PROTOCOLES=['dbus','mediawiki']
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
39 DEFAULT_PROTOCOLE='dbus'
337
4402ac630712 bridge: async callback managed in bridge_constructor + misc
Goffi <goffi@goffi.org>
parents: 276
diff changeset
40 FLAGS=['deprecated', 'async']
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
41
359
eb9d33ba4e36 bridge: templates' constants can now be overrided
Goffi <goffi@goffi.org>
parents: 337
diff changeset
42 ENV_OVERRIDE = "SAT_BRIDGE_CONST_" #Prefix used to override a constant
eb9d33ba4e36 bridge: templates' constants can now be overrided
Goffi <goffi@goffi.org>
parents: 337
diff changeset
43
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
44 import sys
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
45 import os
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
46 from os import path
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
47 from optparse import OptionParser
266
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
48 from ConfigParser import SafeConfigParser as Parser
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
49 from ConfigParser import NoOptionError
266
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
50 import re
273
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
51 from datetime import datetime
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
52
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
53
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
54 class ParseError(Exception):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
55 #Used when the signature parsing is going wrong (invalid signature ?)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
56 pass
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
57
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
58 class Constructor:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
59
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
60 def __init__(self, bridge_template, options):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
61 self.bridge_template = bridge_template
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
62 self.options = options
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
63
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
64 def getValues(self, name):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
65 """Return values of a function in a dict
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
66 @param name: Name of the function to get
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
67 @return: dict, each key has the config value or None if the value is not set"""
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
68 function={}
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
69 for option in ['type','category','sig_in','sig_out','doc']:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
70 try:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
71 value = self.bridge_template.get(name, option)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
72 except NoOptionError:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
73 value = None
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
74 function[option] = value
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
75 return function
266
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
76
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
77 def getDefault(self, name):
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
78 """Return default values of a function in a dict
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
79 @param name: Name of the function to get
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
80 @return: dict, each key is the integer param number (no key if no default value)"""
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
81 default_dict={}
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
82 def_re = re.compile(r"param_(\d+)_default")
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
83
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
84 for option in self.bridge_template.options(name):
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
85 match = def_re.match(option)
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
86 if match:
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
87 try:
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
88 idx = int(match.group(1))
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
89 except ValueError:
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
90 raise ParseError("Invalid value [%s] for parameter number" % match.group(1))
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
91 default_dict[idx] = self.bridge_template.get(name, option)
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
92
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
93 return default_dict
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
94
273
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
95 def getFlags(self, name):
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
96 """Return list of flags set for this function
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
97 @param name: Name of the function to get
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
98 @return: List of flags (string)"""
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
99 flags=[]
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
100 for option in self.bridge_template.options(name):
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
101 if option in FLAGS:
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
102 flags.append(option)
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
103 return flags
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
104
266
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
105 def getArgumentsDoc(self, name):
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
106 """Return documentation of arguments
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
107 @param name: Name of the function to get
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
108 @return: dict, each key is the integer param number (no key if no argument doc), value is a tuple (name, doc)"""
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
109 doc_dict={}
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
110 option_re = re.compile(r"doc_param_(\d+)")
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
111 value_re = re.compile(r"^(\w+): (.*)$", re.MULTILINE | re.DOTALL)
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
112 for option in self.bridge_template.options(name):
273
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
113 if option == 'doc_return':
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
114 doc_dict['return'] = self.bridge_template.get(name, option)
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
115 continue
266
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
116 match = option_re.match(option)
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
117 if match:
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
118 try:
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
119 idx = int(match.group(1))
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
120 except ValueError:
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
121 raise ParseError("Invalid value [%s] for parameter number" % match.group(1))
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
122 value_match = value_re.match(self.bridge_template.get(name, option))
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
123 if not value_match:
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
124 raise ParseError("Invalid value for parameter doc [%i]" % idx)
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
125 doc_dict[idx]=(value_match.group(1),value_match.group(2))
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
126 return doc_dict
273
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
127
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
128 def getDoc(self, name):
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
129 """Return documentation of the method
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
130 @param name: Name of the function to get
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
131 @return: string documentation, or None"""
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
132 if self.bridge_template.has_option(name, "doc"):
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
133 return self.bridge_template.get(name, "doc")
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
134 return None
266
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
135
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
136 def getArguments(self, signature, name=None, default=None, unicode_protect=False):
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
137 """Return arguments to user given a signature
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
138 @param signature: signature in the short form (using s,a,i,b etc)
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
139 @param name: dictionary of arguments name like given by getArguments
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
140 @param default: dictionary of default values, like given by getDefault
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
141 @param unicode_protect: activate unicode protection on strings (return strings as unicode(str))
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
142 @return: list of arguments that correspond to a signature (e.g.: "sss" return "arg1, arg2, arg3")"""
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
143 i=0
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
144 idx=0
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
145 attr_string=[]
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
146 while i<len(signature):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
147 if signature[i] not in ['b','y','n','i','x','q','u','t','d','s','a']:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
148 raise ParseError("Unmanaged attribute type [%c]" % signature[i])
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
149
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
150 attr_string.append(("unicode(%(name)s)%(default)s" if (unicode_protect and signature[i]=='s') else "%(name)s%(default)s") % {
266
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
151 'name':name[idx][0] if (name and name.has_key(idx)) else "arg_%i" % idx,
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
152 'default':"="+default[idx] if (default and default.has_key(idx)) else ''
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
153 }) #give arg_1, arg2, etc or name1, name2=default, etc. \
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
154 #give unicode(arg_1), unicode(arg_2), etc. if unicode_protect is set and arg is a string
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
155 idx+=1
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
156
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
157 if signature[i] == 'a':
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
158 i+=1
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
159 if signature[i]!='{' and signature[i]!='(': #FIXME: must manage tuples out of arrays
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
160 i+=1
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
161 continue #we have a simple type for the array
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
162 opening_car = signature[i]
266
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
163 assert(opening_car in ['{','('])
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
164 closing_car = '}' if opening_car == '{' else ')'
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
165 opening_count = 1
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
166 while (True): #we have a dict or a list of tuples
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
167 i+=1
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
168 if i>=len(signature):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
169 raise ParseError("missing }")
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
170 if signature[i] == opening_car:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
171 opening_count+=1
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
172 if signature[i] == closing_car:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
173 opening_count-=1
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
174 if opening_count == 0:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
175 break
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
176 i+=1
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
177 return ", ".join(attr_string)
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
178
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
179 def generateCoreSide(self):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
180 """create the constructor in SàT core side (backend)"""
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
181 raise NotImplementedError
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
182
271
0288f97334f2 bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents: 267
diff changeset
183 def generateFrontendSide(self):
0288f97334f2 bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents: 267
diff changeset
184 """create the constructor in SàT frontend side"""
0288f97334f2 bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents: 267
diff changeset
185 raise NotImplementedError
0288f97334f2 bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents: 267
diff changeset
186
0288f97334f2 bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents: 267
diff changeset
187 def finalWrite(self, filename, file_buf):
0288f97334f2 bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents: 267
diff changeset
188 """Write the final generated file in DEST_DIR/filename
0288f97334f2 bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents: 267
diff changeset
189 @param filename: name of the file to generate
0288f97334f2 bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents: 267
diff changeset
190 @param file_buf: list of lines (stings) of the file"""
0288f97334f2 bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents: 267
diff changeset
191 if os.path.exists(DEST_DIR) and not os.path.isdir(DEST_DIR):
0288f97334f2 bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents: 267
diff changeset
192 print ("The destination dir [%s] can't be created: a file with this name already exists !")
0288f97334f2 bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents: 267
diff changeset
193 sys.exit(1)
0288f97334f2 bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents: 267
diff changeset
194 try:
0288f97334f2 bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents: 267
diff changeset
195 if not os.path.exists(DEST_DIR):
0288f97334f2 bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents: 267
diff changeset
196 os.mkdir(DEST_DIR)
0288f97334f2 bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents: 267
diff changeset
197 full_path=os.path.join(DEST_DIR,filename)
0288f97334f2 bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents: 267
diff changeset
198 if os.path.exists(full_path) and not self.options.force:
0288f97334f2 bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents: 267
diff changeset
199 print ("The destination file [%s] already exists ! Use --force to overwrite it" % full_path)
0288f97334f2 bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents: 267
diff changeset
200 try:
0288f97334f2 bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents: 267
diff changeset
201 with open(full_path,'w') as dest_file:
0288f97334f2 bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents: 267
diff changeset
202 dest_file.write('\n'.join(file_buf))
0288f97334f2 bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents: 267
diff changeset
203 except IOError:
0288f97334f2 bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents: 267
diff changeset
204 print ("Can't open destination file [%s]" % full_path)
0288f97334f2 bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents: 267
diff changeset
205 except OSError:
0288f97334f2 bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents: 267
diff changeset
206 print("It's not possible to generate the file, check your permissions")
0288f97334f2 bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents: 267
diff changeset
207 exit(1)
0288f97334f2 bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents: 267
diff changeset
208
273
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
209 class MediawikiConstructor(Constructor):
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
210
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
211 def __init__(self, bridge_template, options):
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
212 Constructor.__init__(self, bridge_template, options)
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
213 self.core_template="mediawiki_template.tpl"
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
214 self.core_dest="mediawiki.wiki"
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
215
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
216 def _wikiParameter(self, name, sig_in):
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
217 """Format parameters with the wiki syntax
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
218 @param name: name of the function
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
219 @param sig_in: signature in
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
220 @return: string of the formated parameters"""
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
221 arg_doc = self.getArgumentsDoc(name)
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
222 arg_default = self.getDefault(name)
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
223 args_str = self.getArguments(sig_in)
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
224 args = args_str.split(', ') if args_str else [] #ugly but it works :)
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
225 wiki=[]
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
226 for i in range(len(args)):
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
227 if arg_doc.has_key(i):
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
228 name,doc=arg_doc[i]
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
229 doc='\n:'.join(doc.rstrip('\n').split('\n'))
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
230 wiki.append("; %s: %s" % (name, doc))
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
231 else:
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
232 wiki.append("; arg_%d: " % i)
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
233 if arg_default.has_key(i):
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
234 wiki.append(":''DEFAULT: %s''" % arg_default[i])
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
235 return "\n".join(wiki)
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
236
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
237 def _wikiReturn(self, name):
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
238 """Format return doc with the wiki syntax
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
239 @param name: name of the function
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
240 """
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
241 arg_doc = self.getArgumentsDoc(name)
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
242 wiki=[]
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
243 if arg_doc.has_key('return'):
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
244 wiki.append('\n|-\n! scope=row | return value\n|')
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
245 wiki.append('<br />\n'.join(arg_doc['return'].rstrip('\n').split('\n')))
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
246 return "\n".join(wiki)
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
247
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
248 def generateCoreSide(self):
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
249 signals_part = []
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
250 methods_part = []
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
251 sections = self.bridge_template.sections()
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
252 sections.sort()
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
253 for section in sections:
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
254 function = self.getValues(section)
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
255 print ("Adding %s %s" % (section, function["type"]))
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
256 default = self.getDefault(section)
337
4402ac630712 bridge: async callback managed in bridge_constructor + misc
Goffi <goffi@goffi.org>
parents: 276
diff changeset
257 async_msg = """<br />'''This method is asynchrone'''"""
273
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
258 deprecated_msg = """<br />'''<font color="#FF0000">/!\ WARNING /!\ : This method is deprecated, please don't use it !</font>'''"""
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
259 signature_signal = \
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
260 """\
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
261 ! scope=row | signature
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
262 | %s
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
263 |-\
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
264 """ % function['sig_in']
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
265 signature_method = \
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
266 """\
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
267 ! scope=row | signature in
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
268 | %s
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
269 |-
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
270 ! scope=row | signature out
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
271 | %s
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
272 |-\
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
273 """ % (function['sig_in'], function['sig_out'])
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
274 completion = {
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
275 'signature':signature_signal if function['type']=="signal" else signature_method,
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
276 'sig_out':function['sig_out'] or '',
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
277 'category':function['category'],
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
278 'name':section,
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
279 'doc':self.getDoc(section) or "FIXME: No description available",
337
4402ac630712 bridge: async callback managed in bridge_constructor + misc
Goffi <goffi@goffi.org>
parents: 276
diff changeset
280 'async':async_msg if "async" in self.getFlags(section) else "",
273
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
281 'deprecated':deprecated_msg if "deprecated" in self.getFlags(section) else "",
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
282 'parameters':self._wikiParameter(section, function['sig_in']),
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
283 'return':self._wikiReturn(section) if function['type'] == 'method' else ''
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
284 }
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
285
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
286 dest = signals_part if function['type'] == "signal" else methods_part
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
287 dest.append("""\
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
288 == %(name)s ==
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
289 ''%(doc)s''
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
290 %(deprecated)s
337
4402ac630712 bridge: async callback managed in bridge_constructor + misc
Goffi <goffi@goffi.org>
parents: 276
diff changeset
291 %(async)s
273
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
292 {| class="wikitable" style="text-align:left; width:80%%;"
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
293 ! scope=row | category
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
294 | %(category)s
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
295 |-
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
296 %(signature)s
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
297 ! scope=row | parameters
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
298 |
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
299 %(parameters)s%(return)s
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
300 |}
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
301 """ % completion)
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
302
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
303 #at this point, signals_part, and methods_part should be filled,
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
304 #we just have to place them in the right part of the template
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
305 core_bridge = []
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
306 try:
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
307 with open(self.core_template) as core_template:
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
308 for line in core_template:
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
309 if line.startswith('##SIGNALS_PART##'):
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
310 core_bridge.extend(signals_part)
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
311 elif line.startswith('##METHODS_PART##'):
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
312 core_bridge.extend(methods_part)
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
313 elif line.startswith('##TIMESTAMP##'):
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
314 core_bridge.append('Generated on %s' % datetime.now())
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
315 else:
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
316 core_bridge.append(line.replace('\n',''))
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
317 except IOError:
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
318 print ("Can't open template file [%s]" % self.core_template)
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
319 sys.exit(1)
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
320
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
321 #now we write to final file
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
322 self.finalWrite(self.core_dest, core_bridge)
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
323
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
324 class DbusConstructor(Constructor):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
325
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
326 def __init__(self, bridge_template, options):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
327 Constructor.__init__(self, bridge_template, options)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
328 self.core_template="dbus_core_template.py"
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
329 self.frontend_template="dbus_frontend_template.py"
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
330 self.frontend_dest = self.core_dest="DBus.py"
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
331
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
332 def generateCoreSide(self):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
333 signals_part = []
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
334 methods_part = []
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
335 direct_calls = []
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
336 sections = self.bridge_template.sections()
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
337 sections.sort()
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
338 for section in sections:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
339 function = self.getValues(section)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
340 print ("Adding %s %s" % (section, function["type"]))
266
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
341 default = self.getDefault(section)
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
342 arg_doc = self.getArgumentsDoc(section)
337
4402ac630712 bridge: async callback managed in bridge_constructor + misc
Goffi <goffi@goffi.org>
parents: 276
diff changeset
343 async = "async" in self.getFlags(section)
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
344 completion = {
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
345 'sig_in':function['sig_in'] or '',
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
346 'sig_out':function['sig_out'] or '',
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
347 'category':'REQ' if function['category'] == 'request' else 'COMM',
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
348 'name':section,
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
349 'args':self.getArguments(function['sig_in'], name=arg_doc, default=default )
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
350 }
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
351
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
352 if function["type"] == "signal":
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
353 completion['body'] = "pass" if not self.options.debug else 'debug ("%s")' % section
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
354 signals_part.append("""\
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
355 @dbus.service.signal(const_INT_PREFIX+const_%(category)s_SUFFIX,
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
356 signature='%(sig_in)s')
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
357 def %(name)s(self, %(args)s):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
358 %(body)s
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
359 """ % completion)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
360 direct_calls.append("""\
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
361 def %(name)s(self, %(args)s):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
362 self.dbus_bridge.%(name)s(%(args)s)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
363 """ % completion)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
364
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
365 elif function["type"] == "method":
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
366 completion['debug'] = "" if not self.options.debug else 'debug ("%s")\n%s' % (section,8*' ')
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
367 completion['args_result'] = self.getArguments(function['sig_in'], name=arg_doc, unicode_protect=self.options.unicode)
337
4402ac630712 bridge: async callback managed in bridge_constructor + misc
Goffi <goffi@goffi.org>
parents: 276
diff changeset
368 completion['async_comma'] = ', ' if async and function['sig_in'] else ''
4402ac630712 bridge: async callback managed in bridge_constructor + misc
Goffi <goffi@goffi.org>
parents: 276
diff changeset
369 completion['async_args_def'] = 'callback=None, errback=None' if async else ''
4402ac630712 bridge: async callback managed in bridge_constructor + misc
Goffi <goffi@goffi.org>
parents: 276
diff changeset
370 completion['async_args_call'] = 'callback, errback' if async else ''
4402ac630712 bridge: async callback managed in bridge_constructor + misc
Goffi <goffi@goffi.org>
parents: 276
diff changeset
371 completion['async_callbacks'] = "('callback', 'errback')" if async else "None"
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
372 methods_part.append("""\
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
373 @dbus.service.method(const_INT_PREFIX+const_%(category)s_SUFFIX,
337
4402ac630712 bridge: async callback managed in bridge_constructor + misc
Goffi <goffi@goffi.org>
parents: 276
diff changeset
374 in_signature='%(sig_in)s', out_signature='%(sig_out)s',
4402ac630712 bridge: async callback managed in bridge_constructor + misc
Goffi <goffi@goffi.org>
parents: 276
diff changeset
375 async_callbacks=%(async_callbacks)s)
4402ac630712 bridge: async callback managed in bridge_constructor + misc
Goffi <goffi@goffi.org>
parents: 276
diff changeset
376 def %(name)s(self, %(args)s%(async_comma)s%(async_args_def)s):
4402ac630712 bridge: async callback managed in bridge_constructor + misc
Goffi <goffi@goffi.org>
parents: 276
diff changeset
377 %(debug)sreturn self.cb["%(name)s"](%(args_result)s%(async_comma)s%(async_args_call)s)
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
378 """ % completion)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
379
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
380 #at this point, signals_part, methods_part and direct_calls should be filled,
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
381 #we just have to place them in the right part of the template
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
382 core_bridge = []
359
eb9d33ba4e36 bridge: templates' constants can now be overrided
Goffi <goffi@goffi.org>
parents: 337
diff changeset
383 const_override_pref = filter(lambda env: env.startswith(ENV_OVERRIDE), os.environ)
eb9d33ba4e36 bridge: templates' constants can now be overrided
Goffi <goffi@goffi.org>
parents: 337
diff changeset
384 const_override = [env[len(ENV_OVERRIDE):] for env in const_override_pref]
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
385 try:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
386 with open(self.core_template) as core_template:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
387 for line in core_template:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
388 if line.startswith('##SIGNALS_PART##'):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
389 core_bridge.extend(signals_part)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
390 elif line.startswith('##METHODS_PART##'):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
391 core_bridge.extend(methods_part)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
392 elif line.startswith('##DIRECT_CALLS##'):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
393 core_bridge.extend(direct_calls)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
394 else:
359
eb9d33ba4e36 bridge: templates' constants can now be overrided
Goffi <goffi@goffi.org>
parents: 337
diff changeset
395 if line.startswith('const_'):
eb9d33ba4e36 bridge: templates' constants can now be overrided
Goffi <goffi@goffi.org>
parents: 337
diff changeset
396 const_name = line[len('const_'):line.find(' = ')]
eb9d33ba4e36 bridge: templates' constants can now be overrided
Goffi <goffi@goffi.org>
parents: 337
diff changeset
397 if const_name in const_override:
eb9d33ba4e36 bridge: templates' constants can now be overrided
Goffi <goffi@goffi.org>
parents: 337
diff changeset
398 print ("const %s overriden" % const_name)
eb9d33ba4e36 bridge: templates' constants can now be overrided
Goffi <goffi@goffi.org>
parents: 337
diff changeset
399 core_bridge.append('const_%s = %s' % (const_name, os.environ[ENV_OVERRIDE+const_name]))
eb9d33ba4e36 bridge: templates' constants can now be overrided
Goffi <goffi@goffi.org>
parents: 337
diff changeset
400 continue
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
401 core_bridge.append(line.replace('\n',''))
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
402 except IOError:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
403 print ("Can't open template file [%s]" % self.core_template)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
404 sys.exit(1)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
405
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
406 #now we write to final file
271
0288f97334f2 bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents: 267
diff changeset
407 self.finalWrite(self.core_dest, core_bridge)
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
408
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
409 def generateFrontendSide(self):
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
410 methods_part = []
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
411 sections = self.bridge_template.sections()
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
412 sections.sort()
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
413 for section in sections:
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
414 function = self.getValues(section)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
415 print ("Adding %s %s" % (section, function["type"]))
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
416 default = self.getDefault(section)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
417 arg_doc = self.getArgumentsDoc(section)
337
4402ac630712 bridge: async callback managed in bridge_constructor + misc
Goffi <goffi@goffi.org>
parents: 276
diff changeset
418 async = "async" in self.getFlags(section)
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
419 completion = {
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
420 'sig_in':function['sig_in'] or '',
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
421 'sig_out':function['sig_out'] or '',
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
422 'category':'req' if function['category'] == 'request' else 'comm',
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
423 'name':section,
337
4402ac630712 bridge: async callback managed in bridge_constructor + misc
Goffi <goffi@goffi.org>
parents: 276
diff changeset
424 'args':self.getArguments(function['sig_in'], name=arg_doc, default=default)
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
425 }
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
426
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
427 if function["type"] == "method":
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
428 completion['debug'] = "" if not self.options.debug else 'debug ("%s")\n%s' % (section,8*' ')
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
429 completion['args_result'] = self.getArguments(function['sig_in'], name=arg_doc)
337
4402ac630712 bridge: async callback managed in bridge_constructor + misc
Goffi <goffi@goffi.org>
parents: 276
diff changeset
430 completion['async_args'] = ', callback=None, errback=None' if async else ''
4402ac630712 bridge: async callback managed in bridge_constructor + misc
Goffi <goffi@goffi.org>
parents: 276
diff changeset
431 completion['async_comma'] = ', ' if async and function['sig_in'] else ''
4402ac630712 bridge: async callback managed in bridge_constructor + misc
Goffi <goffi@goffi.org>
parents: 276
diff changeset
432 completion['async_args_result'] = 'reply_handler=callback, error_handler=errback' if async else ''
4402ac630712 bridge: async callback managed in bridge_constructor + misc
Goffi <goffi@goffi.org>
parents: 276
diff changeset
433 result = "self.db_%(category)s_iface.%(name)s(%(args_result)s%(async_comma)s%(async_args_result)s)" % completion
276
a00e87d48213 bridge, bridge constructor: fixed mix stuff
Goffi <goffi@goffi.org>
parents: 273
diff changeset
434 completion['result'] = ("unicode(%s)" if self.options.unicode and function['sig_out'] == 's' else "%s") % result
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
435 methods_part.append("""\
337
4402ac630712 bridge: async callback managed in bridge_constructor + misc
Goffi <goffi@goffi.org>
parents: 276
diff changeset
436 def %(name)s(self, %(args)s%(async_args)s):
276
a00e87d48213 bridge, bridge constructor: fixed mix stuff
Goffi <goffi@goffi.org>
parents: 273
diff changeset
437 %(debug)sreturn %(result)s
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
438 """ % completion)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
439
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
440 #at this point, methods_part should be filled,
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
441 #we just have to place it in the right part of the template
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
442 frontend_bridge = []
359
eb9d33ba4e36 bridge: templates' constants can now be overrided
Goffi <goffi@goffi.org>
parents: 337
diff changeset
443 const_override_pref = filter(lambda env: env.startswith(ENV_OVERRIDE), os.environ)
eb9d33ba4e36 bridge: templates' constants can now be overrided
Goffi <goffi@goffi.org>
parents: 337
diff changeset
444 const_override = [env[len(ENV_OVERRIDE):] for env in const_override_pref]
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
445 try:
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
446 with open(self.frontend_template) as frontend_template:
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
447 for line in frontend_template:
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
448 if line.startswith('##METHODS_PART##'):
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
449 frontend_bridge.extend(methods_part)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
450 else:
359
eb9d33ba4e36 bridge: templates' constants can now be overrided
Goffi <goffi@goffi.org>
parents: 337
diff changeset
451 if line.startswith('const_'):
eb9d33ba4e36 bridge: templates' constants can now be overrided
Goffi <goffi@goffi.org>
parents: 337
diff changeset
452 const_name = line[len('const_'):line.find(' = ')]
eb9d33ba4e36 bridge: templates' constants can now be overrided
Goffi <goffi@goffi.org>
parents: 337
diff changeset
453 if const_name in const_override:
eb9d33ba4e36 bridge: templates' constants can now be overrided
Goffi <goffi@goffi.org>
parents: 337
diff changeset
454 print ("const %s overriden" % const_name)
eb9d33ba4e36 bridge: templates' constants can now be overrided
Goffi <goffi@goffi.org>
parents: 337
diff changeset
455 frontend_bridge.append('const_%s = %s' % (const_name, os.environ[ENV_OVERRIDE+const_name]))
eb9d33ba4e36 bridge: templates' constants can now be overrided
Goffi <goffi@goffi.org>
parents: 337
diff changeset
456 continue
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
457 frontend_bridge.append(line.replace('\n',''))
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
458 except IOError:
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
459 print ("Can't open template file [%s]" % self.frontend_template)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
460 sys.exit(1)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
461
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
462 #now we write to final file
271
0288f97334f2 bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents: 267
diff changeset
463 self.finalWrite(self.frontend_dest, frontend_bridge)
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
464
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
465
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
466 class ConstructorError(Exception):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
467 pass
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
468
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
469 class ConstructorFactory:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
470 def create(self, bridge_template, options):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
471 if options.protocole=='dbus':
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
472 return DbusConstructor(bridge_template, options)
273
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
473 elif options.protocole=='mediawiki':
3b5e856b3a32 Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
Goffi <goffi@goffi.org>
parents: 271
diff changeset
474 return MediawikiConstructor(bridge_template, options)
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
475
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
476 raise ConstructorError('Unknown constructor type')
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
477
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
478 class BridgeConstructor:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
479 def __init__(self):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
480 self.options = None
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
481
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
482 def check_options(self):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
483 """Check command line options"""
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
484 _usage="""
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
485 %prog [options]
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
486
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
487 %prog --help for options list
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
488 """
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
489 parser = OptionParser(usage=_usage,version=ABOUT % VERSION)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
490
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
491 parser.add_option("-p", "--protocole", action="store", type="string", default=DEFAULT_PROTOCOLE,
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
492 help="Generate bridge using PROTOCOLE (default: %s, possible values: [%s])" % (DEFAULT_PROTOCOLE, ", ".join(MANAGED_PROTOCOLES)))
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
493 parser.add_option("-s", "--side", action="store", type="string", default="core",
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
494 help="Which side of the bridge do you want to make ? (default: %default, possible values: [core, frontend])")
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
495 parser.add_option("-t", "--template", action="store", type="string", default='bridge_template.ini',
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
496 help="Use TEMPLATE to generate bridge (default: %default)")
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
497 parser.add_option("-f", "--force", action="store_true", default=False,
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
498 help=("Force overwritting of existing files"))
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
499 parser.add_option("-d", "--debug", action="store_true", default=False,
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
500 help=("Add debug information printing"))
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
501 parser.add_option("--no_unicode", action="store_false", dest="unicode", default=True,
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
502 help=("Remove unicode type protection from string results"))
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
503
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
504
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
505 (self.options, args) = parser.parse_args()
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
506 return args
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
507
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
508 def go(self):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
509 self.check_options()
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
510 self.template = Parser()
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
511 try:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
512 self.template.readfp(open(self.options.template))
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
513 except IOError:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
514 print ("The template file doesn't exist or is not accessible")
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
515 exit(1)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
516 constructor = ConstructorFactory().create(self.template, self.options)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
517 if self.options.side == "core":
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
518 constructor.generateCoreSide()
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
519 elif self.options.side == "frontend":
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
520 constructor.generateFrontendSide()
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
521
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
522 if __name__ == "__main__":
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
523 bc = BridgeConstructor()
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
524 bc.go()