1# Copyright (c) 2013-2017 Jeffrey Pfau
2#
3# This Source Code Form is subject to the terms of the Mozilla Public
4# License, v. 2.0. If a copy of the MPL was not distributed with this
5# file, You can obtain one at http://mozilla.org/MPL/2.0/.
6from ._pylib import ffi, lib  # pylint: disable=no-name-in-module
7
8from collections import namedtuple
9
10
11def create_callback(struct_name, cb_name, func_name=None):
12    func_name = func_name or "_py{}{}".format(struct_name, cb_name[0].upper() + cb_name[1:])
13    full_struct = "struct {}*".format(struct_name)
14
15    def callback(handle, *args):
16        handle = ffi.cast(full_struct, handle)
17        return getattr(ffi.from_handle(handle.pyobj), cb_name)(*args)
18
19    return ffi.def_extern(name=func_name)(callback)
20
21
22__version__ = ffi.string(lib.projectVersion).decode('utf-8')
23
24GitInfo = namedtuple("GitInfo", "commit commitShort branch revision")
25
26GIT = {}
27if lib.gitCommit and lib.gitCommit != "(unknown)":
28    GIT['commit'] = ffi.string(lib.gitCommit).decode('utf-8')
29if lib.gitCommitShort and lib.gitCommitShort != "(unknown)":
30    GIT['commitShort'] = ffi.string(lib.gitCommitShort).decode('utf-8')
31if lib.gitBranch and lib.gitBranch != "(unknown)":
32    GIT['branch'] = ffi.string(lib.gitBranch).decode('utf-8')
33if lib.gitRevision > 0:
34    GIT['revision'] = lib.gitRevision
35
36GIT = GitInfo(**GIT)
37