annotate src/bridge/bridge_contructor.py @ 265:b5f1f3dc9ac6

bridge: automatic bridge generator, first draft
author Goffi <goffi@goffi.org>
date Mon, 24 Jan 2011 01:22:00 +0100
parents
children c4b84a2d2ad1
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
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
44 from ConfigParser import RawConfigParser as Parser
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
45 from ConfigParser import NoOptionError
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
46
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 class ParseError(Exception):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
49 #Used when the signature parsing is going wrong (invalid signature ?)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
50 pass
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
51
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
52 class Constructor:
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 def __init__(self, bridge_template, options):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
55 self.bridge_template = bridge_template
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
56 self.options = options
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 def getValues(self, name):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
59 """Return values of a function in a dict
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
60 @param name: Name of the function to get
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
61 @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
62 function={}
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
63 for option in ['type','category','sig_in','sig_out','doc']:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
64 try:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
65 value = self.bridge_template.get(name, option)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
66 except NoOptionError:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
67 value = None
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
68 function[option] = value
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
69 return function
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
70
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
71 def getArguments(self, signature):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
72 """Return arguments to user given a signature
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
73 @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
74 @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
75 i=0
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
76 idx=0
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
77 attr_string=""
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
78 while i<len(signature):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
79 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
80 raise ParseError("Unmanaged attribute type [%c]" % signature[i])
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
81
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
82 attr_string += ("" if idx==0 else ", ") + ("arg_%i" % idx)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
83 idx+=1
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
84
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
85 if signature[i] == 'a':
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
86 i+=1
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
87 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
88 i+=1
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
89 continue #we have a simple type for the array
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
90 opening_car = signature[i]
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
91 closing_car = '}' if opening_car == '{' else ')'
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
92 opening_count = 1
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
93 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
94 i+=1
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
95 if i>=len(signature):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
96 raise ParseError("missing }")
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
97 if signature[i] == opening_car:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
98 opening_count+=1
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
99 if signature[i] == closing_car:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
100 opening_count-=1
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
101 if opening_count == 0:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
102 break
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
103 i+=1
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
104 return attr_string
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
105
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
106 def generateCoreSide(self):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
107 """create the constructor in SàT core side (backend)"""
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
108 raise NotImplementedError
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
109
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
110 class DbusConstructor(Constructor):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
111
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
112 def __init__(self, bridge_template, options):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
113 Constructor.__init__(self, bridge_template, options)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
114 self.core_template="dbus_core_template.py"
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
115 self.core_dest="DBus.py"
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
116
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
117 def generateCoreSide(self):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
118 signals_part = []
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
119 methods_part = []
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
120 direct_calls = []
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
121 sections = self.bridge_template.sections()
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
122 sections.sort()
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
123 for section in sections:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
124 function = self.getValues(section)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
125 print ("Adding %s %s" % (section, function["type"]))
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
126 completion = {
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
127 'sig_in':function['sig_in'] or '',
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
128 'sig_out':function['sig_out'] or '',
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
129 'category':'REQ' if function['category'] == 'request' else 'COMM',
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
130 'name':section,
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
131 'args':self.getArguments(function['sig_in'])
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
132 }
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
133
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
134 if function["type"] == "signal":
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
135 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
136 signals_part.append("""\
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
137 @dbus.service.signal(const_INT_PREFIX+const_%(category)s_SUFFIX,
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
138 signature='%(sig_in)s')
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
139 def %(name)s(self, %(args)s):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
140 %(body)s
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
141 """ % completion)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
142 direct_calls.append("""\
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
143 def %(name)s(self, %(args)s):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
144 self.dbus_bridge.%(name)s(%(args)s)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
145 """ % completion)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
146
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
147 elif function["type"] == "method":
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
148 completion['debug'] = "" if not self.options.debug else 'debug ("%s")\n%s' % (section,8*' ')
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
149 methods_part.append("""\
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
150 @dbus.service.method(const_INT_PREFIX+const_%(category)s_SUFFIX,
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
151 in_signature='%(sig_in)s', out_signature='%(sig_out)s')
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
152 def %(name)s(self, %(args)s):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
153 %(debug)sreturn self.cb["%(name)s"](%(args)s)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
154 """ % completion)
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 #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
157 #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
158 core_bridge = []
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
159 try:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
160 with open(self.core_template) as core_template:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
161 for line in core_template:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
162 if line.startswith('##SIGNALS_PART##'):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
163 core_bridge.extend(signals_part)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
164 elif line.startswith('##METHODS_PART##'):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
165 core_bridge.extend(methods_part)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
166 elif line.startswith('##DIRECT_CALLS##'):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
167 core_bridge.extend(direct_calls)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
168 else:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
169 core_bridge.append(line.replace('\n',''))
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
170 except IOError:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
171 print ("Can't open template file [%s]" % self.core_template)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
172 sys.exit(1)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
173
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
174 #now we write to final file
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
175 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
176 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
177 try:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
178 with open(self.core_dest,'w') as dest_file:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
179 dest_file.write('\n'.join(core_bridge))
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
180 except IOError:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
181 print ("Can't open destination file [%s]" % self.dest_file)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
182
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
183 class ConstructorError(Exception):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
184 pass
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
185
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
186 class ConstructorFactory:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
187 def create(self, bridge_template, options):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
188 if options.protocole=='dbus':
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
189 return DbusConstructor(bridge_template, options)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
190
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
191 raise ConstructorError('Unknown constructor type')
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
192
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
193 class BridgeConstructor:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
194 def __init__(self):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
195 self.options = None
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
196
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
197 def check_options(self):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
198 """Check command line options"""
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
199 _usage="""
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
200 %prog [options]
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
201
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
202 %prog --help for options list
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
203 """
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
204 parser = OptionParser(usage=_usage,version=ABOUT % VERSION)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
205
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
206 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
207 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
208 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
209 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
210 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
211 help="Use TEMPLATE to generate bridge (default: %default)")
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
212 parser.add_option("-f", "--force", action="store_true", default=False,
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
213 help=("Force overwritting of existing files"))
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
214 parser.add_option("-d", "--debug", action="store_true", default=False,
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
215 help=("Add debug information printing"))
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
216
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
217 (self.options, args) = parser.parse_args()
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
218 return args
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
219
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
220 def go(self):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
221 self.check_options()
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
222 self.template = Parser()
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
223 try:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
224 self.template.readfp(open(self.options.template))
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
225 except IOError:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
226 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
227 exit(1)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
228 constructor = ConstructorFactory().create(self.template, self.options)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
229 if self.options.side == "core":
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
230 constructor.generateCoreSide()
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 if __name__ == "__main__":
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
233 bc = BridgeConstructor()
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
234 bc.go()