Mercurial > libervia-backend
annotate src/bridge/bridge_constructor/bridge_constructor.py @ 2358:71b10dd7a13a
core (memory/sqlite): typo
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 22 Sep 2017 18:04:14 +0200 |
parents | da4097de5a95 |
children | 8b37a62336c3 |
rev | line source |
---|---|
1934
2daf7b4c6756
use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1 #!/usr/bin/env python2 |
265
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 |
2084
e1015a5df6f5
bridge(constructor): constructor now uses argparse instead of optparse
Goffi <goffi@goffi.org>
parents:
2050
diff
changeset
|
4 # SàT: a XMPP client |
1766 | 5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org) |
265
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
595
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
595
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
595
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
595
diff
changeset
|
10 # (at your option) any later version. |
265
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
595
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
595
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
595
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
595
diff
changeset
|
15 # GNU Affero General Public License for more details. |
265
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
595
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
595
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
265
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
2085
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
20 |
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
21 from sat.bridge import bridge_constructor |
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
22 from sat.bridge.bridge_constructor.constants import Const as C |
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
23 from sat.bridge.bridge_constructor import constructors, base_constructor |
2084
e1015a5df6f5
bridge(constructor): constructor now uses argparse instead of optparse
Goffi <goffi@goffi.org>
parents:
2050
diff
changeset
|
24 import argparse |
266
c4b84a2d2ad1
bridge: constructor and template improved, documentation added
Goffi <goffi@goffi.org>
parents:
265
diff
changeset
|
25 from ConfigParser import SafeConfigParser as Parser |
2085
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
26 from importlib import import_module |
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
27 import os |
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
28 import os.path |
265
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 |
2084
e1015a5df6f5
bridge(constructor): constructor now uses argparse instead of optparse
Goffi <goffi@goffi.org>
parents:
2050
diff
changeset
|
30 #consts |
e1015a5df6f5
bridge(constructor): constructor now uses argparse instead of optparse
Goffi <goffi@goffi.org>
parents:
2050
diff
changeset
|
31 __version__ = C.APP_VERSION |
595
1f160467f5de
Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
32 |
265
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 |
588
beaf6bec2fcd
Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
34 class BridgeConstructor(object): |
2085
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
35 |
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
36 def importConstructors(self): |
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
37 constructors_dir = os.path.dirname(constructors.__file__) |
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
38 self.protocoles = {} |
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
39 for dir_ in os.listdir(constructors_dir): |
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
40 init_path = os.path.join(constructors_dir, dir_, '__init__.py') |
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
41 constructor_path = os.path.join(constructors_dir, dir_, 'constructor.py') |
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
42 module_path = "sat.bridge.bridge_constructor.constructors.{}.constructor".format(dir_) |
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
43 if os.path.isfile(init_path) and os.path.isfile(constructor_path): |
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
44 mod = import_module(module_path) |
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
45 for attr in dir(mod): |
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
46 obj = getattr(mod, attr) |
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
47 if not isinstance(obj, type): |
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
48 continue |
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
49 if issubclass(obj, base_constructor.Constructor): |
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
50 name = obj.NAME or dir_ |
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
51 self.protocoles[name] = obj |
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
52 break |
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
53 if not self.protocoles: |
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
54 raise ValueError("no protocole constructor found") |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
55 |
2084
e1015a5df6f5
bridge(constructor): constructor now uses argparse instead of optparse
Goffi <goffi@goffi.org>
parents:
2050
diff
changeset
|
56 def parse_args(self): |
265
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 """Check command line options""" |
2085
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
58 parser = argparse.ArgumentParser(description=C.DESCRIPTION, formatter_class=argparse.RawDescriptionHelpFormatter) |
265
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 |
2084
e1015a5df6f5
bridge(constructor): constructor now uses argparse instead of optparse
Goffi <goffi@goffi.org>
parents:
2050
diff
changeset
|
60 parser.add_argument("--version", action="version", version= __version__) |
2085
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
61 default_protocole = C.DEFAULT_PROTOCOLE if C.DEFAULT_PROTOCOLE in self.protocoles else self.protocoles[0] |
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
62 parser.add_argument("-p", "--protocole", choices=sorted(self.protocoles), default=default_protocole, |
2084
e1015a5df6f5
bridge(constructor): constructor now uses argparse instead of optparse
Goffi <goffi@goffi.org>
parents:
2050
diff
changeset
|
63 help="generate bridge using PROTOCOLE (default: %(default)s)") # (default: %s, possible values: [%s])" % (DEFAULT_PROTOCOLE, ", ".join(MANAGED_PROTOCOLES))) |
e1015a5df6f5
bridge(constructor): constructor now uses argparse instead of optparse
Goffi <goffi@goffi.org>
parents:
2050
diff
changeset
|
64 parser.add_argument("-s", "--side", choices=("core", "frontend"), default="core", |
e1015a5df6f5
bridge(constructor): constructor now uses argparse instead of optparse
Goffi <goffi@goffi.org>
parents:
2050
diff
changeset
|
65 help="which side of the bridge do you want to make ?") # (default: %default, possible values: [core, frontend])") |
2085
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
66 default_template = os.path.join(os.path.dirname(bridge_constructor.__file__), 'bridge_template.ini') |
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
67 parser.add_argument("-t", "--template", type=file, default=default_template, |
2084
e1015a5df6f5
bridge(constructor): constructor now uses argparse instead of optparse
Goffi <goffi@goffi.org>
parents:
2050
diff
changeset
|
68 help="use TEMPLATE to generate bridge (default: %(default)s)") |
e1015a5df6f5
bridge(constructor): constructor now uses argparse instead of optparse
Goffi <goffi@goffi.org>
parents:
2050
diff
changeset
|
69 parser.add_argument("-f", "--force", action="store_true", |
e1015a5df6f5
bridge(constructor): constructor now uses argparse instead of optparse
Goffi <goffi@goffi.org>
parents:
2050
diff
changeset
|
70 help=("force overwritting of existing files")) |
e1015a5df6f5
bridge(constructor): constructor now uses argparse instead of optparse
Goffi <goffi@goffi.org>
parents:
2050
diff
changeset
|
71 parser.add_argument("-d", "--debug", action="store_true", |
e1015a5df6f5
bridge(constructor): constructor now uses argparse instead of optparse
Goffi <goffi@goffi.org>
parents:
2050
diff
changeset
|
72 help=("add debug information printing")) |
e1015a5df6f5
bridge(constructor): constructor now uses argparse instead of optparse
Goffi <goffi@goffi.org>
parents:
2050
diff
changeset
|
73 parser.add_argument("--no-unicode", action="store_false", dest="unicode", |
e1015a5df6f5
bridge(constructor): constructor now uses argparse instead of optparse
Goffi <goffi@goffi.org>
parents:
2050
diff
changeset
|
74 help=("remove unicode type protection from string results")) |
e1015a5df6f5
bridge(constructor): constructor now uses argparse instead of optparse
Goffi <goffi@goffi.org>
parents:
2050
diff
changeset
|
75 parser.add_argument("--flags", nargs='+', default=[], |
e1015a5df6f5
bridge(constructor): constructor now uses argparse instead of optparse
Goffi <goffi@goffi.org>
parents:
2050
diff
changeset
|
76 help=("constructors' specific flags")) |
2085
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
77 parser.add_argument("--dest-dir", default=C.DEST_DIR_DEFAULT, |
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
78 help=("directory when the generated files will be written (default: %(default)s)")) |
265
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 |
2084
e1015a5df6f5
bridge(constructor): constructor now uses argparse instead of optparse
Goffi <goffi@goffi.org>
parents:
2050
diff
changeset
|
80 return parser.parse_args() |
265
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 def go(self): |
2085
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
83 self.importConstructors() |
2084
e1015a5df6f5
bridge(constructor): constructor now uses argparse instead of optparse
Goffi <goffi@goffi.org>
parents:
2050
diff
changeset
|
84 args = self.parse_args() |
2085
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
85 template_parser = Parser() |
265
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 try: |
2085
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
87 template_parser.readfp(args.template) |
265
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 except IOError: |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 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
|
90 exit(1) |
2085
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
91 constructor = self.protocoles[args.protocole](template_parser, args) |
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
92 constructor.generate(args.side) |
da4097de5a95
bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
2084
diff
changeset
|
93 |
265
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 if __name__ == "__main__": |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 bc = BridgeConstructor() |
b5f1f3dc9ac6
bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 bc.go() |