/[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 865 - (show annotations) (download) (as text)
Thu Sep 28 14:19:17 2006 UTC (14 years, 1 month ago) by johnpye
File MIME type: text/x-csrc
File size: 2877 byte(s)
First success at executing python code from inside ASCEND "IMPORT" statement :-)
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
20 #include <stdio.h>
21 #include <string.h>
22
23 #include <utilities/ascConfig.h>
24 #include <utilities/error.h>
25 #include <general/ospath.h>
26
27 #include <compiler/importhandler.h>
28
29 #include <Python.h>
30
31 ImportHandlerCreateFilenameFn extpy_filename;
32 ImportHandlerImportFn extpy_import;
33
34 #ifndef ASC_EXPORT
35 # error "Where is ASC_EXPORT?"
36 #endif
37
38 /**
39 This is the function called from "IMPORT extpy"
40
41 It sets up the functions in this external function library
42 */
43 extern ASC_EXPORT(int) extpy_register(){
44 int result = 0;
45
46 ERROR_REPORTER_HERE(ASC_PROG_NOTE,"Hello from EXTPY...");
47
48 struct ImportHandler *handler;
49 handler = ASC_NEW(struct ImportHandler);
50
51 handler->name = "extpy";
52 handler->filenamefn = extpy_filename;
53 handler->importfn = extpy_import;
54
55 result = importhandler_add(handler);
56
57 if(result){
58 ERROR_REPORTER_HERE(ASC_PROG_ERR,"Failed to register import handler (error = %d)",result);
59 }
60 return result;
61 }
62
63 /**
64 Create a filename base on a partial filename. In that case of python, this
65 just means adding '.py' to the end.
66
67 @param partialname the filename without suffix, as specified in the user's "IMPORT" command
68 @return new filename, or NULL on failure
69 */
70 char *extpy_filename(const char *partialname){
71 char *name;
72 int len;
73 if(partialname==NULL){
74 ERROR_REPORTER_HERE(ASC_PROG_ERR,"Partial name is NULL, can't work out filename");
75 return NULL;
76 }
77
78 len = strlen(partialname);
79 name = ASC_NEW_ARRAY_CLEAR(char,len+4);
80 strcpy(name,partialname);
81 strcat(name,".py");
82 CONSOLE_DEBUG("New filename is '%s'",name);
83 return name;
84 }
85
86 /**
87 Import a python script located at the path indicated.
88
89 @return 0 on success, else error codes (TBD)
90 */
91 int extpy_import(const struct FilePath *fp, const char *initfunc, const char *partialpath){
92 char *name;
93 name = ospath_str(fp);
94 FILE *f;
95
96 CONSOLE_DEBUG("IMPORTING PYTHON SCRIPT %s",name);
97 if(Py_IsInitialized()){
98 CONSOLE_DEBUG("PYTHON IS ALREADY INITIALISED");
99 }else{
100 CONSOLE_DEBUG("INITIALISING PYTHON");
101 Py_Initialize();
102 }
103
104 PyRun_SimpleString("import ascpy");
105 f = fopen(name,"r");
106 PyRun_AnyFile(f,name);
107 fclose(f);
108
109 ASC_FREE(name);
110 return 1;
111 }
112

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