From kde-commits Sat Apr 30 22:23:32 2016 From: Shaheed Haque Date: Sat, 30 Apr 2016 22:23:32 +0000 To: kde-commits Subject: [pykde5/srhaque-new-sip-generator] sip_generation: Allow functions to be used in %MethodCode generat Message-Id: X-MARC-Message: https://marc.info/?l=kde-commits&m=146205502222458 Git commit 3894933a2789481243b1c887bc3fb3b94decbe10 by Shaheed Haque. Committed on 30/04/2016 at 22:23. Pushed by shaheed into branch 'srhaque-new-sip-generator'. Allow functions to be used in %MethodCode generation. M +55 -126 sip_generation/PyKF5_methodcode.py M +14 -10 sip_generation/rules_engine.py M +5 -2 sip_generation/sip_generator.py http://commits.kde.org/pykde5/3894933a2789481243b1c887bc3fb3b94decbe10 diff --git a/sip_generation/PyKF5_methodcode.py b/sip_generation/PyKF5_meth= odcode.py index ec1665a..5904665 100644 --- a/sip_generation/PyKF5_methodcode.py +++ b/sip_generation/PyKF5_methodcode.py @@ -21,6 +21,31 @@ SIP binding custom %MethodCode for PyKF5. = """ = + +from copy import deepcopy + + +def _kcoreconfigskeleton_item_xxx(function, sip, entry): + sip["decl2"] =3D deepcopy(sip["decl"]) + sip["fn_result2"] =3D "void" + sip["code"] =3D """ + Py_BEGIN_ALLOW_THREADS + // sipCpp =3D new sipKCoreConfigSkeleton_Item{} (PyItem{} (*a0,= *a1, a2, a3)); + sipCpp =3D new sipKCoreConfigSkeleton_Item{} (*a0, *a1, a2, a3); + Py_END_ALLOW_THREADS + """.replace("{}", entry["ctx"]) + sip["decl"][2] =3D sip["decl"][2].replace("&", "") + + +def _kcoreconfigskeleton_add_item_xxx(function, sip, entry): + sip["code"] =3D """ + Py_BEGIN_ALLOW_THREADS + sipRes =3D new PyItem{} (sipCpp->currentGroup(), a3->isNull() ? *a= 0 : *a3, a1, a2); + sipCpp->addItem(sipRes, *a0); + Py_END_ALLOW_THREADS + """.format(entry["ctx"]) + + code =3D { "KParts::BrowserExtension": #"kparts/browserextension.h" { @@ -267,195 +292,99 @@ code =3D { { "ItemBool": { - "decl": "const QString& _group, const QString& _key, bool referenc= e, bool defaultValue =3D 1", - "decl2": "const QString& _group, const QString& _key, bool& refere= nce, bool defaultValue =3D 1", - "fn_result2": "void", - "code": - """ - Py_BEGIN_ALLOW_THREADS - // sipCpp =3D new sipKCoreConfigSkeleton_ItemBool (PyItemBool (= *a0, *a1, a2, a3)); - sipCpp =3D new sipKCoreConfigSkeleton_ItemBool (*a0, *a1, a2, a3); - Py_END_ALLOW_THREADS - """ + "code": _kcoreconfigskeleton_item_xxx, + "ctx": "Bool", }, }, "KCoreConfigSkeleton::ItemInt": #"kdecore/kcoreconfigskeleton.h" { "ItemInt": { - "decl": "const QString& _group, const QString& _key, qint32 refere= nce, qint32 defaultValue =3D 0", - "decl2": "const QString& _group, const QString& _key, qint32& refe= rence, qint32 defaultValue =3D 0", - "fn_result2": "void", - "code": - """ - Py_BEGIN_ALLOW_THREADS - // sipCpp =3D new sipKCoreConfigSkeleton_ItemInt (PyItemInt (*a= 0, *a1, a2, a3)); - sipCpp =3D new sipKCoreConfigSkeleton_ItemInt (*a0, *a1, a2, a3); - Py_END_ALLOW_THREADS - """ + "code": _kcoreconfigskeleton_item_xxx, + "ctx": "Int", }, }, "KCoreConfigSkeleton::ItemLongLong": #"kdecore/kcoreconfigskeleton.h" { "ItemLongLong": { - "decl": "const QString& _group, const QString& _key, qint64 refere= nce, qint64 defaultValue =3D 0", - "decl2": "const QString& _group, const QString& _key, qint64& refe= rence, qint64 defaultValue =3D 0", - "fn_result2": "void", - "code": - """ - Py_BEGIN_ALLOW_THREADS - // sipCpp =3D new sipKCoreConfigSkeleton_ItemLongLong (PyItemLo= ngLong (*a0, *a1, a2, a3)); - sipCpp =3D new sipKCoreConfigSkeleton_ItemLongLong (*a0, *a1, a2, = a3); - Py_END_ALLOW_THREADS - """ + "code": _kcoreconfigskeleton_item_xxx, + "ctx": "LongLong", }, }, "KCoreConfigSkeleton::ItemEnum": #"kdecore/kcoreconfigskeleton.h" { "ItemEnum": { - "decl": "const QString& _group, const QString& _key, qint32 refere= nce, const QList choices, qint32 def= aultValue =3D 0", - "decl2": "const QString& _group, const QString& _key, qint32& refe= rence, const QList& choices, qint32 = defaultValue =3D 0", - "fn_result2": "void", - "code": - """ - Py_BEGIN_ALLOW_THREADS - sipCpp =3D new sipKCoreConfigSkeleton_ItemEnum (*a0, *a1, a2, *a3,= a4); - // sipCpp =3D new sipKCoreConfigSkeleton_ItemEnum (PyItemEnum (= *a0, *a1, a2, *a3, a4)); - Py_END_ALLOW_THREADS - """ + "code": _kcoreconfigskeleton_item_xxx, + "ctx": "Enum", }, }, "KCoreConfigSkeleton::ItemUInt": #"kdecore/kcoreconfigskeleton.h" { "ItemUInt": { - "decl": "const QString& _group, const QString& _key, quint32 refer= ence, quint32 defaultValue =3D 0", - "decl2": "const QString& _group, const QString& _key, quint32& ref= erence, quint32 defaultValue =3D 0", - "fn_result2": "void", - "code": - """ - Py_BEGIN_ALLOW_THREADS - // sipCpp =3D new sipKCoreConfigSkeleton_ItemUInt (PyItemUInt (= *a0, *a1, a2, a3)); - sipCpp =3D new sipKCoreConfigSkeleton_ItemUInt (*a0, *a1, a2, a3); - Py_END_ALLOW_THREADS - """ + "code": _kcoreconfigskeleton_item_xxx, + "ctx": "UInt", }, }, "KCoreConfigSkeleton::ItemULongLong": #"kdecore/kcoreconfigskeleton.h" { "ItemULongLong": { - "decl": "const QString& _group, const QString& _key, quint64 refer= ence, quint64 defaultValue =3D 0", - "decl2": "const QString& _group, const QString& _key, quint64& ref= erence, quint64 defaultValue =3D 0", - "fn_result2": "void", - "code": - """ - Py_BEGIN_ALLOW_THREADS - // sipCpp =3D new sipKCoreConfigSkeleton_ItemULongLong (PyItemU= LongLong (*a0, *a1, a2, a3)); - sipCpp =3D new sipKCoreConfigSkeleton_ItemULongLong (*a0, *a1,= a2, a3); - Py_END_ALLOW_THREADS - """ + "code": _kcoreconfigskeleton_item_xxx, + "ctx": "ULongLong", }, }, "KCoreConfigSkeleton::ItemDouble": #"kdecore/kcoreconfigskeleton.h" { "ItemDouble": { - "decl": "const QString& _group, const QString& _key, double refere= nce, double defaultValue =3D 0", - "decl2": "const QString& _group, const QString& _key, double& refe= rence, double defaultValue =3D 0", - "fn_result2": "void", - "code": - """ - Py_BEGIN_ALLOW_THREADS - // sipCpp =3D new sipKCoreConfigSkeleton_ItemDouble (PyItemDoub= le (*a0, *a1, a2, a3)); - sipCpp =3D new sipKCoreConfigSkeleton_ItemDouble (*a0, *a1, a2, a3= ); - Py_END_ALLOW_THREADS - """ + "code": _kcoreconfigskeleton_item_xxx, + "ctx": "Double", }, }, "KCoreConfigSkeleton": #"kdecore/kcoreconfigskeleton.h" { "addItemBool": { - "code": - """ - Py_BEGIN_ALLOW_THREADS - sipRes =3D new PyItemBool (sipCpp->currentGroup(), a3->isNull() ? = *a0 : *a3, a1, a2); - sipCpp->addItem(sipRes, *a0); - Py_END_ALLOW_THREADS - """ + "code": _kcoreconfigskeleton_add_item_xxx, + "ctx": "Bool", }, "addItemInt": { - "code": - """ - Py_BEGIN_ALLOW_THREADS - sipRes =3D new PyItemInt (sipCpp->currentGroup(), a3->isNull() ? *= a0 : *a3, a1, a2); - sipCpp->addItem(sipRes, *a0); - Py_END_ALLOW_THREADS - """ + "code": _kcoreconfigskeleton_add_item_xxx, + "ctx": "Int", }, "addItemUInt": { - "code": - """ - Py_BEGIN_ALLOW_THREADS - sipRes =3D new PyItemUInt (sipCpp->currentGroup(), a3->isNull() ? = *a0 : *a3, a1, a2); - sipCpp->addItem(sipRes, *a0); - Py_END_ALLOW_THREADS - """ + "code": _kcoreconfigskeleton_add_item_xxx, + "ctx": "UInt", }, "addItemLongLong": { - "code": - """ - Py_BEGIN_ALLOW_THREADS - sipRes =3D new PyItemLongLong (sipCpp->currentGroup(), a3->isNull(= ) ? *a0 : *a3, a1, a2); - sipCpp->addItem(sipRes, *a0); - Py_END_ALLOW_THREADS - """ + "code": _kcoreconfigskeleton_add_item_xxx, + "ctx": "LongLong", }, "addItemInt64": { - "code": - """ - Py_BEGIN_ALLOW_THREADS - sipRes =3D new PyItemLongLong (sipCpp->currentGroup(), a3->isNull(= ) ? *a0 : *a3, a1, a2); - sipCpp->addItem(sipRes, *a0); - Py_END_ALLOW_THREADS - """ + "code": _kcoreconfigskeleton_add_item_xxx, + "ctx": "LongLong", }, "addItemULongLong": { - "code": - """ - Py_BEGIN_ALLOW_THREADS - sipRes =3D new PyItemULongLong (sipCpp->currentGroup(), a3->isNull= () ? *a0 : *a3, a1, a2); - sipCpp->addItem(sipRes, *a0); - Py_END_ALLOW_THREADS - """ + "code": _kcoreconfigskeleton_add_item_xxx, + "ctx": "ULongLong", }, "addItemUInt64": { - "code": - """ - Py_BEGIN_ALLOW_THREADS - sipRes =3D new PyItemULongLong (sipCpp->currentGroup(), a3->isNull= () ? *a0 : *a3, a1, a2); - sipCpp->addItem(sipRes, *a0); - Py_END_ALLOW_THREADS - """ + "code": _kcoreconfigskeleton_add_item_xxx, + "ctx": "ULongLong", }, "addItemDouble": { - "code": - """ - Py_BEGIN_ALLOW_THREADS - sipRes =3D new PyItemDouble (sipCpp->currentGroup(), a3->isNull() = ? *a0 : *a3, a1, a2); - sipCpp->addItem(sipRes, *a0); - Py_END_ALLOW_THREADS - """ + "code": _kcoreconfigskeleton_add_item_xxx, + "ctx": "Double", }, "init": { diff --git a/sip_generation/rules_engine.py b/sip_generation/rules_engine.py index 25ceff6..2ee1ee8 100755 --- a/sip_generation/rules_engine.py +++ b/sip_generation/rules_engine.py @@ -607,19 +607,23 @@ class MethodCodeDb(AbstractCompiledCodeDb): sip["code"] =3D "" if entry: before =3D deepcopy(sip) - sip["decl"] =3D entry.get("decl", sip["decl"]) - sip["fn_result"] =3D entry.get("fn_result", sip["fn_result"]) - # - # The user might provide one or other or both of decl2 and fn_= result2 to signify a C++ signature. If - # needed, default a missing value from decl/fn_result. - # - if "decl2" in entry or "fn_result2" in entry: - sip["decl2"] =3D entry.get("decl2", sip["decl"]) - sip["fn_result2"] =3D entry.get("fn_result2", sip["fn_resu= lt2"]) + sip["code"] =3D entry["code"] + if callable(sip["code"]): + sip["code"](function, sip, entry) + else: + sip["decl"] =3D entry.get("decl", sip["decl"]) + sip["fn_result"] =3D entry.get("fn_result", sip["fn_result= "]) + # + # The user might provide one or other or both of decl2 and= fn_result2 to signify a C++ signature. If + # needed, default a missing value from decl/fn_result. + # + if "decl2" in entry or "fn_result2" in entry: + sip["decl2"] =3D entry.get("decl2", sip["decl"]) + sip["fn_result2"] =3D entry.get("fn_result2", sip["fn_= result2"]) # # Fetch/format the code. # - text =3D textwrap.dedent(entry["code"]).strip() + text =3D textwrap.dedent(sip["code"]).strip() text =3D [" " + t for t in text.split("\n")] sip["code"] =3D self.directive + "\n".join(text) + "\n%End\n" self.trace_result(_parents(function), function, before, sip) diff --git a/sip_generation/sip_generator.py b/sip_generation/sip_generator= .py index f59ff2e..4003401 100755 --- a/sip_generation/sip_generator.py +++ b/sip_generation/sip_generator.py @@ -447,12 +447,15 @@ class SipGenerator(object): # # Any %MethodCode? # + self.rules.methodcode(function, sip) sip["template_parameters"] =3D ", ".join(sip["template_paramet= ers"]) - sip["decl"] =3D ", ".join(sip["decl"]) + if not isinstance(sip["decl"], str): + sip["decl"] =3D ", ".join(sip["decl"]) sip["annotations"] =3D ",".join(sip["annotations"]) - self.rules.methodcode(function, sip) decl =3D sip["name"] + "(" + sip["decl"] + ")" if sip["decl2"]: + if not isinstance(sip["decl2"], str): + sip["decl2"] =3D ", ".join(sip["decl2"]) decl +=3D "\n " + pad + "[" if sip["fn_result2"]: if sip["fn_result2"][-1] in "*&":