Python: Reduce boilerplate

This commit is contained in:
Vicki Pfau 2017-05-31 17:04:24 -07:00
parent 06d89aee48
commit 7b66a702cf
2 changed files with 17 additions and 4 deletions

View File

@ -0,0 +1,15 @@
# Copyright (c) 2013-2017 Jeffrey Pfau
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from ._pylib import ffi, lib
def createCallback(structName, cbName, funcName=None):
funcName = funcName or "_py{}{}".format(structName, cbName.capitalize())
fullStruct = "struct {}*".format(structName)
def cb(handle, *args):
h = ffi.cast(fullStruct, handle)
getattr(ffi.from_handle(h.pyobj), cbName)(*args)
return ffi.def_extern(name=funcName)(cb)

View File

@ -4,11 +4,9 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from ._pylib import ffi, lib
from . import createCallback
@ffi.def_extern()
def _pyLog(logger, category, level, message):
l = ffi.cast("struct mLoggerPy*", logger)
ffi.from_handle(l.pyobj).log(category, level, ffi.string(message).decode('UTF-8'))
createCallback("mLoggerPy", "log", "_pyLog")
def installDefault(logger):
lib.mLogSetDefaultLogger(logger._native)