[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [pykde5/srhaque-new-sip-generator] sip_generation: Allow functions to be used in %MethodCode generat
From:       Shaheed Haque <srhaque () theiet ! org>
Date:       2016-04-30 22:23:32
Message-ID: E1awdIS-0002og-LZ () scm ! kde ! org
[Download RAW message or body]

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_methodcode.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"] = deepcopy(sip["decl"])
+    sip["fn_result2"] = "void"
+    sip["code"] = """
+        Py_BEGIN_ALLOW_THREADS
+        //    sipCpp = new sipKCoreConfigSkeleton_Item{} (PyItem{} (*a0, *a1, a2, \
a3)); +        sipCpp = new sipKCoreConfigSkeleton_Item{} (*a0, *a1, a2, a3);
+        Py_END_ALLOW_THREADS
+        """.replace("{}", entry["ctx"])
+    sip["decl"][2] = sip["decl"][2].replace("&", "")
+
+
+def _kcoreconfigskeleton_add_item_xxx(function, sip, entry):
+    sip["code"] = """
+        Py_BEGIN_ALLOW_THREADS
+        sipRes = new PyItem{} (sipCpp->currentGroup(), a3->isNull() ? *a0 : *a3, a1, \
a2); +        sipCpp->addItem(sipRes, *a0);
+        Py_END_ALLOW_THREADS
+        """.format(entry["ctx"])
+
+
 code = {
 "KParts::BrowserExtension": #"kparts/browserextension.h"
 {
@@ -267,195 +292,99 @@ code = {
 {
     "ItemBool":
     {
-        "decl": "const QString& _group, const QString& _key, bool reference, bool \
                defaultValue = 1",
-        "decl2": "const QString& _group, const QString& _key, bool& reference, bool \
                defaultValue = 1",
-        "fn_result2": "void",
-        "code":
-        """
-        Py_BEGIN_ALLOW_THREADS
-        //    sipCpp = new sipKCoreConfigSkeleton_ItemBool (PyItemBool (*a0, *a1, \
                a2, a3));
-        sipCpp = 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 reference, \
                qint32 defaultValue = 0",
-        "decl2": "const QString& _group, const QString& _key, qint32& reference, \
                qint32 defaultValue = 0",
-        "fn_result2": "void",
-        "code":
-        """
-        Py_BEGIN_ALLOW_THREADS
-        //    sipCpp = new sipKCoreConfigSkeleton_ItemInt (PyItemInt (*a0, *a1, a2, \
                a3));
-        sipCpp = 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 reference, \
                qint64 defaultValue = 0",
-        "decl2": "const QString& _group, const QString& _key, qint64& reference, \
                qint64 defaultValue = 0",
-        "fn_result2": "void",
-        "code":
-        """
-        Py_BEGIN_ALLOW_THREADS
-        //    sipCpp = new sipKCoreConfigSkeleton_ItemLongLong (PyItemLongLong (*a0, \
                *a1, a2, a3));
-        sipCpp = 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 reference, const \
                QList<KCoreConfigSkeleton::ItemEnum::Choice> choices, qint32 \
                defaultValue = 0",
-        "decl2": "const QString& _group, const QString& _key, qint32& reference, \
const QList<KCoreConfigSkeleton::ItemEnum::Choice>& choices, qint32 defaultValue = \
                0",
-        "fn_result2": "void",
-        "code":
-        """
-        Py_BEGIN_ALLOW_THREADS
-        sipCpp = new sipKCoreConfigSkeleton_ItemEnum (*a0, *a1, a2, *a3, a4);
-        //    sipCpp = 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 reference, \
                quint32 defaultValue = 0",
-        "decl2": "const QString& _group, const QString& _key, quint32& reference, \
                quint32 defaultValue = 0",
-        "fn_result2": "void",
-        "code":
-        """
-        Py_BEGIN_ALLOW_THREADS
-        //    sipCpp = new sipKCoreConfigSkeleton_ItemUInt (PyItemUInt (*a0, *a1, \
                a2, a3));
-        sipCpp = 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 reference, \
                quint64 defaultValue = 0",
-        "decl2": "const QString& _group, const QString& _key, quint64& reference, \
                quint64 defaultValue = 0",
-        "fn_result2": "void",
-        "code":
-        """
-            Py_BEGIN_ALLOW_THREADS
-        //    sipCpp = new sipKCoreConfigSkeleton_ItemULongLong (PyItemULongLong \
                (*a0, *a1, a2, a3));
-            sipCpp = 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 reference, \
                double defaultValue = 0",
-        "decl2": "const QString& _group, const QString& _key, double& reference, \
                double defaultValue = 0",
-        "fn_result2": "void",
-        "code":
-        """
-        Py_BEGIN_ALLOW_THREADS
-        //    sipCpp = new sipKCoreConfigSkeleton_ItemDouble (PyItemDouble (*a0, \
                *a1, a2, a3));
-        sipCpp = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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"] = ""
         if entry:
             before = deepcopy(sip)
-            sip["decl"] = entry.get("decl", sip["decl"])
-            sip["fn_result"] = 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"] = entry.get("decl2", sip["decl"])
-                sip["fn_result2"] = entry.get("fn_result2", sip["fn_result2"])
+            sip["code"] = entry["code"]
+            if callable(sip["code"]):
+                sip["code"](function, sip, entry)
+            else:
+                sip["decl"] = entry.get("decl", sip["decl"])
+                sip["fn_result"] = 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"] = entry.get("decl2", sip["decl"])
+                    sip["fn_result2"] = entry.get("fn_result2", sip["fn_result2"])
             #
             # Fetch/format the code.
             #
-            text = textwrap.dedent(entry["code"]).strip()
+            text = textwrap.dedent(sip["code"]).strip()
             text = ["    " + t for t in text.split("\n")]
             sip["code"] = 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"] = ", ".join(sip["template_parameters"])
-            sip["decl"] = ", ".join(sip["decl"])
+            if not isinstance(sip["decl"], str):
+                sip["decl"] = ", ".join(sip["decl"])
             sip["annotations"] = ",".join(sip["annotations"])
-            self.rules.methodcode(function, sip)
             decl = sip["name"] + "(" + sip["decl"] + ")"
             if sip["decl2"]:
+                if not isinstance(sip["decl2"], str):
+                    sip["decl2"] = ", ".join(sip["decl2"])
                 decl += "\n    " + pad + "["
                 if sip["fn_result2"]:
                     if sip["fn_result2"][-1] in "*&":


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic