/[ascend]/trunk/models/johnpye/extpy/extpy.c
ViewVC logotype

Annotation of /trunk/models/johnpye/extpy/extpy.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 871 - (hide annotations) (download) (as text)
Thu Oct 5 09:30:08 2006 UTC (14 years ago) by johnpye
File MIME type: text/x-csrc
File size: 4719 byte(s)
Changed the way that SWIG accesses the C++ 'Registry' class in the case of Python (trying to make it language neutral)
1 johnpye 862 /* ASCEND modelling environment
2     Copyright (C) 2006 Carnegie Mellon University
3    
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2, or (at your option)
7     any later version.
8    
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12     GNU General Public License for more details.
13    
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 59 Temple Place - Suite 330,
17     Boston, MA 02111-1307, USA.
18 johnpye 869 *//**
19     @file
20     Import handler to provide external python script functionality for ASCEND.
21 johnpye 862 */
22    
23     #include <stdio.h>
24     #include <string.h>
25    
26     #include <utilities/ascConfig.h>
27     #include <utilities/error.h>
28     #include <general/ospath.h>
29    
30     #include <compiler/importhandler.h>
31    
32 johnpye 865 #include <Python.h>
33    
34 johnpye 862 ImportHandlerCreateFilenameFn extpy_filename;
35     ImportHandlerImportFn extpy_import;
36    
37     #ifndef ASC_EXPORT
38     # error "Where is ASC_EXPORT?"
39     #endif
40    
41     /**
42     This is the function called from "IMPORT extpy"
43    
44     It sets up the functions in this external function library
45     */
46     extern ASC_EXPORT(int) extpy_register(){
47     int result = 0;
48    
49     ERROR_REPORTER_HERE(ASC_PROG_NOTE,"Hello from EXTPY...");
50    
51     struct ImportHandler *handler;
52     handler = ASC_NEW(struct ImportHandler);
53    
54     handler->name = "extpy";
55     handler->filenamefn = extpy_filename;
56     handler->importfn = extpy_import;
57    
58     result = importhandler_add(handler);
59    
60     if(result){
61     ERROR_REPORTER_HERE(ASC_PROG_ERR,"Failed to register import handler (error = %d)",result);
62     }
63     return result;
64     }
65    
66 johnpye 869 /*------------------------------------------------------------------------------
67     METHODS TO EXPOSE DATA TO THE EXTERNAL SCRIPT
68     */
69    
70     /* Return the number of arguments of the application command line */
71     static PyObject *extpy_getbrowser(PyObject *self, PyObject *args){
72     PyObject *browser;
73     if(args!=NULL){
74     ERROR_REPORTER_HERE(ASC_PROG_ERR,"args is not NULL?!");
75     }
76     browser = (PyObject *)importhandler_getsharedpointer("browser");
77     return Py_BuildValue("O",browser);
78     }
79    
80     static PyMethodDef extpymethods[] = {
81     {"getbrowser", extpy_getbrowser, METH_NOARGS,"Retrieve browser pointer"}
82     ,{NULL,NULL,0,NULL}
83     };
84    
85     PyMODINIT_FUNC initextpy(void){
86     PyObject *obj;
87     CONSOLE_DEBUG("registering 'extpy' module...");
88     obj = Py_InitModule3("extpy", extpymethods,"Module for accessing shared ASCEND pointers from python");
89     CONSOLE_DEBUG("returned %p",obj);
90     CONSOLE_DEBUG("name %s",PyModule_GetName(obj));
91     }
92    
93     /*------------------------------------------------------------------------------
94     STANDARD IMPORT HANDLER ROUTINES
95     */
96    
97 johnpye 862 /**
98     Create a filename base on a partial filename. In that case of python, this
99     just means adding '.py' to the end.
100 johnpye 867
101 johnpye 862 @param partialname the filename without suffix, as specified in the user's "IMPORT" command
102     @return new filename, or NULL on failure
103     */
104     char *extpy_filename(const char *partialname){
105     char *name;
106     int len;
107     if(partialname==NULL){
108     ERROR_REPORTER_HERE(ASC_PROG_ERR,"Partial name is NULL, can't work out filename");
109     return NULL;
110     }
111 johnpye 867
112 johnpye 862 len = strlen(partialname);
113     name = ASC_NEW_ARRAY_CLEAR(char,len+4);
114     strcpy(name,partialname);
115     strcat(name,".py");
116 johnpye 865 CONSOLE_DEBUG("New filename is '%s'",name);
117 johnpye 862 return name;
118     }
119    
120     /**
121     Import a python script located at the path indicated.
122    
123     @return 0 on success, else error codes (TBD)
124     */
125 johnpye 864 int extpy_import(const struct FilePath *fp, const char *initfunc, const char *partialpath){
126 johnpye 862 char *name;
127     name = ospath_str(fp);
128 johnpye 865 FILE *f;
129    
130 johnpye 862 CONSOLE_DEBUG("IMPORTING PYTHON SCRIPT %s",name);
131 johnpye 865 if(Py_IsInitialized()){
132     CONSOLE_DEBUG("PYTHON IS ALREADY INITIALISED");
133     }else{
134     CONSOLE_DEBUG("INITIALISING PYTHON");
135     Py_Initialize();
136 johnpye 867 CONSOLE_DEBUG("COMPLETED ATTEMPT TO INITIALISE PYTHON");
137 johnpye 865 }
138    
139 johnpye 867 if(!Py_IsInitialized()){
140     CONSOLE_DEBUG("UNABLE TO INITIALIZE PYTHON");
141     return 1;
142     }
143     PyRun_SimpleString("print \"HELLO FROM PYTHON IN C\"");
144 johnpye 871 CONSOLE_DEBUG("IMPORTING ASCPY...");
145 johnpye 869 PyRun_SimpleString("import ascpy");
146 johnpye 871 CONSOLE_DEBUG("CREATING LIBRARY OBJECT...");
147 johnpye 869 PyRun_SimpleString("L = ascpy.Library()");
148 johnpye 871 CONSOLE_DEBUG("PRINTING MESSAGE...");
149 johnpye 869 PyRun_SimpleString("print \"IMPORTED ASCPY\"");
150     PyRun_SimpleString("print L");
151 johnpye 867
152 johnpye 869 initextpy();
153    
154 johnpye 871 CONSOLE_DEBUG("OPENING THE SCRIPT \"%s\"",name);
155 johnpye 865 f = fopen(name,"r");
156 johnpye 871 if(f==NULL){
157     CONSOLE_DEBUG("Failed opening script");
158     ERROR_REPORTER_HERE(ASC_PROG_ERR,"UNABLE TO OPEN SCRIPT");
159     return 1;
160     }
161     CONSOLE_DEBUG("RUNNING THE SCRIPT");
162 johnpye 865 PyRun_AnyFile(f,name);
163 johnpye 871 CONSOLE_DEBUG("FINISHED RUNNING THE SCRIPT");
164    
165 johnpye 865 fclose(f);
166    
167 johnpye 862 ASC_FREE(name);
168 johnpye 865 return 1;
169 johnpye 862 }
170    

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22