annotate sat/bridge/bridge_constructor/base_constructor.py @ 2949:95a4b28645ed

Added tag 0.7.0b1 for changeset c6464685fcbb
author Goffi <goffi@goffi.org>
date Sun, 05 May 2019 20:26:38 +0200
parents 003b8b4b56a7
children ab2696e34d29
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
2 # -*- coding: utf-8 -*-
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SàT: a XMPP client
2771
003b8b4b56a7 date update
Goffi <goffi@goffi.org>
parents: 2628
diff changeset
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org)
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """base constructor class"""
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
21
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from sat.bridge.bridge_constructor.constants import Const as C
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from ConfigParser import NoOptionError
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 import sys
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 import os
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 import os.path
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 import re
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from importlib import import_module
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
29
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
30
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 class ParseError(Exception):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
32 # Used when the signature parsing is going wrong (invalid signature ?)
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 pass
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
34
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
35
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 class Constructor(object):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 NAME = None # used in arguments parsing, filename will be used if not set
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 # following attribute are used by default generation method
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 # they can be set to dict of strings using python formatting syntax
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 # dict keys will be used to select part to replace (e.g. "signals" key will
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 # replace ##SIGNALS_PART## in template), while the value is the format
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 # keys starting with "signal" will be used for signals, while ones starting with
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 # "method" will be used for methods
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
44 #  check D-Bus constructor for an example
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 CORE_FORMATS = None
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 CORE_TEMPLATE = None
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 CORE_DEST = None
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 FRONTEND_FORMATS = None
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 FRONTEND_TEMPLATE = None
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 FRONTEND_DEST = None
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
51
2087
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents: 2085
diff changeset
52 # set to False if your bridge need only core
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents: 2085
diff changeset
53 FRONTEND_ACTIVATE = True
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents: 2085
diff changeset
54
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 def __init__(self, bridge_template, options):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 self.bridge_template = bridge_template
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 self.args = options
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
58
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 @property
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 def constructor_dir(self):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 constructor_mod = import_module(self.__module__)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 return os.path.dirname(constructor_mod.__file__)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
63
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 def getValues(self, name):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 """Return values of a function in a dict
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 @param name: Name of the function to get
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 @return: dict, each key has the config value or None if the value is not set"""
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 function = {}
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
69 for option in ["type", "category", "sig_in", "sig_out", "doc"]:
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 try:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 value = self.bridge_template.get(name, option)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 except NoOptionError:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 value = None
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 function[option] = value
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 return function
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
76
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 def getDefault(self, name):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 """Return default values of a function in a dict
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 @param name: Name of the function to get
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 @return: dict, each key is the integer param number (no key if no default value)"""
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 default_dict = {}
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 def_re = re.compile(r"param_(\d+)_default")
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
83
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 for option in self.bridge_template.options(name):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 match = def_re.match(option)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 if match:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 try:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 idx = int(match.group(1))
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 except ValueError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
90 raise ParseError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
91 "Invalid value [%s] for parameter number" % match.group(1)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
92 )
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 default_dict[idx] = self.bridge_template.get(name, option)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
94
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 return default_dict
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
96
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 def getFlags(self, name):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 """Return list of flags set for this function
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
99
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 @param name: Name of the function to get
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 @return: List of flags (string)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 """
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 flags = []
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 for option in self.bridge_template.options(name):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 if option in C.DECLARATION_FLAGS:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 flags.append(option)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 return flags
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
108
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 def getArgumentsDoc(self, name):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 """Return documentation of arguments
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 @param name: Name of the function to get
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 @return: dict, each key is the integer param number (no key if no argument doc), value is a tuple (name, doc)"""
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 doc_dict = {}
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 option_re = re.compile(r"doc_param_(\d+)")
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 value_re = re.compile(r"^(\w+): (.*)$", re.MULTILINE | re.DOTALL)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 for option in self.bridge_template.options(name):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
117 if option == "doc_return":
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
118 doc_dict["return"] = self.bridge_template.get(name, option)
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 continue
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 match = option_re.match(option)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 if match:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 try:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 idx = int(match.group(1))
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 except ValueError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
125 raise ParseError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
126 "Invalid value [%s] for parameter number" % match.group(1)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
127 )
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 value_match = value_re.match(self.bridge_template.get(name, option))
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 if not value_match:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 raise ParseError("Invalid value for parameter doc [%i]" % idx)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 doc_dict[idx] = (value_match.group(1), value_match.group(2))
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 return doc_dict
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
133
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 def getDoc(self, name):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 """Return documentation of the method
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 @param name: Name of the function to get
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 @return: string documentation, or None"""
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 if self.bridge_template.has_option(name, "doc"):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 return self.bridge_template.get(name, "doc")
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 return None
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
141
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 def argumentsParser(self, signature):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 """Generator which return individual arguments signatures from a global signature"""
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 start = 0
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 i = 0
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
146
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 while i < len(signature):
2628
779351da2c13 core, frontends: replaced org\.goffi namespaces by org.salutatoi + fixed generation:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
148 if signature[i] not in ["b", "y", "n", "i", "x", "q", "u", "t", "d", "s",
779351da2c13 core, frontends: replaced org\.goffi namespaces by org.salutatoi + fixed generation:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
149 "a"]:
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 raise ParseError("Unmanaged attribute type [%c]" % signature[i])
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
151
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
152 if signature[i] == "a":
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 i += 1
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
154 if (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
155 signature[i] != "{" and signature[i] != "("
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
156 ): # FIXME: must manage tuples out of arrays
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 i += 1
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 yield signature[start:i]
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 start = i
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 continue # we have a simple type for the array
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 opening_car = signature[i]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
162 assert opening_car in ["{", "("]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
163 closing_car = "}" if opening_car == "{" else ")"
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 opening_count = 1
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
165 while True: # we have a dict or a list of tuples
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 i += 1
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 if i >= len(signature):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 raise ParseError("missing }")
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 if signature[i] == opening_car:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 opening_count += 1
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 if signature[i] == closing_car:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 opening_count -= 1
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 if opening_count == 0:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 break
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 i += 1
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 yield signature[start:i]
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 start = i
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
178
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 def getArguments(self, signature, name=None, default=None, unicode_protect=False):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 """Return arguments to user given a signature
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
181
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 @param signature: signature in the short form (using s,a,i,b etc)
2087
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents: 2085
diff changeset
183 @param name: dictionary of arguments name like given by getArgumentsDoc
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 @param default: dictionary of default values, like given by getDefault
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 @param unicode_protect: activate unicode protection on strings (return strings as unicode(str))
2091
f413bfc24458 bridge, quick_frontend: preparation for async bridge
Goffi <goffi@goffi.org>
parents: 2087
diff changeset
186 @return (str): arguments that correspond to a signature (e.g.: "sss" return "arg1, arg2, arg3")
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 """
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 idx = 0
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 attr_string = []
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
190
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 for arg in self.argumentsParser(signature):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
192 attr_string.append(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
193 (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
194 "unicode(%(name)s)%(default)s"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
195 if (unicode_protect and arg == "s")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
196 else "%(name)s%(default)s"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
197 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
198 % {
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
199 "name": name[idx][0] if (name and idx in name) else "arg_%i" % idx,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
200 "default": "=" + default[idx] if (default and idx in default) else "",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
201 }
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
202 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
203 # give arg_1, arg2, etc or name1, name2=default, etc.
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
204 # give unicode(arg_1), unicode(arg_2), etc. if unicode_protect is set and arg is a string
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
205 idx += 1
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
206
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
207 return ", ".join(attr_string)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
208
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 def getTemplatePath(self, template_file):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 """return template path corresponding to file name
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
211
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 @param template_file(str): name of template file
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
213 """
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 return os.path.join(self.constructor_dir, template_file)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
215
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 def core_completion_method(self, completion, function, default, arg_doc, async_):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
217 """override this method to extend completion"""
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
218 pass
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
219
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 def core_completion_signal(self, completion, function, default, arg_doc, async_):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
221 """override this method to extend completion"""
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 pass
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
223
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
224 def frontend_completion_method(self, completion, function, default, arg_doc, async_):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 """override this method to extend completion"""
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 pass
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
227
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
228 def frontend_completion_signal(self, completion, function, default, arg_doc, async_):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 """override this method to extend completion"""
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
230 pass
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
231
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
232 def generate(self, side):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
233 """generate bridge
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
234
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
235 call generateCoreSide or generateFrontendSide if they exists
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
236 else call generic self._generate method
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
237 """
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
238 try:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
239 if side == "core":
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
240 method = self.generateCoreSide
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
241 elif side == "frontend":
2087
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents: 2085
diff changeset
242 if not self.FRONTEND_ACTIVATE:
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents: 2085
diff changeset
243 print(u"This constructor only handle core, please use core side")
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents: 2085
diff changeset
244 sys.exit(1)
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
245 method = self.generateFrontendSide
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
246 except AttributeError:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
247 self._generate(side)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
248 else:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
249 method()
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
250
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
251 def _generate(self, side):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
252 """generate the backend
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
253
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
254 this is a generic method which will use formats found in self.CORE_SIGNAL_FORMAT
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
255 and self.CORE_METHOD_FORMAT (standard format method will be used)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
256 @param side(str): core or frontend
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
257 """
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
258 side_vars = []
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
259 for var in ("FORMATS", "TEMPLATE", "DEST"):
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
260 attr = "{}_{}".format(side.upper(), var)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
261 value = getattr(self, attr)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
262 if value is None:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
263 raise NotImplementedError
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
264 side_vars.append(value)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
265
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
266 FORMATS, TEMPLATE, DEST = side_vars
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
267 del side_vars
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
268
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
269 parts = {part.upper(): [] for part in FORMATS}
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
270 sections = self.bridge_template.sections()
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
271 sections.sort()
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
272 for section in sections:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
273 function = self.getValues(section)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
274 print("Adding %s %s" % (section, function["type"]))
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
275 default = self.getDefault(section)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
276 arg_doc = self.getArgumentsDoc(section)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
277 async_ = "async" in self.getFlags(section)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
278 completion = {
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
279 "sig_in": function["sig_in"] or "",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
280 "sig_out": function["sig_out"] or "",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
281 "category": "plugin" if function["category"] == "plugin" else "core",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
282 "name": section,
2087
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents: 2085
diff changeset
283 # arguments with default values
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
284 "args": self.getArguments(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
285 function["sig_in"], name=arg_doc, default=default
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
286 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
287 }
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
288
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
289 extend_method = getattr(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
290 self, "{}_completion_{}".format(side, function["type"])
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
291 )
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
292 extend_method(completion, function, default, arg_doc, async_)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
293
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
294 for part, fmt in FORMATS.iteritems():
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
295 if part.startswith(function["type"]):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
296 parts[part.upper()].append(fmt.format(**completion))
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
297
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
298 # at this point, signals_part, methods_part and direct_calls should be filled,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
299 # we just have to place them in the right part of the template
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
300 bridge = []
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
301 const_override = {
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
302 env[len(C.ENV_OVERRIDE) :]: v
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
303 for env, v in os.environ.iteritems()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
304 if env.startswith(C.ENV_OVERRIDE)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
305 }
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
306 template_path = self.getTemplatePath(TEMPLATE)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
307 try:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
308 with open(template_path) as template:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
309 for line in template:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
310
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
311 for part, extend_list in parts.iteritems():
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
312 if line.startswith("##{}_PART##".format(part)):
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
313 bridge.extend(extend_list)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
314 break
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
315 else:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
316 # the line is not a magic part replacement
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
317 if line.startswith("const_"):
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
318 const_name = line[len("const_") : line.find(" = ")].strip()
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
319 if const_name in const_override:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
320 print("const {} overriden".format(const_name))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
321 bridge.append(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
322 "const_{} = {}".format(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
323 const_name, const_override[const_name]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
324 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
325 )
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
326 continue
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
327 bridge.append(line.replace("\n", ""))
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
328 except IOError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
329 print("can't open template file [{}]".format(template_path))
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
330 sys.exit(1)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
331
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
332 # now we write to final file
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
333 self.finalWrite(DEST, bridge)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
334
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
335 def finalWrite(self, filename, file_buf):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
336 """Write the final generated file in [dest dir]/filename
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
337
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
338 @param filename: name of the file to generate
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
339 @param file_buf: list of lines (stings) of the file
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
340 """
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
341 if os.path.exists(self.args.dest_dir) and not os.path.isdir(self.args.dest_dir):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
342 print(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
343 "The destination dir [%s] can't be created: a file with this name already exists !"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
344 )
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
345 sys.exit(1)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
346 try:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
347 if not os.path.exists(self.args.dest_dir):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
348 os.mkdir(self.args.dest_dir)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
349 full_path = os.path.join(self.args.dest_dir, filename)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
350 if os.path.exists(full_path) and not self.args.force:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
351 print(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
352 "The destination file [%s] already exists ! Use --force to overwrite it"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
353 % full_path
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
354 )
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
355 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
356 with open(full_path, "w") as dest_file:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
357 dest_file.write("\n".join(file_buf))
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
358 except IOError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
359 print("Can't open destination file [%s]" % full_path)
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
360 except OSError:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
361 print("It's not possible to generate the file, check your permissions")
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
362 exit(1)