Mercurial > libervia-backend
annotate src/bridge/bridge_constructor/bridge_contructor.py @ 271:0288f97334f2
bridge: constructor now generate files in a 'generated' subdirectory
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 24 Jan 2011 21:48:09 +0100 |
parents | bdcd535e179e |
children | 3b5e856b3a32 |
rev | line source |
---|---|
265
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/python |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 #-*- coding: utf-8 -*- |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 """ |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 SAT: a jabber client |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org) |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 This program is free software: you can redistribute it and/or modify |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 it under the terms of the GNU General Public License as published by |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 the Free Software Foundation, either version 3 of the License, or |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 (at your option) any later version. |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 This program is distributed in the hope that it will be useful, |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 but WITHOUT ANY WARRANTY; without even the implied warranty of |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 GNU General Public License for more details. |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 You should have received a copy of the GNU General Public License |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 along with this program. If not, see <http://www.gnu.org/licenses/>. |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 """ |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 #consts |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 NAME = u"bridge_constructor" |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 VERSION="0.1.0" |
271
0288f97334f2
bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents:
267
diff
changeset
|
26 DEST_DIR="generated" |
265
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 ABOUT = NAME+u""" v%s (c) Jérôme Poisson (aka Goffi) 2011 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 --- |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 """+NAME+u""" Copyright (C) 2011 Jérôme Poisson (aka Goffi) |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 This program comes with ABSOLUTELY NO WARRANTY; |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 This is free software, and you are welcome to redistribute it |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 under certain conditions. |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 --- |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 This script construct a SàT bridge using the given protocol |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 """ |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 MANAGED_PROTOCOLES=['dbus'] |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 DEFAULT_PROTOCOLE='dbus' |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 import sys |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 import os |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 from os import path |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 from optparse import OptionParser |
266
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
45 from ConfigParser import SafeConfigParser as Parser |
265
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 from ConfigParser import NoOptionError |
266
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
47 import re |
265
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 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 class ParseError(Exception): |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 #Used when the signature parsing is going wrong (invalid signature ?) |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 pass |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 class Constructor: |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 def __init__(self, bridge_template, options): |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 self.bridge_template = bridge_template |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 self.options = options |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 def getValues(self, name): |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 """Return values of a function in a dict |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 @param name: Name of the function to get |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 @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
|
64 function={} |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 for option in ['type','category','sig_in','sig_out','doc']: |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 try: |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 value = self.bridge_template.get(name, option) |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 except NoOptionError: |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 value = None |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 function[option] = value |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 return function |
266
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
72 |
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
73 def getDefault(self, name): |
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
74 """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
|
75 @param name: Name of the function to get |
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
76 @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
|
77 default_dict={} |
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
78 def_re = re.compile(r"param_(\d+)_default") |
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
79 |
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
80 for option in self.bridge_template.options(name): |
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
81 if option == 'doc_return': |
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
82 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
|
83 continue |
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
84 match = def_re.match(option) |
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
85 if match: |
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
86 try: |
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
87 idx = int(match.group(1)) |
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
88 except ValueError: |
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
89 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
|
90 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
|
91 |
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
92 return default_dict |
265
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 |
266
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
94 def getArgumentsDoc(self, name): |
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
95 """Return documentation of arguments |
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
96 @param name: Name of the function to get |
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
97 @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
|
98 doc_dict={} |
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
99 option_re = re.compile(r"doc_param_(\d+)") |
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
100 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
|
101 for option in self.bridge_template.options(name): |
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
102 match = option_re.match(option) |
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
103 if match: |
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
104 try: |
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
105 idx = int(match.group(1)) |
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
106 except ValueError: |
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
107 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
|
108 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
|
109 if not value_match: |
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
110 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
|
111 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
|
112 return doc_dict |
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
113 |
267 | 114 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
|
115 """Return arguments to user given a signature |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 @param signature: signature in the short form (using s,a,i,b etc) |
267 | 117 @param name: dictionary of arguments name like given by getArguments |
118 @param default: dictionary of default values, like given by getDefault | |
119 @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
|
120 @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
|
121 i=0 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 idx=0 |
267 | 123 attr_string=[] |
265
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 while i<len(signature): |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 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
|
126 raise ParseError("Unmanaged attribute type [%c]" % signature[i]) |
267 | 127 |
128 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
|
129 '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
|
130 'default':"="+default[idx] if (default and default.has_key(idx)) else '' |
267 | 131 }) #give arg_1, arg2, etc or name1, name2=default, etc. \ |
132 #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
|
133 idx+=1 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 if signature[i] == 'a': |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
136 i+=1 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 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
|
138 i+=1 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 continue #we have a simple type for the array |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 opening_car = signature[i] |
266
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
141 assert(opening_car in ['{','(']) |
265
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 closing_car = '}' if opening_car == '{' else ')' |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 opening_count = 1 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
144 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
|
145 i+=1 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
146 if i>=len(signature): |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
147 raise ParseError("missing }") |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 if signature[i] == opening_car: |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 opening_count+=1 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 if signature[i] == closing_car: |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
151 opening_count-=1 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
152 if opening_count == 0: |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 break |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 i+=1 |
267 | 155 return ", ".join(attr_string) |
265
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
156 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
157 def generateCoreSide(self): |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
158 """create the constructor in SàT core side (backend)""" |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
159 raise NotImplementedError |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 |
271
0288f97334f2
bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents:
267
diff
changeset
|
161 def generateFrontendSide(self): |
0288f97334f2
bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents:
267
diff
changeset
|
162 """create the constructor in SàT frontend side""" |
0288f97334f2
bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents:
267
diff
changeset
|
163 raise NotImplementedError |
0288f97334f2
bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents:
267
diff
changeset
|
164 |
0288f97334f2
bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents:
267
diff
changeset
|
165 def finalWrite(self, filename, file_buf): |
0288f97334f2
bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents:
267
diff
changeset
|
166 """Write the final generated file in DEST_DIR/filename |
0288f97334f2
bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents:
267
diff
changeset
|
167 @param filename: name of the file to generate |
0288f97334f2
bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents:
267
diff
changeset
|
168 @param file_buf: list of lines (stings) of the file""" |
0288f97334f2
bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents:
267
diff
changeset
|
169 if os.path.exists(DEST_DIR) and not os.path.isdir(DEST_DIR): |
0288f97334f2
bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents:
267
diff
changeset
|
170 print ("The destination dir [%s] can't be created: a file with this name already exists !") |
0288f97334f2
bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents:
267
diff
changeset
|
171 sys.exit(1) |
0288f97334f2
bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents:
267
diff
changeset
|
172 try: |
0288f97334f2
bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents:
267
diff
changeset
|
173 if not os.path.exists(DEST_DIR): |
0288f97334f2
bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents:
267
diff
changeset
|
174 os.mkdir(DEST_DIR) |
0288f97334f2
bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents:
267
diff
changeset
|
175 full_path=os.path.join(DEST_DIR,filename) |
0288f97334f2
bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents:
267
diff
changeset
|
176 if os.path.exists(full_path) and not self.options.force: |
0288f97334f2
bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents:
267
diff
changeset
|
177 print ("The destination file [%s] already exists ! Use --force to overwrite it" % full_path) |
0288f97334f2
bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents:
267
diff
changeset
|
178 try: |
0288f97334f2
bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents:
267
diff
changeset
|
179 with open(full_path,'w') as dest_file: |
0288f97334f2
bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents:
267
diff
changeset
|
180 dest_file.write('\n'.join(file_buf)) |
0288f97334f2
bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents:
267
diff
changeset
|
181 except IOError: |
0288f97334f2
bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents:
267
diff
changeset
|
182 print ("Can't open destination file [%s]" % full_path) |
0288f97334f2
bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents:
267
diff
changeset
|
183 except OSError: |
0288f97334f2
bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents:
267
diff
changeset
|
184 print("It's not possible to generate the file, check your permissions") |
0288f97334f2
bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents:
267
diff
changeset
|
185 exit(1) |
0288f97334f2
bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents:
267
diff
changeset
|
186 |
265
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
187 class DbusConstructor(Constructor): |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
188 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
189 def __init__(self, bridge_template, options): |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
190 Constructor.__init__(self, bridge_template, options) |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
191 self.core_template="dbus_core_template.py" |
267 | 192 self.frontend_template="dbus_frontend_template.py" |
193 self.frontend_dest = self.core_dest="DBus.py" | |
265
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 def generateCoreSide(self): |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
196 signals_part = [] |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
197 methods_part = [] |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
198 direct_calls = [] |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
199 sections = self.bridge_template.sections() |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
200 sections.sort() |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
201 for section in sections: |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
202 function = self.getValues(section) |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
203 print ("Adding %s %s" % (section, function["type"])) |
266
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
204 default = self.getDefault(section) |
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
205 arg_doc = self.getArgumentsDoc(section) |
265
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
206 completion = { |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
207 'sig_in':function['sig_in'] or '', |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
208 'sig_out':function['sig_out'] or '', |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
209 'category':'REQ' if function['category'] == 'request' else 'COMM', |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
210 'name':section, |
267 | 211 '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
|
212 } |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
213 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
214 if function["type"] == "signal": |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
215 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
|
216 signals_part.append("""\ |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
217 @dbus.service.signal(const_INT_PREFIX+const_%(category)s_SUFFIX, |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
218 signature='%(sig_in)s') |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
219 def %(name)s(self, %(args)s): |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
220 %(body)s |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
221 """ % completion) |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
222 direct_calls.append("""\ |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
223 def %(name)s(self, %(args)s): |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
224 self.dbus_bridge.%(name)s(%(args)s) |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
225 """ % completion) |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
226 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
227 elif function["type"] == "method": |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
228 completion['debug'] = "" if not self.options.debug else 'debug ("%s")\n%s' % (section,8*' ') |
267 | 229 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
|
230 methods_part.append("""\ |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
231 @dbus.service.method(const_INT_PREFIX+const_%(category)s_SUFFIX, |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
232 in_signature='%(sig_in)s', out_signature='%(sig_out)s') |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
233 def %(name)s(self, %(args)s): |
267 | 234 %(debug)sreturn self.cb["%(name)s"](%(args_result)s) |
265
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
235 """ % completion) |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
236 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
237 #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
|
238 #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
|
239 core_bridge = [] |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
240 try: |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
241 with open(self.core_template) as core_template: |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
242 for line in core_template: |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
243 if line.startswith('##SIGNALS_PART##'): |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
244 core_bridge.extend(signals_part) |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
245 elif line.startswith('##METHODS_PART##'): |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
246 core_bridge.extend(methods_part) |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
247 elif line.startswith('##DIRECT_CALLS##'): |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
248 core_bridge.extend(direct_calls) |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
249 else: |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
250 core_bridge.append(line.replace('\n','')) |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
251 except IOError: |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
252 print ("Can't open template file [%s]" % self.core_template) |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
253 sys.exit(1) |
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 #now we write to final file |
271
0288f97334f2
bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents:
267
diff
changeset
|
256 self.finalWrite(self.core_dest, core_bridge) |
267 | 257 |
258 def generateFrontendSide(self): | |
259 methods_part = [] | |
260 sections = self.bridge_template.sections() | |
261 sections.sort() | |
262 for section in sections: | |
263 function = self.getValues(section) | |
264 print ("Adding %s %s" % (section, function["type"])) | |
265 default = self.getDefault(section) | |
266 arg_doc = self.getArgumentsDoc(section) | |
267 completion = { | |
268 'sig_in':function['sig_in'] or '', | |
269 'sig_out':function['sig_out'] or '', | |
270 'category':'req' if function['category'] == 'request' else 'comm', | |
271 'name':section, | |
272 'args':self.getArguments(function['sig_in'], name=arg_doc, default=default ) | |
273 } | |
274 | |
275 if function["type"] == "method": | |
276 completion['debug'] = "" if not self.options.debug else 'debug ("%s")\n%s' % (section,8*' ') | |
277 completion['args_result'] = self.getArguments(function['sig_in'], name=arg_doc) | |
278 methods_part.append("""\ | |
279 def %(name)s(self, %(args)s): | |
280 %(debug)sreturn self.db_%(category)s_iface.%(name)s(%(args_result)s) | |
281 """ % completion) | |
282 | |
283 #at this point, methods_part should be filled, | |
284 #we just have to place it in the right part of the template | |
285 frontend_bridge = [] | |
286 try: | |
287 with open(self.frontend_template) as frontend_template: | |
288 for line in frontend_template: | |
289 if line.startswith('##METHODS_PART##'): | |
290 frontend_bridge.extend(methods_part) | |
291 else: | |
292 frontend_bridge.append(line.replace('\n','')) | |
293 except IOError: | |
294 print ("Can't open template file [%s]" % self.frontend_template) | |
295 sys.exit(1) | |
296 | |
297 #now we write to final file | |
271
0288f97334f2
bridge: constructor now generate files in a 'generated' subdirectory
Goffi <goffi@goffi.org>
parents:
267
diff
changeset
|
298 self.finalWrite(self.frontend_dest, frontend_bridge) |
267 | 299 |
265
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
300 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
301 class ConstructorError(Exception): |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
302 pass |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
303 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
304 class ConstructorFactory: |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
305 def create(self, bridge_template, options): |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
306 if options.protocole=='dbus': |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
307 return DbusConstructor(bridge_template, options) |
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 raise ConstructorError('Unknown constructor type') |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
310 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
311 class BridgeConstructor: |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
312 def __init__(self): |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
313 self.options = None |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
314 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
315 def check_options(self): |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
316 """Check command line options""" |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
317 _usage=""" |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
318 %prog [options] |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
319 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
320 %prog --help for options list |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
321 """ |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
322 parser = OptionParser(usage=_usage,version=ABOUT % VERSION) |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
323 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
324 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
|
325 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
|
326 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
|
327 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
|
328 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
|
329 help="Use TEMPLATE to generate bridge (default: %default)") |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
330 parser.add_option("-f", "--force", action="store_true", default=False, |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
331 help=("Force overwritting of existing files")) |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
332 parser.add_option("-d", "--debug", action="store_true", default=False, |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
333 help=("Add debug information printing")) |
267 | 334 parser.add_option("--no_unicode", action="store_false", dest="unicode", default=True, |
335 help=("Remove unicode type protection from string results")) | |
336 | |
265
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
337 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
338 (self.options, args) = parser.parse_args() |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
339 return args |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
340 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
341 def go(self): |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
342 self.check_options() |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
343 self.template = Parser() |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
344 try: |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
345 self.template.readfp(open(self.options.template)) |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
346 except IOError: |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
347 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
|
348 exit(1) |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
349 constructor = ConstructorFactory().create(self.template, self.options) |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
350 if self.options.side == "core": |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
351 constructor.generateCoreSide() |
267 | 352 elif self.options.side == "frontend": |
353 constructor.generateFrontendSide() | |
265
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
354 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
355 if __name__ == "__main__": |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
356 bc = BridgeConstructor() |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
357 bc.go() |