annotate src/bridge/bridge_constructor/bridge_contructor.py @ 269:78140bb2f0e3

updated .hgignore
author Goffi <goffi@goffi.org>
date Mon, 24 Jan 2011 21:43:07 +0100
parents bdcd535e179e
children 0288f97334f2
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
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
113 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
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)
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
116 @param name: dictionary of arguments name like given by getArguments
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
117 @param default: dictionary of default values, like given by getDefault
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
118 @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
119 @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
120 i=0
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
121 idx=0
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
122 attr_string=[]
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
123 while i<len(signature):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
124 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
125 raise ParseError("Unmanaged attribute type [%c]" % signature[i])
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
126
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
127 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
128 '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
129 'default':"="+default[idx] if (default and default.has_key(idx)) else ''
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
130 }) #give arg_1, arg2, etc or name1, name2=default, etc. \
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
131 #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
132 idx+=1
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 signature[i] == 'a':
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
135 i+=1
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
136 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
137 i+=1
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
138 continue #we have a simple type for the array
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
139 opening_car = signature[i]
266
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
140 assert(opening_car in ['{','('])
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
141 closing_car = '}' if opening_car == '{' else ')'
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
142 opening_count = 1
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
143 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
144 i+=1
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
145 if i>=len(signature):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
146 raise ParseError("missing }")
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
147 if signature[i] == opening_car:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
148 opening_count+=1
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
149 if signature[i] == closing_car:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
150 opening_count-=1
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
151 if opening_count == 0:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
152 break
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
153 i+=1
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
154 return ", ".join(attr_string)
265
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 def generateCoreSide(self):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
157 """create the constructor in SàT core side (backend)"""
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
158 raise NotImplementedError
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
159
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
160 class DbusConstructor(Constructor):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
161
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
162 def __init__(self, bridge_template, options):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
163 Constructor.__init__(self, bridge_template, options)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
164 self.core_template="dbus_core_template.py"
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
165 self.frontend_template="dbus_frontend_template.py"
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
166 self.frontend_dest = self.core_dest="DBus.py"
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
167
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
168 def generateCoreSide(self):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
169 signals_part = []
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
170 methods_part = []
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
171 direct_calls = []
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
172 sections = self.bridge_template.sections()
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
173 sections.sort()
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
174 for section in sections:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
175 function = self.getValues(section)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
176 print ("Adding %s %s" % (section, function["type"]))
266
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
177 default = self.getDefault(section)
c4b84a2d2ad1 bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents: 265
diff changeset
178 arg_doc = self.getArgumentsDoc(section)
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
179 completion = {
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
180 'sig_in':function['sig_in'] or '',
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
181 'sig_out':function['sig_out'] or '',
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
182 'category':'REQ' if function['category'] == 'request' else 'COMM',
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
183 'name':section,
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
184 '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
185 }
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
186
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
187 if function["type"] == "signal":
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
188 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
189 signals_part.append("""\
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
190 @dbus.service.signal(const_INT_PREFIX+const_%(category)s_SUFFIX,
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
191 signature='%(sig_in)s')
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
192 def %(name)s(self, %(args)s):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
193 %(body)s
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
194 """ % completion)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
195 direct_calls.append("""\
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
196 def %(name)s(self, %(args)s):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
197 self.dbus_bridge.%(name)s(%(args)s)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
198 """ % completion)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
199
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
200 elif function["type"] == "method":
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
201 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
202 completion['args_result'] = self.getArguments(function['sig_in'], name=arg_doc, unicode_protect=self.options.unicode)
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
203 methods_part.append("""\
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
204 @dbus.service.method(const_INT_PREFIX+const_%(category)s_SUFFIX,
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
205 in_signature='%(sig_in)s', out_signature='%(sig_out)s')
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
206 def %(name)s(self, %(args)s):
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
207 %(debug)sreturn self.cb["%(name)s"](%(args_result)s)
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
208 """ % completion)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
209
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
210 #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
211 #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
212 core_bridge = []
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
213 try:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
214 with open(self.core_template) as core_template:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
215 for line in core_template:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
216 if line.startswith('##SIGNALS_PART##'):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
217 core_bridge.extend(signals_part)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
218 elif line.startswith('##METHODS_PART##'):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
219 core_bridge.extend(methods_part)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
220 elif line.startswith('##DIRECT_CALLS##'):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
221 core_bridge.extend(direct_calls)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
222 else:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
223 core_bridge.append(line.replace('\n',''))
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
224 except IOError:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
225 print ("Can't open template file [%s]" % self.core_template)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
226 sys.exit(1)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
227
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
228 #now we write to final file
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
229 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
230 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
231 try:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
232 with open(self.core_dest,'w') as dest_file:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
233 dest_file.write('\n'.join(core_bridge))
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
234 except IOError:
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
235 print ("Can't open destination file [%s]" % self.core_dest)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
236
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
237 def generateFrontendSide(self):
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
238 methods_part = []
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
239 sections = self.bridge_template.sections()
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
240 sections.sort()
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
241 for section in sections:
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
242 function = self.getValues(section)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
243 print ("Adding %s %s" % (section, function["type"]))
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
244 default = self.getDefault(section)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
245 arg_doc = self.getArgumentsDoc(section)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
246 completion = {
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
247 'sig_in':function['sig_in'] or '',
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
248 'sig_out':function['sig_out'] or '',
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
249 'category':'req' if function['category'] == 'request' else 'comm',
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
250 'name':section,
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
251 'args':self.getArguments(function['sig_in'], name=arg_doc, default=default )
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
252 }
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
253
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
254 if function["type"] == "method":
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
255 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
256 completion['args_result'] = self.getArguments(function['sig_in'], name=arg_doc)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
257 methods_part.append("""\
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
258 def %(name)s(self, %(args)s):
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
259 %(debug)sreturn self.db_%(category)s_iface.%(name)s(%(args_result)s)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
260 """ % completion)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
261
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
262 #at this point, methods_part should be filled,
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
263 #we just have to place it in the right part of the template
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
264 frontend_bridge = []
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
265 try:
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
266 with open(self.frontend_template) as frontend_template:
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
267 for line in frontend_template:
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
268 if line.startswith('##METHODS_PART##'):
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
269 frontend_bridge.extend(methods_part)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
270 else:
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
271 frontend_bridge.append(line.replace('\n',''))
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
272 except IOError:
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
273 print ("Can't open template file [%s]" % self.frontend_template)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
274 sys.exit(1)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
275
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
276 #now we write to final file
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
277 if os.path.exists(self.frontend_dest) and not self.options.force:
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
278 print ("The destination file [%s] already exists ! Use --force to overwrite it" % self.frontend_dest)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
279 try:
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
280 with open(self.frontend_dest,'w') as dest_file:
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
281 dest_file.write('\n'.join(frontend_bridge))
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
282 except IOError:
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
283 print ("Can't open destination file [%s]" % self.frontend_dest)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
284
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
285
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
286 class ConstructorError(Exception):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
287 pass
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
288
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
289 class ConstructorFactory:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
290 def create(self, bridge_template, options):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
291 if options.protocole=='dbus':
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
292 return DbusConstructor(bridge_template, options)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
293
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
294 raise ConstructorError('Unknown constructor type')
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
295
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
296 class BridgeConstructor:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
297 def __init__(self):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
298 self.options = None
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
299
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
300 def check_options(self):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
301 """Check command line options"""
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
302 _usage="""
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
303 %prog [options]
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
304
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
305 %prog --help for options list
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
306 """
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
307 parser = OptionParser(usage=_usage,version=ABOUT % VERSION)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
308
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
309 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
310 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
311 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
312 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
313 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
314 help="Use TEMPLATE to generate bridge (default: %default)")
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
315 parser.add_option("-f", "--force", action="store_true", default=False,
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
316 help=("Force overwritting of existing files"))
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
317 parser.add_option("-d", "--debug", action="store_true", default=False,
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
318 help=("Add debug information printing"))
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
319 parser.add_option("--no_unicode", action="store_false", dest="unicode", default=True,
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
320 help=("Remove unicode type protection from string results"))
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
321
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
322
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
323 (self.options, args) = parser.parse_args()
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
324 return args
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 go(self):
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
327 self.check_options()
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
328 self.template = Parser()
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
329 try:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
330 self.template.readfp(open(self.options.template))
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
331 except IOError:
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
332 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
333 exit(1)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
334 constructor = ConstructorFactory().create(self.template, self.options)
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
335 if self.options.side == "core":
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
336 constructor.generateCoreSide()
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
337 elif self.options.side == "frontend":
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents: 266
diff changeset
338 constructor.generateFrontendSide()
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
339
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
340 if __name__ == "__main__":
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
341 bc = BridgeConstructor()
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
342 bc.go()