annotate sat/bridge/bridge_constructor/bridge_constructor.py @ 3145:3f414c897e70

core (constants): don't parse `sat.conf` from current dir anymore + parse root_dir last
author Goffi <goffi@goffi.org>
date Wed, 29 Jan 2020 19:12:46 +0100
parents 559a625a236b
children 560642ab1c10
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1 #!/usr/bin/env python3
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
265
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
3136
9d0df638c8b4 dates update
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
5 # Copyright (C) 2009-2020 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
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
25 from configparser import ConfigParser as Parser
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
26 from importlib import import_module
2085
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
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
30 # consts
2084
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 def importConstructors(self):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents: 2084
diff changeset
36 constructors_dir = os.path.dirname(constructors.__file__)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents: 2084
diff changeset
37 self.protocoles = {}
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents: 2084
diff changeset
38 for dir_ in os.listdir(constructors_dir):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
39 init_path = os.path.join(constructors_dir, dir_, "__init__.py")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
40 constructor_path = os.path.join(constructors_dir, dir_, "constructor.py")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
41 module_path = "sat.bridge.bridge_constructor.constructors.{}.constructor".format(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
42 dir_
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
43 )
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents: 2084
diff changeset
44 if os.path.isfile(init_path) and os.path.isfile(constructor_path):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents: 2084
diff changeset
45 mod = import_module(module_path)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents: 2084
diff changeset
46 for attr in dir(mod):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents: 2084
diff changeset
47 obj = getattr(mod, attr)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents: 2084
diff changeset
48 if not isinstance(obj, type):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents: 2084
diff changeset
49 continue
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents: 2084
diff changeset
50 if issubclass(obj, base_constructor.Constructor):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents: 2084
diff changeset
51 name = obj.NAME or dir_
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents: 2084
diff changeset
52 self.protocoles[name] = obj
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents: 2084
diff changeset
53 break
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents: 2084
diff changeset
54 if not self.protocoles:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents: 2084
diff changeset
55 raise ValueError("no protocole constructor found")
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
56
2084
e1015a5df6f5 bridge(constructor): constructor now uses argparse instead of optparse
Goffi <goffi@goffi.org>
parents: 2050
diff changeset
57 def parse_args(self):
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
58 """Check command line options"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
59 parser = argparse.ArgumentParser(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
60 description=C.DESCRIPTION,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
61 formatter_class=argparse.RawDescriptionHelpFormatter,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
62 )
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
63
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
64 parser.add_argument("--version", action="version", version=__version__)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
65 default_protocole = (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
66 C.DEFAULT_PROTOCOLE
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
67 if C.DEFAULT_PROTOCOLE in self.protocoles
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
68 else self.protocoles[0]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
69 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
70 parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
71 "-p",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
72 "--protocole",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
73 choices=sorted(self.protocoles),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
74 default=default_protocole,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
75 help="generate bridge using PROTOCOLE (default: %(default)s)",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
76 ) # (default: %s, possible values: [%s])" % (DEFAULT_PROTOCOLE, ", ".join(MANAGED_PROTOCOLES)))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
77 parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
78 "-s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
79 "--side",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
80 choices=("core", "frontend"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
81 default="core",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
82 help="which side of the bridge do you want to make ?",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
83 ) # (default: %default, possible values: [core, frontend])")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
84 default_template = os.path.join(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
85 os.path.dirname(bridge_constructor.__file__), "bridge_template.ini"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
86 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
87 parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
88 "-t",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
89 "--template",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
90 type=argparse.FileType(),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
91 default=default_template,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
92 help="use TEMPLATE to generate bridge (default: %(default)s)",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
93 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
94 parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
95 "-f",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
96 "--force",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
97 action="store_true",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
98 help=("force overwritting of existing files"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
99 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
100 parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
101 "-d", "--debug", action="store_true", help=("add debug information printing")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
102 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
103 parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
104 "--no-unicode",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
105 action="store_false",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
106 dest="unicode",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
107 help=("remove unicode type protection from string results"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
108 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
109 parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
110 "--flags", nargs="+", default=[], help=("constructors' specific flags")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
111 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
112 parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
113 "--dest-dir",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
114 default=C.DEST_DIR_DEFAULT,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
115 help=(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
116 "directory when the generated files will be written (default: %(default)s)"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
117 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
118 )
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
119
2084
e1015a5df6f5 bridge(constructor): constructor now uses argparse instead of optparse
Goffi <goffi@goffi.org>
parents: 2050
diff changeset
120 return parser.parse_args()
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
121
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
122 def go(self):
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents: 2084
diff changeset
123 self.importConstructors()
2084
e1015a5df6f5 bridge(constructor): constructor now uses argparse instead of optparse
Goffi <goffi@goffi.org>
parents: 2050
diff changeset
124 args = self.parse_args()
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents: 2084
diff changeset
125 template_parser = Parser()
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
126 try:
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents: 2084
diff changeset
127 template_parser.readfp(args.template)
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
128 except IOError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
129 print("The template file doesn't exist or is not accessible")
265
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
130 exit(1)
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents: 2084
diff changeset
131 constructor = self.protocoles[args.protocole](template_parser, args)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents: 2084
diff changeset
132 constructor.generate(args.side)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents: 2084
diff changeset
133
265
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 __name__ == "__main__":
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
136 bc = BridgeConstructor()
b5f1f3dc9ac6 bridge: automatic bridge generator, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
137 bc.go()