]> www.infradead.org Git - users/dhowells/kafs-utils.git/commitdiff
rxgen: Extract py module emitter
authorDavid Howells <dhowells@redhat.com>
Thu, 9 Jan 2014 14:44:55 +0000 (14:44 +0000)
committerDavid Howells <dhowells@redhat.com>
Thu, 9 Jan 2014 14:49:38 +0000 (14:49 +0000)
Extract the code to emit the python module creation code from the main rxgen
script and put into its own perl module.

Signed-off-by: David Howells <dhowells@redhat.com>
rxgen/emit_py_module.pm [new file with mode: 0644]
rxgen/rxgen.pl

diff --git a/rxgen/emit_py_module.pm b/rxgen/emit_py_module.pm
new file mode 100644 (file)
index 0000000..de31f76
--- /dev/null
@@ -0,0 +1,105 @@
+#
+# Copyright (C) 2014 Red Hat, Inc. All Rights Reserved.
+# Written by David Howells (dhowells@redhat.com)
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public Licence
+# as published by the Free Software Foundation; either version
+# 2 of the Licence, or (at your option) any later version.
+#
+
+###############################################################################
+#
+# Emit python module definition.
+#
+###############################################################################
+sub emit_py_module() {
+    # Emit python structure wrapper static method table
+    print PYOUT "\n";
+    print PYOUT "/*\n";
+    print PYOUT " * The static methods.\n";
+    print PYOUT " */\n";
+    print PYOUT "static PyMethodDef module_methods[] = {\n";
+
+    foreach my $s (@structs) {
+       my $struct = @{$s}[0];
+       print PYOUT "\t{\"new_$struct\", (PyCFunction)kafs_new_py_$struct, METH_NOARGS,\n";
+       print PYOUT "\t \"Create a new $struct record.\"\n";
+       print PYOUT "\t},\n";
+    }
+
+    foreach $func (sort keys %funcs) {
+       my @params = @{$funcs{$func}};
+       print PYOUT "\t{\"$func\", (PyCFunction)kafs_$func, METH_VARARGS, \"\" },\n";
+    }
+
+    print PYOUT "\t{\"rx_new_connection\", (PyCFunction)kafs_py_rx_new_connection, METH_VARARGS,\n";
+    print PYOUT "\t\"\" },\n";
+
+    print PYOUT "\t{}\n";
+    print PYOUT "};\n";
+
+    # Emit python structure wrapper loader
+    print PYOUT "\n";
+
+    print PYOUT "static PyModuleDef kafs_module = {\n";
+    print PYOUT "\t.m_base = PyModuleDef_HEAD_INIT,\n";
+    print PYOUT "\t.m_name = \"kafs\",\n";
+    print PYOUT "\t.m_doc = \"AFS stuff.\",\n";
+    print PYOUT "\t.m_size = -1,\n";
+    print PYOUT "\t.m_methods = module_methods,\n";
+    print PYOUT "};\n";
+
+    print PYHDR "\n";
+    print PYHDR "extern PyObject *pykafs_load_wrappers(void);\n";
+
+    print PYOUT "\n";
+    print PYOUT "PyObject *pykafs_load_wrappers(void)\n";
+    print PYOUT "{\n";
+    print PYOUT "\tPyObject *m;\n";
+
+    # Load types
+    if (@structs) {
+       print PYOUT "\tif (";
+       print PYOUT "PyType_Ready(&py_rx_connectionType) < 0";
+       my $first = 0;
+       foreach my $s (@structs) {
+           my @members = @{$s};
+           my $struct = $members[0];
+           print PYOUT " ||\n\t    " unless ($first);
+           print PYOUT "PyType_Ready(&py_", $struct, "Type) < 0";
+           $first = 0;
+       }
+       print PYOUT ")\n";
+       print PYOUT "\t\treturn NULL;\n";
+    }
+
+    print PYOUT "\n";
+    print PYOUT "\tm = PyModule_Create(&kafs_module);\n";
+    print PYOUT "\tif (!m)\n";
+    print PYOUT "\t\treturn NULL;\n";
+
+    if (%constants) {
+       print PYOUT "\n";
+       foreach my $c (sort keys %constants) {
+           print PYOUT "\tPyModule_AddIntConstant(m, \"$c\", $c);\n";
+       }
+    }
+
+    if (@structs) {
+       print PYOUT "\n";
+       foreach my $s (@structs) {
+           my @members = @{$s};
+           my $struct = $members[0];
+           print PYOUT "\tPy_INCREF(&py_", $struct, "Type);\n";
+           print PYOUT "\tPyModule_AddObject(m, \"$struct\", (PyObject *)&py_", $struct, "Type);\n";
+       }
+
+       print PYOUT "\n";
+       print PYOUT "\treturn m;\n";
+    }
+
+    print PYOUT "}\n";
+}
+
+1;
index c5e00c83827407d49bfe9a4876c469775c41a047..5a4439bcc21f4491c889590d1a4591640da54e22 100755 (executable)
@@ -20,6 +20,7 @@ use strict;
 use lib "rxgen";
 use emit_py_types;
 use emit_py_sync_funcs;
+use emit_py_module;
 
 die "Need list of sources\n" if ($#ARGV < 0);
 
@@ -579,98 +580,4 @@ foreach $func (sort keys %funcs) {
     emit_py_func_simple_sync_call($func, \@request, \@reply, @params);
 }
 
-###############################################################################
-#
-# Emit python module definition.
-#
-###############################################################################
-sub emit_py_module() {
-    # Emit python structure wrapper static method table
-    print PYOUT "\n";
-    print PYOUT "/*\n";
-    print PYOUT " * The static methods.\n";
-    print PYOUT " */\n";
-    print PYOUT "static PyMethodDef module_methods[] = {\n";
-
-    foreach my $s (@structs) {
-       my $struct = @{$s}[0];
-       print PYOUT "\t{\"new_$struct\", (PyCFunction)kafs_new_py_$struct, METH_NOARGS,\n";
-       print PYOUT "\t \"Create a new $struct record.\"\n";
-       print PYOUT "\t},\n";
-    }
-
-    foreach $func (sort keys %funcs) {
-       my @params = @{$funcs{$func}};
-       print PYOUT "\t{\"$func\", (PyCFunction)kafs_$func, METH_VARARGS, \"\" },\n";
-    }
-
-    print PYOUT "\t{\"rx_new_connection\", (PyCFunction)kafs_py_rx_new_connection, METH_VARARGS,\n";
-    print PYOUT "\t\"\" },\n";
-
-    print PYOUT "\t{}\n";
-    print PYOUT "};\n";
-
-    # Emit python structure wrapper loader
-    print PYOUT "\n";
-
-    print PYOUT "static PyModuleDef kafs_module = {\n";
-    print PYOUT "\t.m_base = PyModuleDef_HEAD_INIT,\n";
-    print PYOUT "\t.m_name = \"kafs\",\n";
-    print PYOUT "\t.m_doc = \"AFS stuff.\",\n";
-    print PYOUT "\t.m_size = -1,\n";
-    print PYOUT "\t.m_methods = module_methods,\n";
-    print PYOUT "};\n";
-
-    print PYHDR "\n";
-    print PYHDR "extern PyObject *pykafs_load_wrappers(void);\n";
-
-    print PYOUT "\n";
-    print PYOUT "PyObject *pykafs_load_wrappers(void)\n";
-    print PYOUT "{\n";
-    print PYOUT "\tPyObject *m;\n";
-
-    # Load types
-    if (@structs) {
-       print PYOUT "\tif (";
-       print PYOUT "PyType_Ready(&py_rx_connectionType) < 0";
-       my $first = 0;
-       foreach my $s (@structs) {
-           my @members = @{$s};
-           my $struct = $members[0];
-           print PYOUT " ||\n\t    " unless ($first);
-           print PYOUT "PyType_Ready(&py_", $struct, "Type) < 0";
-           $first = 0;
-       }
-       print PYOUT ")\n";
-       print PYOUT "\t\treturn NULL;\n";
-    }
-
-    print PYOUT "\n";
-    print PYOUT "\tm = PyModule_Create(&kafs_module);\n";
-    print PYOUT "\tif (!m)\n";
-    print PYOUT "\t\treturn NULL;\n";
-
-    if (%constants) {
-       print PYOUT "\n";
-       foreach my $c (sort keys %constants) {
-           print PYOUT "\tPyModule_AddIntConstant(m, \"$c\", $c);\n";
-       }
-    }
-
-    if (@structs) {
-       print PYOUT "\n";
-       foreach my $s (@structs) {
-           my @members = @{$s};
-           my $struct = $members[0];
-           print PYOUT "\tPy_INCREF(&py_", $struct, "Type);\n";
-           print PYOUT "\tPyModule_AddObject(m, \"$struct\", (PyObject *)&py_", $struct, "Type);\n";
-       }
-
-       print PYOUT "\n";
-       print PYOUT "\treturn m;\n";
-    }
-
-    print PYOUT "}\n";
-}
-
 emit_py_module();