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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 869 - (show annotations) (download) (as text)
Wed Oct 4 14:03:46 2006 UTC (17 years, 2 months ago) by johnpye
File MIME type: text/x-csrc
File size: 4328 byte(s)
'hello world' works from external python module, correctly hooks into 'browser' object.
1 /* 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 *//**
19 @file
20 Import handler to provide external python script functionality for ASCEND.
21 */
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 #include <Python.h>
33
34 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 /*------------------------------------------------------------------------------
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 /**
98 Create a filename base on a partial filename. In that case of python, this
99 just means adding '.py' to the end.
100
101 @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
112 len = strlen(partialname);
113 name = ASC_NEW_ARRAY_CLEAR(char,len+4);
114 strcpy(name,partialname);
115 strcat(name,".py");
116 CONSOLE_DEBUG("New filename is '%s'",name);
117 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 int extpy_import(const struct FilePath *fp, const char *initfunc, const char *partialpath){
126 char *name;
127 name = ospath_str(fp);
128 FILE *f;
129
130 CONSOLE_DEBUG("IMPORTING PYTHON SCRIPT %s",name);
131 if(Py_IsInitialized()){
132 CONSOLE_DEBUG("PYTHON IS ALREADY INITIALISED");
133 }else{
134 CONSOLE_DEBUG("INITIALISING PYTHON");
135 Py_Initialize();
136 CONSOLE_DEBUG("COMPLETED ATTEMPT TO INITIALISE PYTHON");
137 }
138
139 if(!Py_IsInitialized()){
140 CONSOLE_DEBUG("UNABLE TO INITIALIZE PYTHON");
141 return 1;
142 }
143 PyRun_SimpleString("print \"HELLO FROM PYTHON IN C\"");
144 PyRun_SimpleString("import ascpy");
145 PyRun_SimpleString("L = ascpy.Library()");
146 PyRun_SimpleString("print \"IMPORTED ASCPY\"");
147 PyRun_SimpleString("print L");
148
149 initextpy();
150
151 f = fopen(name,"r");
152 PyRun_AnyFile(f,name);
153 fclose(f);
154
155 ASC_FREE(name);
156 return 1;
157 }
158

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