gopy: add gopy convenience python module

Fixes #14

Change-Id: I9ae86ff54e1abcd3dbb943b5ce7a1319914026d5
This commit is contained in:
Sebastien Binet 2015-08-06 17:28:29 +02:00
parent b8a602ce56
commit a9d6b9af52
1 changed files with 46 additions and 0 deletions

46
gopy.py Normal file
View File

@ -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)