annotate src/bridge/bridge_contructor.py @ 266:c4b84a2d2ad1

bridge: constructor and template improved, documentation added
author Goffi <goffi@goffi.org>
date Mon, 24 Jan 2011 17:47:45 +0100
parents b5f1f3dc9ac6
children
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"
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
26 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
27
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 """+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
30 This program comes with ABSOLUTELY NO WARRANTY;
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
31 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
32 under certain conditions.
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
33 ---
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 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
36 """
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
37 MANAGED_PROTOCOLES=['dbus']
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
38 DEFAULT_PROTOCOLE='dbus'
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
39
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
40 import sys
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
41 import os
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
42 from os import path
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
43 from optparse import OptionParser
266
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
44 from ConfigParser import SafeConfigParser as Parser
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
45 from ConfigParser import NoOptionError
266
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
46 import re
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
47
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
48
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
49 class ParseError(Exception):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
50 #Used when the signature parsing is going wrong (invalid signature ?)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
51 pass
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 class Constructor:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
54
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
55 def __init__(self, bridge_template, options):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
56 self.bridge_template = bridge_template
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
57 self.options = options
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
58
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
59 def getValues(self, name):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
60 """Return values of a function in a dict
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
61 @param name: Name of the function to get
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
62 @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
63 function={}
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
64 for option in ['type','category','sig_in','sig_out','doc']:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
65 try:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
66 value = self.bridge_template.get(name, option)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
67 except NoOptionError:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
68 value = None
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
69 function[option] = value
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
70 return function
266
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
71
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
72 def getDefault(self, name):
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
73 """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
74 @param name: Name of the function to get
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
75 @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
76 default_dict={}
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
77 def_re = re.compile(r"param_(\d+)_default")
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
78
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
79 for option in self.bridge_template.options(name):
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
80 if option == 'doc_return':
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
81 default_dict['return'] = self.bridge_template.get(name, option)
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
82 continue
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
83 match = def_re.match(option)
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
84 if match:
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
85 try:
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
86 idx = int(match.group(1))
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
87 except ValueError:
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
88 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
89 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
90
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
91 return default_dict
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
92
266
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
93 def getArgumentsDoc(self, name):
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
94 """Return documentation of arguments
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
95 @param name: Name of the function to get
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
96 @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
97 doc_dict={}
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
98 option_re = re.compile(r"doc_param_(\d+)")
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
99 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
100 for option in self.bridge_template.options(name):
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
101 match = option_re.match(option)
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
102 if match:
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
103 try:
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
104 idx = int(match.group(1))
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
105 except ValueError:
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
106 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
107 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
108 if not value_match:
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
109 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
110 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
111 return doc_dict
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
112
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
113 def getArguments(self, signature, name=None, default=None):
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
114 """Return arguments to user given a signature
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
115 @param signature: signature in the short form (using s,a,i,b etc)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
116 @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
117 i=0
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
118 idx=0
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
119 attr_string=""
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
120 while i<len(signature):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
121 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
122 raise ParseError("Unmanaged attribute type [%c]" % signature[i])
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
123
266
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
124 attr_string += ("" if idx==0 else ", ") + ("%(name)s%(default)s" % {
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
125 '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
126 'default':"="+default[idx] if (default and default.has_key(idx)) else ''
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
127 }) #give arg_1, arg2, etc or name1, name2=default, etc
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
128 idx+=1
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
129
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
130 if signature[i] == 'a':
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
131 i+=1
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
132 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
133 i+=1
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
134 continue #we have a simple type for the array
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
135 opening_car = signature[i]
266
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
136 assert(opening_car in ['{','('])
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
137 closing_car = '}' if opening_car == '{' else ')'
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
138 opening_count = 1
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
139 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
140 i+=1
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
141 if i>=len(signature):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
142 raise ParseError("missing }")
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
143 if signature[i] == opening_car:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
144 opening_count+=1
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
145 if signature[i] == closing_car:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
146 opening_count-=1
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
147 if opening_count == 0:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
148 break
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
149 i+=1
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
150 return attr_string
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
151
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
152 def generateCoreSide(self):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
153 """create the constructor in SàT core side (backend)"""
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
154 raise NotImplementedError
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
155
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
156 class DbusConstructor(Constructor):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
157
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
158 def __init__(self, bridge_template, options):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
159 Constructor.__init__(self, bridge_template, options)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
160 self.core_template="dbus_core_template.py"
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
161 self.core_dest="DBus.py"
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
162
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
163 def generateCoreSide(self):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
164 signals_part = []
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
165 methods_part = []
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
166 direct_calls = []
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
167 sections = self.bridge_template.sections()
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
168 sections.sort()
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
169 for section in sections:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
170 function = self.getValues(section)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
171 print ("Adding %s %s" % (section, function["type"]))
266
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
172 default = self.getDefault(section)
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
173 arg_doc = self.getArgumentsDoc(section)
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
174 completion = {
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
175 'sig_in':function['sig_in'] or '',
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
176 'sig_out':function['sig_out'] or '',
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
177 'category':'REQ' if function['category'] == 'request' else 'COMM',
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
178 'name':section,
266
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
179 '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
180 }
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
181
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
182 if function["type"] == "signal":
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
183 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
184 signals_part.append("""\
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
185 @dbus.service.signal(const_INT_PREFIX+const_%(category)s_SUFFIX,
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
186 signature='%(sig_in)s')
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
187 def %(name)s(self, %(args)s):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
188 %(body)s
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
189 """ % completion)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
190 direct_calls.append("""\
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
191 def %(name)s(self, %(args)s):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
192 self.dbus_bridge.%(name)s(%(args)s)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
193 """ % completion)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
194
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
195 elif function["type"] == "method":
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
196 completion['debug'] = "" if not self.options.debug else 'debug ("%s")\n%s' % (section,8*' ')
266
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
197 completion['args_nodefault'] = self.getArguments(function['sig_in'], name=arg_doc)
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
198 methods_part.append("""\
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
199 @dbus.service.method(const_INT_PREFIX+const_%(category)s_SUFFIX,
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
200 in_signature='%(sig_in)s', out_signature='%(sig_out)s')
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
201 def %(name)s(self, %(args)s):
266
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
202 %(debug)sreturn self.cb["%(name)s"](%(args_nodefault)s)
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
203 """ % completion)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
204
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
205 #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
206 #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
207 core_bridge = []
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
208 try:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
209 with open(self.core_template) as core_template:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
210 for line in core_template:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
211 if line.startswith('##SIGNALS_PART##'):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
212 core_bridge.extend(signals_part)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
213 elif line.startswith('##METHODS_PART##'):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
214 core_bridge.extend(methods_part)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
215 elif line.startswith('##DIRECT_CALLS##'):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
216 core_bridge.extend(direct_calls)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
217 else:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
218 core_bridge.append(line.replace('\n',''))
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
219 except IOError:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
220 print ("Can't open template file [%s]" % self.core_template)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
221 sys.exit(1)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
222
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
223 #now we write to final file
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
224 if os.path.exists(self.core_dest) and not self.options.force:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
225 print ("The destination file [%s] already exists ! Use --force to overwrite it" % self.core_dest)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
226 try:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
227 with open(self.core_dest,'w') as dest_file:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
228 dest_file.write('\n'.join(core_bridge))
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
229 except IOError:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
230 print ("Can't open destination file [%s]" % self.dest_file)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
231
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
232 class ConstructorError(Exception):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
233 pass
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
234
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
235 class ConstructorFactory:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
236 def create(self, bridge_template, options):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
237 if options.protocole=='dbus':
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
238 return DbusConstructor(bridge_template, options)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
239
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
240 raise ConstructorError('Unknown constructor type')
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
241
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
242 class BridgeConstructor:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
243 def __init__(self):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
244 self.options = None
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
245
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
246 def check_options(self):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
247 """Check command line options"""
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
248 _usage="""
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
249 %prog [options]
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
250
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
251 %prog --help for options list
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
252 """
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
253 parser = OptionParser(usage=_usage,version=ABOUT % VERSION)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
254
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
255 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
256 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
257 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
258 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
259 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
260 help="Use TEMPLATE to generate bridge (default: %default)")
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
261 parser.add_option("-f", "--force", action="store_true", default=False,
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
262 help=("Force overwritting of existing files"))
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
263 parser.add_option("-d", "--debug", action="store_true", default=False,
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
264 help=("Add debug information printing"))
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
266 (self.options, args) = parser.parse_args()
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
267 return args
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
268
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
269 def go(self):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
270 self.check_options()
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
271 self.template = Parser()
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
272 try:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
273 self.template.readfp(open(self.options.template))
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
274 except IOError:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
275 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
276 exit(1)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
277 constructor = ConstructorFactory().create(self.template, self.options)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
278 if self.options.side == "core":
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
279 constructor.generateCoreSide()
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
280
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
281 if __name__ == "__main__":
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
282 bc = BridgeConstructor()
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
283 bc.go()