comparison sat/bridge/bridge_constructor/bridge_constructor.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 26edcf3a30eb
children 003b8b4b56a7
comparison
equal deleted inserted replaced
2623:49533de4540b 2624:56f94936df1e
1 #!/usr/bin/env python2 1 #!/usr/bin/env python2
2 #-*- coding: utf-8 -*- 2 # -*- coding: utf-8 -*-
3 3
4 # SàT: a XMPP client 4 # SàT: a XMPP client
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org) 5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
6 6
7 # This program is free software: you can redistribute it and/or modify 7 # This program is free software: you can redistribute it and/or modify
21 from sat.bridge import bridge_constructor 21 from sat.bridge import bridge_constructor
22 from sat.bridge.bridge_constructor.constants import Const as C 22 from sat.bridge.bridge_constructor.constants import Const as C
23 from sat.bridge.bridge_constructor import constructors, base_constructor 23 from sat.bridge.bridge_constructor import constructors, base_constructor
24 import argparse 24 import argparse
25 from ConfigParser import SafeConfigParser as Parser 25 from ConfigParser import SafeConfigParser as Parser
26 from importlib import import_module 26 from importlib import import_module
27 import os 27 import os
28 import os.path 28 import os.path
29 29
30 #consts 30 # consts
31 __version__ = C.APP_VERSION 31 __version__ = C.APP_VERSION
32 32
33 33
34 class BridgeConstructor(object): 34 class BridgeConstructor(object):
35
36 def importConstructors(self): 35 def importConstructors(self):
37 constructors_dir = os.path.dirname(constructors.__file__) 36 constructors_dir = os.path.dirname(constructors.__file__)
38 self.protocoles = {} 37 self.protocoles = {}
39 for dir_ in os.listdir(constructors_dir): 38 for dir_ in os.listdir(constructors_dir):
40 init_path = os.path.join(constructors_dir, dir_, '__init__.py') 39 init_path = os.path.join(constructors_dir, dir_, "__init__.py")
41 constructor_path = os.path.join(constructors_dir, dir_, 'constructor.py') 40 constructor_path = os.path.join(constructors_dir, dir_, "constructor.py")
42 module_path = "sat.bridge.bridge_constructor.constructors.{}.constructor".format(dir_) 41 module_path = "sat.bridge.bridge_constructor.constructors.{}.constructor".format(
42 dir_
43 )
43 if os.path.isfile(init_path) and os.path.isfile(constructor_path): 44 if os.path.isfile(init_path) and os.path.isfile(constructor_path):
44 mod = import_module(module_path) 45 mod = import_module(module_path)
45 for attr in dir(mod): 46 for attr in dir(mod):
46 obj = getattr(mod, attr) 47 obj = getattr(mod, attr)
47 if not isinstance(obj, type): 48 if not isinstance(obj, type):
53 if not self.protocoles: 54 if not self.protocoles:
54 raise ValueError("no protocole constructor found") 55 raise ValueError("no protocole constructor found")
55 56
56 def parse_args(self): 57 def parse_args(self):
57 """Check command line options""" 58 """Check command line options"""
58 parser = argparse.ArgumentParser(description=C.DESCRIPTION, formatter_class=argparse.RawDescriptionHelpFormatter) 59 parser = argparse.ArgumentParser(
60 description=C.DESCRIPTION,
61 formatter_class=argparse.RawDescriptionHelpFormatter,
62 )
59 63
60 parser.add_argument("--version", action="version", version= __version__) 64 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] 65 default_protocole = (
62 parser.add_argument("-p", "--protocole", choices=sorted(self.protocoles), default=default_protocole, 66 C.DEFAULT_PROTOCOLE
63 help="generate bridge using PROTOCOLE (default: %(default)s)") # (default: %s, possible values: [%s])" % (DEFAULT_PROTOCOLE, ", ".join(MANAGED_PROTOCOLES))) 67 if C.DEFAULT_PROTOCOLE in self.protocoles
64 parser.add_argument("-s", "--side", choices=("core", "frontend"), default="core", 68 else self.protocoles[0]
65 help="which side of the bridge do you want to make ?") # (default: %default, possible values: [core, frontend])") 69 )
66 default_template = os.path.join(os.path.dirname(bridge_constructor.__file__), 'bridge_template.ini') 70 parser.add_argument(
67 parser.add_argument("-t", "--template", type=file, default=default_template, 71 "-p",
68 help="use TEMPLATE to generate bridge (default: %(default)s)") 72 "--protocole",
69 parser.add_argument("-f", "--force", action="store_true", 73 choices=sorted(self.protocoles),
70 help=("force overwritting of existing files")) 74 default=default_protocole,
71 parser.add_argument("-d", "--debug", action="store_true", 75 help="generate bridge using PROTOCOLE (default: %(default)s)",
72 help=("add debug information printing")) 76 ) # (default: %s, possible values: [%s])" % (DEFAULT_PROTOCOLE, ", ".join(MANAGED_PROTOCOLES)))
73 parser.add_argument("--no-unicode", action="store_false", dest="unicode", 77 parser.add_argument(
74 help=("remove unicode type protection from string results")) 78 "-s",
75 parser.add_argument("--flags", nargs='+', default=[], 79 "--side",
76 help=("constructors' specific flags")) 80 choices=("core", "frontend"),
77 parser.add_argument("--dest-dir", default=C.DEST_DIR_DEFAULT, 81 default="core",
78 help=("directory when the generated files will be written (default: %(default)s)")) 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=file,
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 )
79 119
80 return parser.parse_args() 120 return parser.parse_args()
81 121
82 def go(self): 122 def go(self):
83 self.importConstructors() 123 self.importConstructors()
84 args = self.parse_args() 124 args = self.parse_args()
85 template_parser = Parser() 125 template_parser = Parser()
86 try: 126 try:
87 template_parser.readfp(args.template) 127 template_parser.readfp(args.template)
88 except IOError: 128 except IOError:
89 print ("The template file doesn't exist or is not accessible") 129 print("The template file doesn't exist or is not accessible")
90 exit(1) 130 exit(1)
91 constructor = self.protocoles[args.protocole](template_parser, args) 131 constructor = self.protocoles[args.protocole](template_parser, args)
92 constructor.generate(args.side) 132 constructor.generate(args.side)
93 133
94 134