Mercurial > libervia-backend
comparison libervia/backend/bridge/bridge_constructor/bridge_constructor.py @ 4071:4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 02 Jun 2023 11:49:51 +0200 |
parents | sat/bridge/bridge_constructor/bridge_constructor.py@524856bd7b19 |
children |
comparison
equal
deleted
inserted
replaced
4070:d10748475025 | 4071:4b842c1fb686 |
---|---|
1 #!/usr/bin/env python3 | |
2 | |
3 | |
4 # Libervia: an XMPP client | |
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 | |
21 from libervia.backend.bridge import bridge_constructor | |
22 from libervia.backend.bridge.bridge_constructor.constants import Const as C | |
23 from libervia.backend.bridge.bridge_constructor import constructors, base_constructor | |
24 import argparse | |
25 from configparser import ConfigParser as Parser | |
26 from importlib import import_module | |
27 import os | |
28 import os.path | |
29 | |
30 # consts | |
31 __version__ = C.APP_VERSION | |
32 | |
33 | |
34 class BridgeConstructor(object): | |
35 def import_constructors(self): | |
36 constructors_dir = os.path.dirname(constructors.__file__) | |
37 self.protocoles = {} | |
38 for dir_ in os.listdir(constructors_dir): | |
39 init_path = os.path.join(constructors_dir, dir_, "__init__.py") | |
40 constructor_path = os.path.join(constructors_dir, dir_, "constructor.py") | |
41 module_path = "libervia.backend.bridge.bridge_constructor.constructors.{}.constructor".format( | |
42 dir_ | |
43 ) | |
44 if os.path.isfile(init_path) and os.path.isfile(constructor_path): | |
45 mod = import_module(module_path) | |
46 for attr in dir(mod): | |
47 obj = getattr(mod, attr) | |
48 if not isinstance(obj, type): | |
49 continue | |
50 if issubclass(obj, base_constructor.Constructor): | |
51 name = obj.NAME or dir_ | |
52 self.protocoles[name] = obj | |
53 break | |
54 if not self.protocoles: | |
55 raise ValueError("no protocole constructor found") | |
56 | |
57 def parse_args(self): | |
58 """Check command line options""" | |
59 parser = argparse.ArgumentParser( | |
60 description=C.DESCRIPTION, | |
61 formatter_class=argparse.RawDescriptionHelpFormatter, | |
62 ) | |
63 | |
64 parser.add_argument("--version", action="version", version=__version__) | |
65 default_protocole = ( | |
66 C.DEFAULT_PROTOCOLE | |
67 if C.DEFAULT_PROTOCOLE in self.protocoles | |
68 else self.protocoles[0] | |
69 ) | |
70 parser.add_argument( | |
71 "-p", | |
72 "--protocole", | |
73 choices=sorted(self.protocoles), | |
74 default=default_protocole, | |
75 help="generate bridge using PROTOCOLE (default: %(default)s)", | |
76 ) # (default: %s, possible values: [%s])" % (DEFAULT_PROTOCOLE, ", ".join(MANAGED_PROTOCOLES))) | |
77 parser.add_argument( | |
78 "-s", | |
79 "--side", | |
80 choices=("core", "frontend"), | |
81 default="core", | |
82 help="which side of the bridge do you want to make ?", | |
83 ) # (default: %default, possible values: [core, frontend])") | |
84 default_template = os.path.join( | |
85 os.path.dirname(bridge_constructor.__file__), "bridge_template.ini" | |
86 ) | |
87 parser.add_argument( | |
88 "-t", | |
89 "--template", | |
90 type=argparse.FileType(), | |
91 default=default_template, | |
92 help="use TEMPLATE to generate bridge (default: %(default)s)", | |
93 ) | |
94 parser.add_argument( | |
95 "-f", | |
96 "--force", | |
97 action="store_true", | |
98 help=("force overwritting of existing files"), | |
99 ) | |
100 parser.add_argument( | |
101 "-d", "--debug", action="store_true", help=("add debug information printing") | |
102 ) | |
103 parser.add_argument( | |
104 "--no-unicode", | |
105 action="store_false", | |
106 dest="unicode", | |
107 help=("remove unicode type protection from string results"), | |
108 ) | |
109 parser.add_argument( | |
110 "--flags", nargs="+", default=[], help=("constructors' specific flags") | |
111 ) | |
112 parser.add_argument( | |
113 "--dest-dir", | |
114 default=C.DEST_DIR_DEFAULT, | |
115 help=( | |
116 "directory when the generated files will be written (default: %(default)s)" | |
117 ), | |
118 ) | |
119 | |
120 return parser.parse_args() | |
121 | |
122 def go(self): | |
123 self.import_constructors() | |
124 args = self.parse_args() | |
125 template_parser = Parser() | |
126 try: | |
127 template_parser.read_file(args.template) | |
128 except IOError: | |
129 print("The template file doesn't exist or is not accessible") | |
130 exit(1) | |
131 constructor = self.protocoles[args.protocole](template_parser, args) | |
132 constructor.generate(args.side) | |
133 | |
134 | |
135 if __name__ == "__main__": | |
136 bc = BridgeConstructor() | |
137 bc.go() |