refactoring: renamed sat package to libervia.backend
This commit is contained in:
parent
a15648d682
commit
d2685ba11b
24 changed files with 1293 additions and 1293 deletions
0
libervia/backend/bridge/bridge_constructor/__init__.py
Normal file
0
libervia/backend/bridge/bridge_constructor/__init__.py
Normal file
364
libervia/backend/bridge/bridge_constructor/base_constructor.py
Normal file
364
libervia/backend/bridge/bridge_constructor/base_constructor.py
Normal file
|
|
@ -0,0 +1,364 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
|
||||
# Libervia: an XMPP client
|
||||
# Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
|
||||
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
"""base constructor class"""
|
||||
|
||||
from libervia.backend.bridge.bridge_constructor.constants import Const as C
|
||||
from configparser import NoOptionError
|
||||
import sys
|
||||
import os
|
||||
import os.path
|
||||
import re
|
||||
from importlib import import_module
|
||||
|
||||
|
||||
class ParseError(Exception):
|
||||
# Used when the signature parsing is going wrong (invalid signature ?)
|
||||
pass
|
||||
|
||||
|
||||
class Constructor(object):
|
||||
NAME = None # used in arguments parsing, filename will be used if not set
|
||||
# following attribute are used by default generation method
|
||||
# they can be set to dict of strings using python formatting syntax
|
||||
# dict keys will be used to select part to replace (e.g. "signals" key will
|
||||
# replace ##SIGNALS_PART## in template), while the value is the format
|
||||
# keys starting with "signal" will be used for signals, while ones starting with
|
||||
# "method" will be used for methods
|
||||
# check D-Bus constructor for an example
|
||||
CORE_FORMATS = None
|
||||
CORE_TEMPLATE = None
|
||||
CORE_DEST = None
|
||||
FRONTEND_FORMATS = None
|
||||
FRONTEND_TEMPLATE = None
|
||||
FRONTEND_DEST = None
|
||||
|
||||
# set to False if your bridge needs only core
|
||||
FRONTEND_ACTIVATE = True
|
||||
|
||||
def __init__(self, bridge_template, options):
|
||||
self.bridge_template = bridge_template
|
||||
self.args = options
|
||||
|
||||
@property
|
||||
def constructor_dir(self):
|
||||
constructor_mod = import_module(self.__module__)
|
||||
return os.path.dirname(constructor_mod.__file__)
|
||||
|
||||
def getValues(self, name):
|
||||
"""Return values of a function in a dict
|
||||
@param name: Name of the function to get
|
||||
@return: dict, each key has the config value or None if the value is not set"""
|
||||
function = {}
|
||||
for option in ["type", "category", "sig_in", "sig_out", "doc"]:
|
||||
try:
|
||||
value = self.bridge_template.get(name, option)
|
||||
except NoOptionError:
|
||||
value = None
|
||||
function[option] = value
|
||||
return function
|
||||
|
||||
def get_default(self, name):
|
||||
"""Return default values of a function in a dict
|
||||
@param name: Name of the function to get
|
||||
@return: dict, each key is the integer param number (no key if no default value)"""
|
||||
default_dict = {}
|
||||
def_re = re.compile(r"param_(\d+)_default")
|
||||
|
||||
for option in self.bridge_template.options(name):
|
||||
match = def_re.match(option)
|
||||
if match:
|
||||
try:
|
||||
idx = int(match.group(1))
|
||||
except ValueError:
|
||||
raise ParseError(
|
||||
"Invalid value [%s] for parameter number" % match.group(1)
|
||||
)
|
||||
default_dict[idx] = self.bridge_template.get(name, option)
|
||||
|
||||
return default_dict
|
||||
|
||||
def getFlags(self, name):
|
||||
"""Return list of flags set for this function
|
||||
|
||||
@param name: Name of the function to get
|
||||
@return: List of flags (string)
|
||||
"""
|
||||
flags = []
|
||||
for option in self.bridge_template.options(name):
|
||||
if option in C.DECLARATION_FLAGS:
|
||||
flags.append(option)
|
||||
return flags
|
||||
|
||||
def get_arguments_doc(self, name):
|
||||
"""Return documentation of arguments
|
||||
@param name: Name of the function to get
|
||||
@return: dict, each key is the integer param number (no key if no argument doc), value is a tuple (name, doc)"""
|
||||
doc_dict = {}
|
||||
option_re = re.compile(r"doc_param_(\d+)")
|
||||
value_re = re.compile(r"^(\w+): (.*)$", re.MULTILINE | re.DOTALL)
|
||||
for option in self.bridge_template.options(name):
|
||||
if option == "doc_return":
|
||||
doc_dict["return"] = self.bridge_template.get(name, option)
|
||||
continue
|
||||
match = option_re.match(option)
|
||||
if match:
|
||||
try:
|
||||
idx = int(match.group(1))
|
||||
except ValueError:
|
||||
raise ParseError(
|
||||
"Invalid value [%s] for parameter number" % match.group(1)
|
||||
)
|
||||
value_match = value_re.match(self.bridge_template.get(name, option))
|
||||
if not value_match:
|
||||
raise ParseError("Invalid value for parameter doc [%i]" % idx)
|
||||
doc_dict[idx] = (value_match.group(1), value_match.group(2))
|
||||
return doc_dict
|
||||
|
||||
def get_doc(self, name):
|
||||
"""Return documentation of the method
|
||||
@param name: Name of the function to get
|
||||
@return: string documentation, or None"""
|
||||
if self.bridge_template.has_option(name, "doc"):
|
||||
return self.bridge_template.get(name, "doc")
|
||||
return None
|
||||
|
||||
def arguments_parser(self, signature):
|
||||
"""Generator which return individual arguments signatures from a global signature"""
|
||||
start = 0
|
||||
i = 0
|
||||
|
||||
while i < len(signature):
|
||||
if signature[i] not in ["b", "y", "n", "i", "x", "q", "u", "t", "d", "s",
|
||||
"a"]:
|
||||
raise ParseError("Unmanaged attribute type [%c]" % signature[i])
|
||||
|
||||
if signature[i] == "a":
|
||||
i += 1
|
||||
if (
|
||||
signature[i] != "{" and signature[i] != "("
|
||||
): # FIXME: must manage tuples out of arrays
|
||||
i += 1
|
||||
yield signature[start:i]
|
||||
start = i
|
||||
continue # we have a simple type for the array
|
||||
opening_car = signature[i]
|
||||
assert opening_car in ["{", "("]
|
||||
closing_car = "}" if opening_car == "{" else ")"
|
||||
opening_count = 1
|
||||
while True: # we have a dict or a list of tuples
|
||||
i += 1
|
||||
if i >= len(signature):
|
||||
raise ParseError("missing }")
|
||||
if signature[i] == opening_car:
|
||||
opening_count += 1
|
||||
if signature[i] == closing_car:
|
||||
opening_count -= 1
|
||||
if opening_count == 0:
|
||||
break
|
||||
i += 1
|
||||
yield signature[start:i]
|
||||
start = i
|
||||
|
||||
def get_arguments(self, signature, name=None, default=None, unicode_protect=False):
|
||||
"""Return arguments to user given a signature
|
||||
|
||||
@param signature: signature in the short form (using s,a,i,b etc)
|
||||
@param name: dictionary of arguments name like given by get_arguments_doc
|
||||
@param default: dictionary of default values, like given by get_default
|
||||
@param unicode_protect: activate unicode protection on strings (return strings as unicode(str))
|
||||
@return (str): arguments that correspond to a signature (e.g.: "sss" return "arg1, arg2, arg3")
|
||||
"""
|
||||
idx = 0
|
||||
attr_string = []
|
||||
|
||||
for arg in self.arguments_parser(signature):
|
||||
attr_string.append(
|
||||
(
|
||||
"str(%(name)s)%(default)s"
|
||||
if (unicode_protect and arg == "s")
|
||||
else "%(name)s%(default)s"
|
||||
)
|
||||
% {
|
||||
"name": name[idx][0] if (name and idx in name) else "arg_%i" % idx,
|
||||
"default": "=" + default[idx] if (default and idx in default) else "",
|
||||
}
|
||||
)
|
||||
# give arg_1, arg2, etc or name1, name2=default, etc.
|
||||
# give unicode(arg_1), unicode(arg_2), etc. if unicode_protect is set and arg is a string
|
||||
idx += 1
|
||||
|
||||
return ", ".join(attr_string)
|
||||
|
||||
def get_template_path(self, template_file):
|
||||
"""return template path corresponding to file name
|
||||
|
||||
@param template_file(str): name of template file
|
||||
"""
|
||||
return os.path.join(self.constructor_dir, template_file)
|
||||
|
||||
def core_completion_method(self, completion, function, default, arg_doc, async_):
|
||||
"""override this method to extend completion"""
|
||||
pass
|
||||
|
||||
def core_completion_signal(self, completion, function, default, arg_doc, async_):
|
||||
"""override this method to extend completion"""
|
||||
pass
|
||||
|
||||
def frontend_completion_method(self, completion, function, default, arg_doc, async_):
|
||||
"""override this method to extend completion"""
|
||||
pass
|
||||
|
||||
def frontend_completion_signal(self, completion, function, default, arg_doc, async_):
|
||||
"""override this method to extend completion"""
|
||||
pass
|
||||
|
||||
def generate(self, side):
|
||||
"""generate bridge
|
||||
|
||||
call generate_core_side or generateFrontendSide if they exists
|
||||
else call generic self._generate method
|
||||
"""
|
||||
try:
|
||||
if side == "core":
|
||||
method = self.generate_core_side
|
||||
elif side == "frontend":
|
||||
if not self.FRONTEND_ACTIVATE:
|
||||
print("This constructor only handle core, please use core side")
|
||||
sys.exit(1)
|
||||
method = self.generateFrontendSide
|
||||
except AttributeError:
|
||||
self._generate(side)
|
||||
else:
|
||||
method()
|
||||
|
||||
def _generate(self, side):
|
||||
"""generate the backend
|
||||
|
||||
this is a generic method which will use formats found in self.CORE_SIGNAL_FORMAT
|
||||
and self.CORE_METHOD_FORMAT (standard format method will be used)
|
||||
@param side(str): core or frontend
|
||||
"""
|
||||
side_vars = []
|
||||
for var in ("FORMATS", "TEMPLATE", "DEST"):
|
||||
attr = "{}_{}".format(side.upper(), var)
|
||||
value = getattr(self, attr)
|
||||
if value is None:
|
||||
raise NotImplementedError
|
||||
side_vars.append(value)
|
||||
|
||||
FORMATS, TEMPLATE, DEST = side_vars
|
||||
del side_vars
|
||||
|
||||
parts = {part.upper(): [] for part in FORMATS}
|
||||
sections = self.bridge_template.sections()
|
||||
sections.sort()
|
||||
for section in sections:
|
||||
function = self.getValues(section)
|
||||
print(("Adding %s %s" % (section, function["type"])))
|
||||
default = self.get_default(section)
|
||||
arg_doc = self.get_arguments_doc(section)
|
||||
async_ = "async" in self.getFlags(section)
|
||||
completion = {
|
||||
"sig_in": function["sig_in"] or "",
|
||||
"sig_out": function["sig_out"] or "",
|
||||
"category": "plugin" if function["category"] == "plugin" else "core",
|
||||
"name": section,
|
||||
# arguments with default values
|
||||
"args": self.get_arguments(
|
||||
function["sig_in"], name=arg_doc, default=default
|
||||
),
|
||||
"args_no_default": self.get_arguments(function["sig_in"], name=arg_doc),
|
||||
}
|
||||
|
||||
extend_method = getattr(
|
||||
self, "{}_completion_{}".format(side, function["type"])
|
||||
)
|
||||
extend_method(completion, function, default, arg_doc, async_)
|
||||
|
||||
for part, fmt in FORMATS.items():
|
||||
if (part.startswith(function["type"])
|
||||
or part.startswith(f"async_{function['type']}")):
|
||||
parts[part.upper()].append(fmt.format(**completion))
|
||||
|
||||
# at this point, signals_part, methods_part and direct_calls should be filled,
|
||||
# we just have to place them in the right part of the template
|
||||
bridge = []
|
||||
const_override = {
|
||||
env[len(C.ENV_OVERRIDE) :]: v
|
||||
for env, v in os.environ.items()
|
||||
if env.startswith(C.ENV_OVERRIDE)
|
||||
}
|
||||
template_path = self.get_template_path(TEMPLATE)
|
||||
try:
|
||||
with open(template_path) as template:
|
||||
for line in template:
|
||||
|
||||
for part, extend_list in parts.items():
|
||||
if line.startswith("##{}_PART##".format(part)):
|
||||
bridge.extend(extend_list)
|
||||
break
|
||||
else:
|
||||
# the line is not a magic part replacement
|
||||
if line.startswith("const_"):
|
||||
const_name = line[len("const_") : line.find(" = ")].strip()
|
||||
if const_name in const_override:
|
||||
print(("const {} overriden".format(const_name)))
|
||||
bridge.append(
|
||||
"const_{} = {}".format(
|
||||
const_name, const_override[const_name]
|
||||
)
|
||||
)
|
||||
continue
|
||||
bridge.append(line.replace("\n", ""))
|
||||
except IOError:
|
||||
print(("can't open template file [{}]".format(template_path)))
|
||||
sys.exit(1)
|
||||
|
||||
# now we write to final file
|
||||
self.final_write(DEST, bridge)
|
||||
|
||||
def final_write(self, filename, file_buf):
|
||||
"""Write the final generated file in [dest dir]/filename
|
||||
|
||||
@param filename: name of the file to generate
|
||||
@param file_buf: list of lines (stings) of the file
|
||||
"""
|
||||
if os.path.exists(self.args.dest_dir) and not os.path.isdir(self.args.dest_dir):
|
||||
print(
|
||||
"The destination dir [%s] can't be created: a file with this name already exists !"
|
||||
)
|
||||
sys.exit(1)
|
||||
try:
|
||||
if not os.path.exists(self.args.dest_dir):
|
||||
os.mkdir(self.args.dest_dir)
|
||||
full_path = os.path.join(self.args.dest_dir, filename)
|
||||
if os.path.exists(full_path) and not self.args.force:
|
||||
print((
|
||||
"The destination file [%s] already exists ! Use --force to overwrite it"
|
||||
% full_path
|
||||
))
|
||||
try:
|
||||
with open(full_path, "w") as dest_file:
|
||||
dest_file.write("\n".join(file_buf))
|
||||
except IOError:
|
||||
print(("Can't open destination file [%s]" % full_path))
|
||||
except OSError:
|
||||
print("It's not possible to generate the file, check your permissions")
|
||||
exit(1)
|
||||
137
libervia/backend/bridge/bridge_constructor/bridge_constructor.py
Executable file
137
libervia/backend/bridge/bridge_constructor/bridge_constructor.py
Executable file
|
|
@ -0,0 +1,137 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
|
||||
# Libervia: an XMPP client
|
||||
# Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
|
||||
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
from libervia.backend.bridge import bridge_constructor
|
||||
from libervia.backend.bridge.bridge_constructor.constants import Const as C
|
||||
from libervia.backend.bridge.bridge_constructor import constructors, base_constructor
|
||||
import argparse
|
||||
from configparser import ConfigParser as Parser
|
||||
from importlib import import_module
|
||||
import os
|
||||
import os.path
|
||||
|
||||
# consts
|
||||
__version__ = C.APP_VERSION
|
||||
|
||||
|
||||
class BridgeConstructor(object):
|
||||
def import_constructors(self):
|
||||
constructors_dir = os.path.dirname(constructors.__file__)
|
||||
self.protocoles = {}
|
||||
for dir_ in os.listdir(constructors_dir):
|
||||
init_path = os.path.join(constructors_dir, dir_, "__init__.py")
|
||||
constructor_path = os.path.join(constructors_dir, dir_, "constructor.py")
|
||||
module_path = "libervia.backend.bridge.bridge_constructor.constructors.{}.constructor".format(
|
||||
dir_
|
||||
)
|
||||
if os.path.isfile(init_path) and os.path.isfile(constructor_path):
|
||||
mod = import_module(module_path)
|
||||
for attr in dir(mod):
|
||||
obj = getattr(mod, attr)
|
||||
if not isinstance(obj, type):
|
||||
continue
|
||||
if issubclass(obj, base_constructor.Constructor):
|
||||
name = obj.NAME or dir_
|
||||
self.protocoles[name] = obj
|
||||
break
|
||||
if not self.protocoles:
|
||||
raise ValueError("no protocole constructor found")
|
||||
|
||||
def parse_args(self):
|
||||
"""Check command line options"""
|
||||
parser = argparse.ArgumentParser(
|
||||
description=C.DESCRIPTION,
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter,
|
||||
)
|
||||
|
||||
parser.add_argument("--version", action="version", version=__version__)
|
||||
default_protocole = (
|
||||
C.DEFAULT_PROTOCOLE
|
||||
if C.DEFAULT_PROTOCOLE in self.protocoles
|
||||
else self.protocoles[0]
|
||||
)
|
||||
parser.add_argument(
|
||||
"-p",
|
||||
"--protocole",
|
||||
choices=sorted(self.protocoles),
|
||||
default=default_protocole,
|
||||
help="generate bridge using PROTOCOLE (default: %(default)s)",
|
||||
) # (default: %s, possible values: [%s])" % (DEFAULT_PROTOCOLE, ", ".join(MANAGED_PROTOCOLES)))
|
||||
parser.add_argument(
|
||||
"-s",
|
||||
"--side",
|
||||
choices=("core", "frontend"),
|
||||
default="core",
|
||||
help="which side of the bridge do you want to make ?",
|
||||
) # (default: %default, possible values: [core, frontend])")
|
||||
default_template = os.path.join(
|
||||
os.path.dirname(bridge_constructor.__file__), "bridge_template.ini"
|
||||
)
|
||||
parser.add_argument(
|
||||
"-t",
|
||||
"--template",
|
||||
type=argparse.FileType(),
|
||||
default=default_template,
|
||||
help="use TEMPLATE to generate bridge (default: %(default)s)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-f",
|
||||
"--force",
|
||||
action="store_true",
|
||||
help=("force overwritting of existing files"),
|
||||
)
|
||||
parser.add_argument(
|
||||
"-d", "--debug", action="store_true", help=("add debug information printing")
|
||||
)
|
||||
parser.add_argument(
|
||||
"--no-unicode",
|
||||
action="store_false",
|
||||
dest="unicode",
|
||||
help=("remove unicode type protection from string results"),
|
||||
)
|
||||
parser.add_argument(
|
||||
"--flags", nargs="+", default=[], help=("constructors' specific flags")
|
||||
)
|
||||
parser.add_argument(
|
||||
"--dest-dir",
|
||||
default=C.DEST_DIR_DEFAULT,
|
||||
help=(
|
||||
"directory when the generated files will be written (default: %(default)s)"
|
||||
),
|
||||
)
|
||||
|
||||
return parser.parse_args()
|
||||
|
||||
def go(self):
|
||||
self.import_constructors()
|
||||
args = self.parse_args()
|
||||
template_parser = Parser()
|
||||
try:
|
||||
template_parser.read_file(args.template)
|
||||
except IOError:
|
||||
print("The template file doesn't exist or is not accessible")
|
||||
exit(1)
|
||||
constructor = self.protocoles[args.protocole](template_parser, args)
|
||||
constructor.generate(args.side)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
bc = BridgeConstructor()
|
||||
bc.go()
|
||||
1026
libervia/backend/bridge/bridge_constructor/bridge_template.ini
Normal file
1026
libervia/backend/bridge/bridge_constructor/bridge_template.ini
Normal file
File diff suppressed because it is too large
Load diff
43
libervia/backend/bridge/bridge_constructor/constants.py
Normal file
43
libervia/backend/bridge/bridge_constructor/constants.py
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
|
||||
# Libervia: an XMPP client
|
||||
# Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
|
||||
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from libervia.backend.core import constants
|
||||
|
||||
|
||||
class Const(constants.Const):
|
||||
|
||||
NAME = "bridge_constructor"
|
||||
DEST_DIR_DEFAULT = "generated"
|
||||
DESCRIPTION = """{name} Copyright (C) 2009-2021 Jérôme Poisson (aka Goffi)
|
||||
|
||||
This script construct a SàT bridge using the given protocol
|
||||
|
||||
This program comes with ABSOLUTELY NO WARRANTY;
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions.
|
||||
""".format(
|
||||
name=NAME, version=constants.Const.APP_VERSION
|
||||
)
|
||||
# TODO: move protocoles in separate files (plugins?)
|
||||
DEFAULT_PROTOCOLE = "dbus"
|
||||
|
||||
# flags used method/signal declaration (not to be confused with constructor flags)
|
||||
DECLARATION_FLAGS = ["deprecated", "async"]
|
||||
|
||||
ENV_OVERRIDE = "SAT_BRIDGE_CONST_" # Prefix used to override a constant
|
||||
|
|
@ -0,0 +1,102 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
|
||||
# Libervia: an XMPP client
|
||||
# Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
|
||||
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from libervia.backend.bridge.bridge_constructor import base_constructor
|
||||
from xml.dom import minidom
|
||||
import sys
|
||||
|
||||
|
||||
class DbusXmlConstructor(base_constructor.Constructor):
|
||||
"""Constructor for DBus XML syntaxt (used by Qt frontend)"""
|
||||
|
||||
def __init__(self, bridge_template, options):
|
||||
base_constructor.Constructor.__init__(self, bridge_template, options)
|
||||
|
||||
self.template = "dbus_xml_template.xml"
|
||||
self.core_dest = "org.libervia.sat.xml"
|
||||
self.default_annotation = {
|
||||
"a{ss}": "StringDict",
|
||||
"a(sa{ss}as)": "QList<Contact>",
|
||||
"a{i(ss)}": "HistoryT",
|
||||
"a(sss)": "QList<MenuT>",
|
||||
"a{sa{s(sia{ss})}}": "PresenceStatusT",
|
||||
}
|
||||
|
||||
def generate_core_side(self):
|
||||
try:
|
||||
doc = minidom.parse(self.get_template_path(self.template))
|
||||
interface_elt = doc.getElementsByTagName("interface")[0]
|
||||
except IOError:
|
||||
print("Can't access template")
|
||||
sys.exit(1)
|
||||
except IndexError:
|
||||
print("Template error")
|
||||
sys.exit(1)
|
||||
|
||||
sections = self.bridge_template.sections()
|
||||
sections.sort()
|
||||
for section in sections:
|
||||
function = self.getValues(section)
|
||||
print(("Adding %s %s" % (section, function["type"])))
|
||||
new_elt = doc.createElement(
|
||||
"method" if function["type"] == "method" else "signal"
|
||||
)
|
||||
new_elt.setAttribute("name", section)
|
||||
|
||||
idx = 0
|
||||
args_doc = self.get_arguments_doc(section)
|
||||
for arg in self.arguments_parser(function["sig_in"] or ""):
|
||||
arg_elt = doc.createElement("arg")
|
||||
arg_elt.setAttribute(
|
||||
"name", args_doc[idx][0] if idx in args_doc else "arg_%i" % idx
|
||||
)
|
||||
arg_elt.setAttribute("type", arg)
|
||||
_direction = "in" if function["type"] == "method" else "out"
|
||||
arg_elt.setAttribute("direction", _direction)
|
||||
new_elt.appendChild(arg_elt)
|
||||
if "annotation" in self.args.flags:
|
||||
if arg in self.default_annotation:
|
||||
annot_elt = doc.createElement("annotation")
|
||||
annot_elt.setAttribute(
|
||||
"name", "com.trolltech.QtDBus.QtTypeName.In%d" % idx
|
||||
)
|
||||
annot_elt.setAttribute("value", self.default_annotation[arg])
|
||||
new_elt.appendChild(annot_elt)
|
||||
idx += 1
|
||||
|
||||
if function["sig_out"]:
|
||||
arg_elt = doc.createElement("arg")
|
||||
arg_elt.setAttribute("type", function["sig_out"])
|
||||
arg_elt.setAttribute("direction", "out")
|
||||
new_elt.appendChild(arg_elt)
|
||||
if "annotation" in self.args.flags:
|
||||
if function["sig_out"] in self.default_annotation:
|
||||
annot_elt = doc.createElement("annotation")
|
||||
annot_elt.setAttribute(
|
||||
"name", "com.trolltech.QtDBus.QtTypeName.Out0"
|
||||
)
|
||||
annot_elt.setAttribute(
|
||||
"value", self.default_annotation[function["sig_out"]]
|
||||
)
|
||||
new_elt.appendChild(annot_elt)
|
||||
|
||||
interface_elt.appendChild(new_elt)
|
||||
|
||||
# now we write to final file
|
||||
self.final_write(self.core_dest, [doc.toprettyxml()])
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
<node>
|
||||
<interface name="org.libervia.Libervia.core">
|
||||
</interface>
|
||||
</node>
|
||||
|
|
@ -0,0 +1,118 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
|
||||
# Libervia: an XMPP client
|
||||
# Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
|
||||
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from libervia.backend.bridge.bridge_constructor import base_constructor
|
||||
|
||||
|
||||
class DbusConstructor(base_constructor.Constructor):
|
||||
NAME = "dbus"
|
||||
CORE_TEMPLATE = "dbus_core_template.py"
|
||||
CORE_DEST = "dbus_bridge.py"
|
||||
CORE_FORMATS = {
|
||||
"methods_declarations": """\
|
||||
Method('{name}', arguments='{sig_in}', returns='{sig_out}'),""",
|
||||
|
||||
"methods": """\
|
||||
def dbus_{name}(self, {args}):
|
||||
{debug}return self._callback("{name}", {args_no_default})\n""",
|
||||
|
||||
"signals_declarations": """\
|
||||
Signal('{name}', '{sig_in}'),""",
|
||||
|
||||
"signals": """\
|
||||
def {name}(self, {args}):
|
||||
self._obj.emitSignal("{name}", {args})\n""",
|
||||
}
|
||||
|
||||
FRONTEND_TEMPLATE = "dbus_frontend_template.py"
|
||||
FRONTEND_DEST = CORE_DEST
|
||||
FRONTEND_FORMATS = {
|
||||
"methods": """\
|
||||
def {name}(self, {args}{async_comma}{async_args}):
|
||||
{error_handler}{blocking_call}{debug}return {result}\n""",
|
||||
"async_methods": """\
|
||||
def {name}(self{async_comma}{args}):
|
||||
loop = asyncio.get_running_loop()
|
||||
fut = loop.create_future()
|
||||
reply_handler = lambda ret=None: loop.call_soon_threadsafe(fut.set_result, ret)
|
||||
error_handler = lambda err: loop.call_soon_threadsafe(fut.set_exception, dbus_to_bridge_exception(err))
|
||||
self.db_{category}_iface.{name}({args_result}{async_comma}timeout=const_TIMEOUT, reply_handler=reply_handler, error_handler=error_handler)
|
||||
{debug}return fut\n""",
|
||||
}
|
||||
|
||||
def core_completion_signal(self, completion, function, default, arg_doc, async_):
|
||||
completion["category"] = completion["category"].upper()
|
||||
completion["body"] = (
|
||||
"pass"
|
||||
if not self.args.debug
|
||||
else 'log.debug ("{}")'.format(completion["name"])
|
||||
)
|
||||
|
||||
def core_completion_method(self, completion, function, default, arg_doc, async_):
|
||||
completion.update(
|
||||
{
|
||||
"debug": (
|
||||
"" if not self.args.debug
|
||||
else f'log.debug ("{completion["name"]}")\n{8 * " "}'
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
def frontend_completion_method(self, completion, function, default, arg_doc, async_):
|
||||
completion.update(
|
||||
{
|
||||
# XXX: we can manage blocking call in the same way as async one: if callback is None the call will be blocking
|
||||
"debug": ""
|
||||
if not self.args.debug
|
||||
else 'log.debug ("%s")\n%s' % (completion["name"], 8 * " "),
|
||||
"args_result": self.get_arguments(function["sig_in"], name=arg_doc),
|
||||
"async_args": "callback=None, errback=None",
|
||||
"async_comma": ", " if function["sig_in"] else "",
|
||||
"error_handler": """if callback is None:
|
||||
error_handler = None
|
||||
else:
|
||||
if errback is None:
|
||||
errback = log.error
|
||||
error_handler = lambda err:errback(dbus_to_bridge_exception(err))
|
||||
""",
|
||||
}
|
||||
)
|
||||
if async_:
|
||||
completion["blocking_call"] = ""
|
||||
completion[
|
||||
"async_args_result"
|
||||
] = "timeout=const_TIMEOUT, reply_handler=callback, error_handler=error_handler"
|
||||
else:
|
||||
# XXX: To have a blocking call, we must have not reply_handler, so we test if callback exists, and add reply_handler only in this case
|
||||
completion[
|
||||
"blocking_call"
|
||||
] = """kwargs={}
|
||||
if callback is not None:
|
||||
kwargs['timeout'] = const_TIMEOUT
|
||||
kwargs['reply_handler'] = callback
|
||||
kwargs['error_handler'] = error_handler
|
||||
"""
|
||||
completion["async_args_result"] = "**kwargs"
|
||||
result = (
|
||||
"self.db_%(category)s_iface.%(name)s(%(args_result)s%(async_comma)s%(async_args_result)s)"
|
||||
% completion
|
||||
)
|
||||
completion["result"] = (
|
||||
"str(%s)" if self.args.unicode and function["sig_out"] == "s" else "%s"
|
||||
) % result
|
||||
|
|
@ -0,0 +1,171 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
# Libervia communication bridge
|
||||
# Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
|
||||
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from types import MethodType
|
||||
from functools import partialmethod
|
||||
from twisted.internet import defer, reactor
|
||||
from libervia.backend.core.i18n import _
|
||||
from libervia.backend.core.log import getLogger
|
||||
from libervia.backend.core.exceptions import BridgeInitError
|
||||
from libervia.backend.tools import config
|
||||
from txdbus import client, objects, error
|
||||
from txdbus.interface import DBusInterface, Method, Signal
|
||||
|
||||
|
||||
log = getLogger(__name__)
|
||||
|
||||
# Interface prefix
|
||||
const_INT_PREFIX = config.config_get(
|
||||
config.parse_main_conf(),
|
||||
"",
|
||||
"bridge_dbus_int_prefix",
|
||||
"org.libervia.Libervia")
|
||||
const_ERROR_PREFIX = const_INT_PREFIX + ".error"
|
||||
const_OBJ_PATH = "/org/libervia/Libervia/bridge"
|
||||
const_CORE_SUFFIX = ".core"
|
||||
const_PLUGIN_SUFFIX = ".plugin"
|
||||
|
||||
|
||||
class ParseError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class DBusException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class MethodNotRegistered(DBusException):
|
||||
dbusErrorName = const_ERROR_PREFIX + ".MethodNotRegistered"
|
||||
|
||||
|
||||
class GenericException(DBusException):
|
||||
def __init__(self, twisted_error):
|
||||
"""
|
||||
|
||||
@param twisted_error (Failure): instance of twisted Failure
|
||||
error message is used to store a repr of message and condition in a tuple,
|
||||
so it can be evaluated by the frontend bridge.
|
||||
"""
|
||||
try:
|
||||
# twisted_error.value is a class
|
||||
class_ = twisted_error.value().__class__
|
||||
except TypeError:
|
||||
# twisted_error.value is an instance
|
||||
class_ = twisted_error.value.__class__
|
||||
data = twisted_error.getErrorMessage()
|
||||
try:
|
||||
data = (data, twisted_error.value.condition)
|
||||
except AttributeError:
|
||||
data = (data,)
|
||||
else:
|
||||
data = (str(twisted_error),)
|
||||
self.dbusErrorName = ".".join(
|
||||
(const_ERROR_PREFIX, class_.__module__, class_.__name__)
|
||||
)
|
||||
super(GenericException, self).__init__(repr(data))
|
||||
|
||||
@classmethod
|
||||
def create_and_raise(cls, exc):
|
||||
raise cls(exc)
|
||||
|
||||
|
||||
class DBusObject(objects.DBusObject):
|
||||
|
||||
core_iface = DBusInterface(
|
||||
const_INT_PREFIX + const_CORE_SUFFIX,
|
||||
##METHODS_DECLARATIONS_PART##
|
||||
##SIGNALS_DECLARATIONS_PART##
|
||||
)
|
||||
plugin_iface = DBusInterface(
|
||||
const_INT_PREFIX + const_PLUGIN_SUFFIX
|
||||
)
|
||||
|
||||
dbusInterfaces = [core_iface, plugin_iface]
|
||||
|
||||
def __init__(self, path):
|
||||
super().__init__(path)
|
||||
log.debug("Init DBusObject...")
|
||||
self.cb = {}
|
||||
|
||||
def register_method(self, name, cb):
|
||||
self.cb[name] = cb
|
||||
|
||||
def _callback(self, name, *args, **kwargs):
|
||||
"""Call the callback if it exists, raise an exception else"""
|
||||
try:
|
||||
cb = self.cb[name]
|
||||
except KeyError:
|
||||
raise MethodNotRegistered
|
||||
else:
|
||||
d = defer.maybeDeferred(cb, *args, **kwargs)
|
||||
d.addErrback(GenericException.create_and_raise)
|
||||
return d
|
||||
|
||||
##METHODS_PART##
|
||||
|
||||
class bridge:
|
||||
|
||||
def __init__(self):
|
||||
log.info("Init DBus...")
|
||||
self._obj = DBusObject(const_OBJ_PATH)
|
||||
|
||||
async def post_init(self):
|
||||
try:
|
||||
conn = await client.connect(reactor)
|
||||
except error.DBusException as e:
|
||||
if e.errName == "org.freedesktop.DBus.Error.NotSupported":
|
||||
log.error(
|
||||
_(
|
||||
"D-Bus is not launched, please see README to see instructions on "
|
||||
"how to launch it"
|
||||
)
|
||||
)
|
||||
raise BridgeInitError(str(e))
|
||||
|
||||
conn.exportObject(self._obj)
|
||||
await conn.requestBusName(const_INT_PREFIX)
|
||||
|
||||
##SIGNALS_PART##
|
||||
def register_method(self, name, callback):
|
||||
log.debug(f"registering DBus bridge method [{name}]")
|
||||
self._obj.register_method(name, callback)
|
||||
|
||||
def emit_signal(self, name, *args):
|
||||
self._obj.emitSignal(name, *args)
|
||||
|
||||
def add_method(
|
||||
self, name, int_suffix, in_sign, out_sign, method, async_=False, doc={}
|
||||
):
|
||||
"""Dynamically add a method to D-Bus bridge"""
|
||||
# FIXME: doc parameter is kept only temporary, the time to remove it from calls
|
||||
log.debug(f"Adding method {name!r} to D-Bus bridge")
|
||||
self._obj.plugin_iface.addMethod(
|
||||
Method(name, arguments=in_sign, returns=out_sign)
|
||||
)
|
||||
# we have to create a method here instead of using partialmethod, because txdbus
|
||||
# uses __func__ which doesn't work with partialmethod
|
||||
def caller(self_, *args, **kwargs):
|
||||
return self_._callback(name, *args, **kwargs)
|
||||
setattr(self._obj, f"dbus_{name}", MethodType(caller, self._obj))
|
||||
self.register_method(name, method)
|
||||
|
||||
def add_signal(self, name, int_suffix, signature, doc={}):
|
||||
"""Dynamically add a signal to D-Bus bridge"""
|
||||
log.debug(f"Adding signal {name!r} to D-Bus bridge")
|
||||
self._obj.plugin_iface.addSignal(Signal(name, signature))
|
||||
setattr(bridge, name, partialmethod(bridge.emit_signal, name))
|
||||
|
|
@ -0,0 +1,223 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
# SàT communication bridge
|
||||
# Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
|
||||
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import asyncio
|
||||
import dbus
|
||||
import ast
|
||||
from libervia.backend.core.i18n import _
|
||||
from libervia.backend.tools import config
|
||||
from libervia.backend.core.log import getLogger
|
||||
from libervia.backend.core.exceptions import BridgeExceptionNoService, BridgeInitError
|
||||
from dbus.mainloop.glib import DBusGMainLoop
|
||||
from .bridge_frontend import BridgeException
|
||||
|
||||
|
||||
DBusGMainLoop(set_as_default=True)
|
||||
log = getLogger(__name__)
|
||||
|
||||
|
||||
# Interface prefix
|
||||
const_INT_PREFIX = config.config_get(
|
||||
config.parse_main_conf(),
|
||||
"",
|
||||
"bridge_dbus_int_prefix",
|
||||
"org.libervia.Libervia")
|
||||
const_ERROR_PREFIX = const_INT_PREFIX + ".error"
|
||||
const_OBJ_PATH = '/org/libervia/Libervia/bridge'
|
||||
const_CORE_SUFFIX = ".core"
|
||||
const_PLUGIN_SUFFIX = ".plugin"
|
||||
const_TIMEOUT = 120
|
||||
|
||||
|
||||
def dbus_to_bridge_exception(dbus_e):
|
||||
"""Convert a DBusException to a BridgeException.
|
||||
|
||||
@param dbus_e (DBusException)
|
||||
@return: BridgeException
|
||||
"""
|
||||
full_name = dbus_e.get_dbus_name()
|
||||
if full_name.startswith(const_ERROR_PREFIX):
|
||||
name = dbus_e.get_dbus_name()[len(const_ERROR_PREFIX) + 1:]
|
||||
else:
|
||||
name = full_name
|
||||
# XXX: dbus_e.args doesn't contain the original DBusException args, but we
|
||||
# receive its serialized form in dbus_e.args[0]. From that we can rebuild
|
||||
# the original arguments list thanks to ast.literal_eval (secure eval).
|
||||
message = dbus_e.get_dbus_message() # similar to dbus_e.args[0]
|
||||
try:
|
||||
message, condition = ast.literal_eval(message)
|
||||
except (SyntaxError, ValueError, TypeError):
|
||||
condition = ''
|
||||
return BridgeException(name, message, condition)
|
||||
|
||||
|
||||
class bridge:
|
||||
|
||||
def bridge_connect(self, callback, errback):
|
||||
try:
|
||||
self.sessions_bus = dbus.SessionBus()
|
||||
self.db_object = self.sessions_bus.get_object(const_INT_PREFIX,
|
||||
const_OBJ_PATH)
|
||||
self.db_core_iface = dbus.Interface(self.db_object,
|
||||
dbus_interface=const_INT_PREFIX + const_CORE_SUFFIX)
|
||||
self.db_plugin_iface = dbus.Interface(self.db_object,
|
||||
dbus_interface=const_INT_PREFIX + const_PLUGIN_SUFFIX)
|
||||
except dbus.exceptions.DBusException as e:
|
||||
if e._dbus_error_name in ('org.freedesktop.DBus.Error.ServiceUnknown',
|
||||
'org.freedesktop.DBus.Error.Spawn.ExecFailed'):
|
||||
errback(BridgeExceptionNoService())
|
||||
elif e._dbus_error_name == 'org.freedesktop.DBus.Error.NotSupported':
|
||||
log.error(_("D-Bus is not launched, please see README to see instructions on how to launch it"))
|
||||
errback(BridgeInitError)
|
||||
else:
|
||||
errback(e)
|
||||
else:
|
||||
callback()
|
||||
#props = self.db_core_iface.getProperties()
|
||||
|
||||
def register_signal(self, functionName, handler, iface="core"):
|
||||
if iface == "core":
|
||||
self.db_core_iface.connect_to_signal(functionName, handler)
|
||||
elif iface == "plugin":
|
||||
self.db_plugin_iface.connect_to_signal(functionName, handler)
|
||||
else:
|
||||
log.error(_('Unknown interface'))
|
||||
|
||||
def __getattribute__(self, name):
|
||||
""" usual __getattribute__ if the method exists, else try to find a plugin method """
|
||||
try:
|
||||
return object.__getattribute__(self, name)
|
||||
except AttributeError:
|
||||
# The attribute is not found, we try the plugin proxy to find the requested method
|
||||
|
||||
def get_plugin_method(*args, **kwargs):
|
||||
# We first check if we have an async call. We detect this in two ways:
|
||||
# - if we have the 'callback' and 'errback' keyword arguments
|
||||
# - or if the last two arguments are callable
|
||||
|
||||
async_ = False
|
||||
args = list(args)
|
||||
|
||||
if kwargs:
|
||||
if 'callback' in kwargs:
|
||||
async_ = True
|
||||
_callback = kwargs.pop('callback')
|
||||
_errback = kwargs.pop('errback', lambda failure: log.error(str(failure)))
|
||||
try:
|
||||
args.append(kwargs.pop('profile'))
|
||||
except KeyError:
|
||||
try:
|
||||
args.append(kwargs.pop('profile_key'))
|
||||
except KeyError:
|
||||
pass
|
||||
# at this point, kwargs should be empty
|
||||
if kwargs:
|
||||
log.warning("unexpected keyword arguments, they will be ignored: {}".format(kwargs))
|
||||
elif len(args) >= 2 and callable(args[-1]) and callable(args[-2]):
|
||||
async_ = True
|
||||
_errback = args.pop()
|
||||
_callback = args.pop()
|
||||
|
||||
method = getattr(self.db_plugin_iface, name)
|
||||
|
||||
if async_:
|
||||
kwargs['timeout'] = const_TIMEOUT
|
||||
kwargs['reply_handler'] = _callback
|
||||
kwargs['error_handler'] = lambda err: _errback(dbus_to_bridge_exception(err))
|
||||
|
||||
try:
|
||||
return method(*args, **kwargs)
|
||||
except ValueError as e:
|
||||
if e.args[0].startswith("Unable to guess signature"):
|
||||
# XXX: if frontend is started too soon after backend, the
|
||||
# inspection misses methods (notably plugin dynamically added
|
||||
# methods). The following hack works around that by redoing the
|
||||
# cache of introspected methods signatures.
|
||||
log.debug("using hack to work around inspection issue")
|
||||
proxy = self.db_plugin_iface.proxy_object
|
||||
IN_PROGRESS = proxy.INTROSPECT_STATE_INTROSPECT_IN_PROGRESS
|
||||
proxy._introspect_state = IN_PROGRESS
|
||||
proxy._Introspect()
|
||||
return self.db_plugin_iface.get_dbus_method(name)(*args, **kwargs)
|
||||
raise e
|
||||
|
||||
return get_plugin_method
|
||||
|
||||
##METHODS_PART##
|
||||
|
||||
class AIOBridge(bridge):
|
||||
|
||||
def register_signal(self, functionName, handler, iface="core"):
|
||||
loop = asyncio.get_running_loop()
|
||||
async_handler = lambda *args: asyncio.run_coroutine_threadsafe(handler(*args), loop)
|
||||
return super().register_signal(functionName, async_handler, iface)
|
||||
|
||||
def __getattribute__(self, name):
|
||||
""" usual __getattribute__ if the method exists, else try to find a plugin method """
|
||||
try:
|
||||
return object.__getattribute__(self, name)
|
||||
except AttributeError:
|
||||
# The attribute is not found, we try the plugin proxy to find the requested method
|
||||
def get_plugin_method(*args, **kwargs):
|
||||
loop = asyncio.get_running_loop()
|
||||
fut = loop.create_future()
|
||||
method = getattr(self.db_plugin_iface, name)
|
||||
reply_handler = lambda ret=None: loop.call_soon_threadsafe(
|
||||
fut.set_result, ret)
|
||||
error_handler = lambda err: loop.call_soon_threadsafe(
|
||||
fut.set_exception, dbus_to_bridge_exception(err))
|
||||
try:
|
||||
method(
|
||||
*args,
|
||||
**kwargs,
|
||||
timeout=const_TIMEOUT,
|
||||
reply_handler=reply_handler,
|
||||
error_handler=error_handler
|
||||
)
|
||||
except ValueError as e:
|
||||
if e.args[0].startswith("Unable to guess signature"):
|
||||
# same hack as for bridge.__getattribute__
|
||||
log.warning("using hack to work around inspection issue")
|
||||
proxy = self.db_plugin_iface.proxy_object
|
||||
IN_PROGRESS = proxy.INTROSPECT_STATE_INTROSPECT_IN_PROGRESS
|
||||
proxy._introspect_state = IN_PROGRESS
|
||||
proxy._Introspect()
|
||||
self.db_plugin_iface.get_dbus_method(name)(
|
||||
*args,
|
||||
**kwargs,
|
||||
timeout=const_TIMEOUT,
|
||||
reply_handler=reply_handler,
|
||||
error_handler=error_handler
|
||||
)
|
||||
|
||||
else:
|
||||
raise e
|
||||
return fut
|
||||
|
||||
return get_plugin_method
|
||||
|
||||
def bridge_connect(self):
|
||||
loop = asyncio.get_running_loop()
|
||||
fut = loop.create_future()
|
||||
super().bridge_connect(
|
||||
callback=lambda: loop.call_soon_threadsafe(fut.set_result, None),
|
||||
errback=lambda e: loop.call_soon_threadsafe(fut.set_exception, e)
|
||||
)
|
||||
return fut
|
||||
|
||||
##ASYNC_METHODS_PART##
|
||||
|
|
@ -0,0 +1,100 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
|
||||
# Libervia: an XMPP client
|
||||
# Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
|
||||
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from libervia.backend.bridge.bridge_constructor import base_constructor
|
||||
|
||||
# from textwraps import dedent
|
||||
|
||||
|
||||
class EmbeddedConstructor(base_constructor.Constructor):
|
||||
NAME = "embedded"
|
||||
CORE_TEMPLATE = "embedded_template.py"
|
||||
CORE_DEST = "embedded.py"
|
||||
CORE_FORMATS = {
|
||||
"methods": """\
|
||||
def {name}(self, {args}{args_comma}callback=None, errback=None):
|
||||
{ret_routine}
|
||||
""",
|
||||
"signals": """\
|
||||
def {name}(self, {args}):
|
||||
try:
|
||||
cb = self._signals_cbs["{category}"]["{name}"]
|
||||
except KeyError:
|
||||
log.warning(u"ignoring signal {name}: no callback registered")
|
||||
else:
|
||||
cb({args_result})
|
||||
""",
|
||||
}
|
||||
FRONTEND_TEMPLATE = "embedded_frontend_template.py"
|
||||
FRONTEND_DEST = CORE_DEST
|
||||
FRONTEND_FORMATS = {}
|
||||
|
||||
def core_completion_method(self, completion, function, default, arg_doc, async_):
|
||||
completion.update(
|
||||
{
|
||||
"debug": ""
|
||||
if not self.args.debug
|
||||
else 'log.debug ("%s")\n%s' % (completion["name"], 8 * " "),
|
||||
"args_result": self.get_arguments(function["sig_in"], name=arg_doc),
|
||||
"args_comma": ", " if function["sig_in"] else "",
|
||||
}
|
||||
)
|
||||
|
||||
if async_:
|
||||
completion["cb_or_lambda"] = (
|
||||
"callback" if function["sig_out"] else "lambda __: callback()"
|
||||
)
|
||||
completion[
|
||||
"ret_routine"
|
||||
] = """\
|
||||
d = self._methods_cbs["{name}"]({args_result})
|
||||
if callback is not None:
|
||||
d.addCallback({cb_or_lambda})
|
||||
if errback is None:
|
||||
d.addErrback(lambda failure_: log.error(failure_))
|
||||
else:
|
||||
d.addErrback(errback)
|
||||
return d
|
||||
""".format(
|
||||
**completion
|
||||
)
|
||||
else:
|
||||
completion["ret_or_nothing"] = "ret" if function["sig_out"] else ""
|
||||
completion[
|
||||
"ret_routine"
|
||||
] = """\
|
||||
try:
|
||||
ret = self._methods_cbs["{name}"]({args_result})
|
||||
except Exception as e:
|
||||
if errback is not None:
|
||||
errback(e)
|
||||
else:
|
||||
raise e
|
||||
else:
|
||||
if callback is None:
|
||||
return ret
|
||||
else:
|
||||
callback({ret_or_nothing})""".format(
|
||||
**completion
|
||||
)
|
||||
|
||||
def core_completion_signal(self, completion, function, default, arg_doc, async_):
|
||||
completion.update(
|
||||
{"args_result": self.get_arguments(function["sig_in"], name=arg_doc)}
|
||||
)
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
|
||||
# Libervia: an XMPP client
|
||||
# Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
|
||||
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from libervia.backend.bridge.embedded import bridge
|
||||
|
|
@ -0,0 +1,123 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
|
||||
# Libervia: an XMPP client
|
||||
# Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
|
||||
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from libervia.backend.core.log import getLogger
|
||||
|
||||
log = getLogger(__name__)
|
||||
from libervia.backend.core import exceptions
|
||||
|
||||
|
||||
class _Bridge(object):
|
||||
def __init__(self):
|
||||
log.debug("Init embedded bridge...")
|
||||
self._methods_cbs = {}
|
||||
self._signals_cbs = {"core": {}, "plugin": {}}
|
||||
|
||||
def bridge_connect(self, callback, errback):
|
||||
callback()
|
||||
|
||||
def register_method(self, name, callback):
|
||||
log.debug("registering embedded bridge method [{}]".format(name))
|
||||
if name in self._methods_cbs:
|
||||
raise exceptions.ConflictError("method {} is already regitered".format(name))
|
||||
self._methods_cbs[name] = callback
|
||||
|
||||
def register_signal(self, functionName, handler, iface="core"):
|
||||
iface_dict = self._signals_cbs[iface]
|
||||
if functionName in iface_dict:
|
||||
raise exceptions.ConflictError(
|
||||
"signal {name} is already regitered for interface {iface}".format(
|
||||
name=functionName, iface=iface
|
||||
)
|
||||
)
|
||||
iface_dict[functionName] = handler
|
||||
|
||||
def call_method(self, name, out_sign, async_, args, kwargs):
|
||||
callback = kwargs.pop("callback", None)
|
||||
errback = kwargs.pop("errback", None)
|
||||
if async_:
|
||||
d = self._methods_cbs[name](*args, **kwargs)
|
||||
if callback is not None:
|
||||
d.addCallback(callback if out_sign else lambda __: callback())
|
||||
if errback is None:
|
||||
d.addErrback(lambda failure_: log.error(failure_))
|
||||
else:
|
||||
d.addErrback(errback)
|
||||
return d
|
||||
else:
|
||||
try:
|
||||
ret = self._methods_cbs[name](*args, **kwargs)
|
||||
except Exception as e:
|
||||
if errback is not None:
|
||||
errback(e)
|
||||
else:
|
||||
raise e
|
||||
else:
|
||||
if callback is None:
|
||||
return ret
|
||||
else:
|
||||
if out_sign:
|
||||
callback(ret)
|
||||
else:
|
||||
callback()
|
||||
|
||||
def send_signal(self, name, args, kwargs):
|
||||
try:
|
||||
cb = self._signals_cbs["plugin"][name]
|
||||
except KeyError:
|
||||
log.debug("ignoring signal {}: no callback registered".format(name))
|
||||
else:
|
||||
cb(*args, **kwargs)
|
||||
|
||||
def add_method(self, name, int_suffix, in_sign, out_sign, method, async_=False, doc={}):
|
||||
# FIXME: doc parameter is kept only temporary, the time to remove it from calls
|
||||
log.debug("Adding method [{}] to embedded bridge".format(name))
|
||||
self.register_method(name, method)
|
||||
setattr(
|
||||
self.__class__,
|
||||
name,
|
||||
lambda self_, *args, **kwargs: self.call_method(
|
||||
name, out_sign, async_, args, kwargs
|
||||
),
|
||||
)
|
||||
|
||||
def add_signal(self, name, int_suffix, signature, doc={}):
|
||||
setattr(
|
||||
self.__class__,
|
||||
name,
|
||||
lambda self_, *args, **kwargs: self.send_signal(name, args, kwargs),
|
||||
)
|
||||
|
||||
## signals ##
|
||||
|
||||
|
||||
##SIGNALS_PART##
|
||||
## methods ##
|
||||
|
||||
##METHODS_PART##
|
||||
|
||||
# we want the same instance for both core and frontend
|
||||
bridge = None
|
||||
|
||||
|
||||
def bridge():
|
||||
global bridge
|
||||
if bridge is None:
|
||||
bridge = _Bridge()
|
||||
return bridge
|
||||
|
|
@ -0,0 +1,168 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
|
||||
# Libervia: an XMPP client
|
||||
# Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
|
||||
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from libervia.backend.bridge.bridge_constructor import base_constructor
|
||||
import sys
|
||||
from datetime import datetime
|
||||
import re
|
||||
|
||||
|
||||
class MediawikiConstructor(base_constructor.Constructor):
|
||||
def __init__(self, bridge_template, options):
|
||||
base_constructor.Constructor.__init__(self, bridge_template, options)
|
||||
self.core_template = "mediawiki_template.tpl"
|
||||
self.core_dest = "mediawiki.wiki"
|
||||
|
||||
def _add_text_decorations(self, text):
|
||||
"""Add text decorations like coloration or shortcuts"""
|
||||
|
||||
def anchor_link(match):
|
||||
link = match.group(1)
|
||||
# we add anchor_link for [method_name] syntax:
|
||||
if link in self.bridge_template.sections():
|
||||
return "[[#%s|%s]]" % (link, link)
|
||||
print("WARNING: found an anchor link to an unknown method")
|
||||
return link
|
||||
|
||||
return re.sub(r"\[(\w+)\]", anchor_link, text)
|
||||
|
||||
def _wiki_parameter(self, name, sig_in):
|
||||
"""Format parameters with the wiki syntax
|
||||
@param name: name of the function
|
||||
@param sig_in: signature in
|
||||
@return: string of the formated parameters"""
|
||||
arg_doc = self.get_arguments_doc(name)
|
||||
arg_default = self.get_default(name)
|
||||
args_str = self.get_arguments(sig_in)
|
||||
args = args_str.split(", ") if args_str else [] # ugly but it works :)
|
||||
wiki = []
|
||||
for i in range(len(args)):
|
||||
if i in arg_doc:
|
||||
name, doc = arg_doc[i]
|
||||
doc = "\n:".join(doc.rstrip("\n").split("\n"))
|
||||
wiki.append("; %s: %s" % (name, self._add_text_decorations(doc)))
|
||||
else:
|
||||
wiki.append("; arg_%d: " % i)
|
||||
if i in arg_default:
|
||||
wiki.append(":''DEFAULT: %s''" % arg_default[i])
|
||||
return "\n".join(wiki)
|
||||
|
||||
def _wiki_return(self, name):
|
||||
"""Format return doc with the wiki syntax
|
||||
@param name: name of the function
|
||||
"""
|
||||
arg_doc = self.get_arguments_doc(name)
|
||||
wiki = []
|
||||
if "return" in arg_doc:
|
||||
wiki.append("\n|-\n! scope=row | return value\n|")
|
||||
wiki.append(
|
||||
"<br />\n".join(
|
||||
self._add_text_decorations(arg_doc["return"]).rstrip("\n").split("\n")
|
||||
)
|
||||
)
|
||||
return "\n".join(wiki)
|
||||
|
||||
def generate_core_side(self):
|
||||
signals_part = []
|
||||
methods_part = []
|
||||
sections = self.bridge_template.sections()
|
||||
sections.sort()
|
||||
for section in sections:
|
||||
function = self.getValues(section)
|
||||
print(("Adding %s %s" % (section, function["type"])))
|
||||
async_msg = """<br />'''This method is asynchronous'''"""
|
||||
deprecated_msg = """<br />'''<font color="#FF0000">/!\ WARNING /!\ : This method is deprecated, please don't use it !</font>'''"""
|
||||
signature_signal = (
|
||||
"""\
|
||||
! scope=row | signature
|
||||
| %s
|
||||
|-\
|
||||
"""
|
||||
% function["sig_in"]
|
||||
)
|
||||
signature_method = """\
|
||||
! scope=row | signature in
|
||||
| %s
|
||||
|-
|
||||
! scope=row | signature out
|
||||
| %s
|
||||
|-\
|
||||
""" % (
|
||||
function["sig_in"],
|
||||
function["sig_out"],
|
||||
)
|
||||
completion = {
|
||||
"signature": signature_signal
|
||||
if function["type"] == "signal"
|
||||
else signature_method,
|
||||
"sig_out": function["sig_out"] or "",
|
||||
"category": function["category"],
|
||||
"name": section,
|
||||
"doc": self.get_doc(section) or "FIXME: No description available",
|
||||
"async": async_msg if "async" in self.getFlags(section) else "",
|
||||
"deprecated": deprecated_msg
|
||||
if "deprecated" in self.getFlags(section)
|
||||
else "",
|
||||
"parameters": self._wiki_parameter(section, function["sig_in"]),
|
||||
"return": self._wiki_return(section)
|
||||
if function["type"] == "method"
|
||||
else "",
|
||||
}
|
||||
|
||||
dest = signals_part if function["type"] == "signal" else methods_part
|
||||
dest.append(
|
||||
"""\
|
||||
== %(name)s ==
|
||||
''%(doc)s''
|
||||
%(deprecated)s
|
||||
%(async)s
|
||||
{| class="wikitable" style="text-align:left; width:80%%;"
|
||||
! scope=row | category
|
||||
| %(category)s
|
||||
|-
|
||||
%(signature)s
|
||||
! scope=row | parameters
|
||||
|
|
||||
%(parameters)s%(return)s
|
||||
|}
|
||||
"""
|
||||
% completion
|
||||
)
|
||||
|
||||
# at this point, signals_part, and methods_part should be filled,
|
||||
# we just have to place them in the right part of the template
|
||||
core_bridge = []
|
||||
template_path = self.get_template_path(self.core_template)
|
||||
try:
|
||||
with open(template_path) as core_template:
|
||||
for line in core_template:
|
||||
if line.startswith("##SIGNALS_PART##"):
|
||||
core_bridge.extend(signals_part)
|
||||
elif line.startswith("##METHODS_PART##"):
|
||||
core_bridge.extend(methods_part)
|
||||
elif line.startswith("##TIMESTAMP##"):
|
||||
core_bridge.append("Generated on %s" % datetime.now())
|
||||
else:
|
||||
core_bridge.append(line.replace("\n", ""))
|
||||
except IOError:
|
||||
print(("Can't open template file [%s]" % template_path))
|
||||
sys.exit(1)
|
||||
|
||||
# now we write to final file
|
||||
self.final_write(self.core_dest, core_bridge)
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
[[Catégorie:Salut à Toi]]
|
||||
[[Catégorie:documentation développeur]]
|
||||
|
||||
= Overview =
|
||||
This is an autogenerated doc for SàT bridge's API
|
||||
= Signals =
|
||||
##SIGNALS_PART##
|
||||
= Methods =
|
||||
##METHODS_PART##
|
||||
----
|
||||
##TIMESTAMP##
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue