From a9d6b9af52dda6f314778a22df196dcb69ce2a2d Mon Sep 17 00:00:00 2001 From: Sebastien Binet Date: Thu, 6 Aug 2015 17:28:29 +0200 Subject: [PATCH] gopy: add gopy convenience python module Fixes #14 Change-Id: I9ae86ff54e1abcd3dbb943b5ce7a1319914026d5 --- gopy.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 gopy.py diff --git a/gopy.py b/gopy.py new file mode 100644 index 0000000..7dc94ea --- /dev/null +++ b/gopy.py @@ -0,0 +1,46 @@ +# Copyright 2015 The go-python Authors. All rights reserved. +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file. + +__doc__ = """gopy is a convenience module to wrap and bind a Go package""" +__author__ = "The go-python authors" + +__all__ = [ + "load", + ] + +### stdlib imports --- +import imp +import os +import sys + +def printf(args): + sys.stdout.write(args) + pass + +def load(pkg, output=""): + """ + `load` takes a fully qualified Go package name and runs `gopy bind` on it. + @returns the C-extension module object + """ + printf("::: gopy.loading '%s'...\n" % pkg) + + from subprocess import check_call + if output == "": + output = os.getcwd() + pass + + check_call(["gopy","bind", "-output=%s" % output, pkg]) + + n = os.path.basename(pkg) + printf("::: gopy.module=gopy.%s\n" % n) + + ok = imp.find_module(n, [output]) + if not ok: + raise RuntimeError("could not find module 'gopy.%s'" % n) + fname, path, descr = ok + return imp.load_module("gopy."+n, fname, path, descr) + + + +