libpymolfile.i 3.75 KB
Newer Older
Berk Onat's avatar
Berk Onat committed
1
/* -*- C -*-  (not really, but good for syntax highlighting) */
Berk Onat's avatar
Berk Onat committed
2
/* SWIG interface for libpymolfile of VMD molfile_plugins
Berk Onat's avatar
Berk Onat committed
3
   Copyright (c) 2017 Berk Onat <b.onat@warwick.ac.uk>
Berk Onat's avatar
Berk Onat committed
4
   Published under UIUC LICENSE
Berk Onat's avatar
Berk Onat committed
5

Berk Onat's avatar
Berk Onat committed
6
   swig -c++ -python -outdir . molfile/libpymolfile.i
Berk Onat's avatar
Berk Onat committed
7 8 9 10 11
*/
%define DOCSTRING
"
:Author:  Berk Onat <b.onat@warwick.ac.uk>
:Year:    2017
Berk Onat's avatar
Berk Onat committed
12
:Licence: UIUC LICENSE
Berk Onat's avatar
Berk Onat committed
13 14 15 16 17 18 19 20 21 22 23


"
%enddef

%module(docstring=DOCSTRING) libpymolfile


%{
/* Python SWIG interface to libpymolfile
   Copyright (c) 2017 Berk Onat <b.onat@warwick.ac.uk>
Berk Onat's avatar
Berk Onat committed
24
   Published with UIUC LICENSE
Berk Onat's avatar
Berk Onat committed
25 26
 */
#define SWIG_FILE_WITH_INIT
27
#define __STDC_FORMAT_MACROS
Berk Onat's avatar
Berk Onat committed
28 29 30 31
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
32
#include <inttypes.h>
Berk Onat's avatar
Berk Onat committed
33 34 35 36 37 38
#include "pymolfile.h"
%}

%include "numpy.i"

%init %{
39
Py_Initialize();
Berk Onat's avatar
Berk Onat committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
import_array();
%}


/* 
  Wrapping only high-level plugin functions to register VMD 
  plugins and to retrive the data through molfile_plugin interface.

  Only modifing call signatures. This will help one to access functions 
  without dealing with pointers from python.
*/


/* pymolfile.c 
   initialize and finalize molfile plugins
*/
%feature("autodoc", "0") molfile_plugin_list;
57
extern PyObject* molfile_plugin_list(int maxsize);
Berk Onat's avatar
Berk Onat committed
58 59 60 61 62 63 64 65

%feature("autodoc", "0") molfile_init;
extern int molfile_init(void);

%feature("autodoc", "0") molfile_finish;
extern int molfile_finish(void);

%feature("autodoc", "0") get_plugin;
66 67
extern PyObject* get_plugin(PyObject* molcapsule, int plug_no);

Berk Onat's avatar
Berk Onat committed
68 69

%feature("autodoc", "0") molfile_plugin_info;
70
extern PyObject * molfile_plugin_info(PyObject* molcapsule, int plugin_no);
Berk Onat's avatar
Berk Onat committed
71

72 73 74 75 76 77 78 79

%feature("autodoc", "0") my_open_file_read;
%rename (open_file_read) my_open_file_read;
%exception my_open_file_read {
  $action
  if (PyErr_Occurred()) SWIG_fail;
}
%inline %{
80
PyObject * my_open_file_read(PyObject* molcapsule, char* fname, char* ftype, int natoms) {
81 82 83 84
    if (PyType_Ready(&MolObjectType) < 0)
        return NULL;
    PyTypeObject *type = &MolObjectType;
    MolObject *plugin_c;
85
    molfile_plugin_t* plugin = (molfile_plugin_t*) PyMolfileCapsule_AsVoidPtr(molcapsule);
86
    plugin_c = (MolObject *)type->tp_alloc(type, 0);
87 88 89 90 91 92 93 94 95 96
    /*plugin_c->plugin = plugin;*/
    plugin_c->plugin = molcapsule;
    void *file_handle = plugin->open_file_read(fname, ftype, &natoms);
    plugin_c->file_handle = PyMolfileCapsule_FromVoidPtr(file_handle, del_molfile_file_handle);
    if (!plugin_c->file_handle) {
        Py_RETURN_NONE;
    } else {
        plugin_c->natoms = natoms;
        return (PyObject *)plugin_c;
    }
97 98 99
  }
%}

Berk Onat's avatar
Berk Onat committed
100 101 102 103 104 105 106 107 108
%feature("autodoc", "0") my_close_file_read;
%rename (close_file_read) my_close_file_read;
%exception my_close_file_read {
  $action
  if (PyErr_Occurred()) SWIG_fail;
}
%inline %{
PyObject * my_close_file_read(PyObject* molpack) {
    MolObject* plugin_handle = (MolObject*) molpack;
109 110 111 112
    PyObject* plugincapsule = plugin_handle->plugin;   
    PyObject* filecapsule = plugin_handle->file_handle;
    molfile_plugin_t* plugin = (molfile_plugin_t*) PyMolfileCapsule_AsVoidPtr(plugincapsule);
    void *file_handle = (void*) PyMolfileCapsule_AsVoidPtr(filecapsule);
113
    plugin->close_file_read(file_handle);
114
    Py_RETURN_TRUE;
Berk Onat's avatar
Berk Onat committed
115 116 117 118 119 120 121 122 123 124 125 126
  }
%}

%feature("autodoc", "0") read_fill_structure;
extern PyObject * read_fill_structure(PyObject* molpack, PyObject* prototype);

%feature("autodoc", "0") read_fill_bonds;
extern PyObject * read_fill_bonds(PyObject* molpack);

%feature("autodoc", "0") read_fill_angles;
extern PyObject * read_fill_angles(PyObject* molpack);

Berk Onat's avatar
Berk Onat committed
127 128 129
%feature("autodoc", "0") read_fill_next_timestep;
extern PyObject * read_fill_next_timestep(PyObject* molpack);

130 131
%feature("autodoc", "0") are_plugins_same;
PyObject* are_plugins_same(PyObject* molpack_a, PyObject* molpack_b);
Berk Onat's avatar
Berk Onat committed
132

133 134
%feature("autodoc", "0") are_filehandles_same;
PyObject* are_filehandles_same(PyObject* molpack_a, PyObject* molpack_b);
Berk Onat's avatar
Berk Onat committed
135