comparison sat/bridge/bridge_constructor/bridge_constructor.py @ 2562:26edcf3a30eb

core, setup: huge cleaning: - moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents src/bridge/bridge_constructor/bridge_constructor.py@0046283a285d
children 56f94936df1e
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
1 #!/usr/bin/env python2
2 #-*- coding: utf-8 -*-
3
4 # SàT: a XMPP client
5 # Copyright (C) 2009-2018 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 sat.bridge import bridge_constructor
22 from sat.bridge.bridge_constructor.constants import Const as C
23 from sat.bridge.bridge_constructor import constructors, base_constructor
24 import argparse
25 from ConfigParser import SafeConfigParser 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
36 def importConstructors(self):
37 constructors_dir = os.path.dirname(constructors.__file__)
38 self.protocoles = {}
39 for dir_ in os.listdir(constructors_dir):
40 init_path = os.path.join(constructors_dir, dir_, '__init__.py')
41 constructor_path = os.path.join(constructors_dir, dir_, 'constructor.py')
42 module_path = "sat.bridge.bridge_constructor.constructors.{}.constructor".format(dir_)
43 if os.path.isfile(init_path) and os.path.isfile(constructor_path):
44 mod = import_module(module_path)
45 for attr in dir(mod):
46 obj = getattr(mod, attr)
47 if not isinstance(obj, type):
48 continue
49 if issubclass(obj, base_constructor.Constructor):
50 name = obj.NAME or dir_
51 self.protocoles[name] = obj
52 break
53 if not self.protocoles:
54 raise ValueError("no protocole constructor found")
55
56 def parse_args(self):
57 """Check command line options"""
58 parser = argparse.ArgumentParser(description=C.DESCRIPTION, formatter_class=argparse.RawDescriptionHelpFormatter)
59
60 parser.add_argument("--version", action="version", version= __version__)
61 default_protocole = C.DEFAULT_PROTOCOLE if C.DEFAULT_PROTOCOLE in self.protocoles else self.protocoles[0]
62 parser.add_argument("-p", "--protocole", choices=sorted(self.protocoles), default=default_protocole,
63 help="generate bridge using PROTOCOLE (default: %(default)s)") # (default: %s, possible values: [%s])" % (DEFAULT_PROTOCOLE, ", ".join(MANAGED_PROTOCOLES)))
64 parser.add_argument("-s", "--side", choices=("core", "frontend"), default="core",
65 help="which side of the bridge do you want to make ?") # (default: %default, possible values: [core, frontend])")
66 default_template = os.path.join(os.path.dirname(bridge_constructor.__file__), 'bridge_template.ini')
67 parser.add_argument("-t", "--template", type=file, default=default_template,
68 help="use TEMPLATE to generate bridge (default: %(default)s)")
69 parser.add_argument("-f", "--force", action="store_true",
70 help=("force overwritting of existing files"))
71 parser.add_argument("-d", "--debug", action="store_true",
72 help=("add debug information printing"))
73 parser.add_argument("--no-unicode", action="store_false", dest="unicode",
74 help=("remove unicode type protection from string results"))
75 parser.add_argument("--flags", nargs='+', default=[],
76 help=("constructors' specific flags"))
77 parser.add_argument("--dest-dir", default=C.DEST_DIR_DEFAULT,
78 help=("directory when the generated files will be written (default: %(default)s)"))
79
80 return parser.parse_args()
81
82 def go(self):
83 self.importConstructors()
84 args = self.parse_args()
85 template_parser = Parser()
86 try:
87 template_parser.readfp(args.template)
88 except IOError:
89 print ("The template file doesn't exist or is not accessible")
90 exit(1)
91 constructor = self.protocoles[args.protocole](template_parser, args)
92 constructor.generate(args.side)
93
94
95 if __name__ == "__main__":
96 bc = BridgeConstructor()
97 bc.go()