![]() |
User Manual, Developers Guide and API Documentation |
![]() |
00001 /******************************************************************************* 00002 * This file is part of openWNS (open Wireless Network Simulator) 00003 * _____________________________________________________________________________ 00004 * 00005 * Copyright (C) 2004-2007 00006 * Chair of Communication Networks (ComNets) 00007 * Kopernikusstr. 16, D-52074 Aachen, Germany 00008 * phone: ++49-241-80-27910, 00009 * fax: ++49-241-80-22242 00010 * email: info@openwns.org 00011 * www: http://www.openwns.org 00012 * _____________________________________________________________________________ 00013 * 00014 * openWNS is free software; you can redistribute it and/or modify it under the 00015 * terms of the GNU Lesser General Public License version 2 as published by the 00016 * Free Software Foundation; 00017 * 00018 * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY 00019 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 00020 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 00021 * details. 00022 * 00023 * You should have received a copy of the GNU Lesser General Public License 00024 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00025 * 00026 ******************************************************************************/ 00027 00028 #include <WNS/Python.hpp> 00029 00030 #include <WNS/pyconfig/Parser.hpp> 00031 #include <WNS/pyconfig/helper/Functions.hpp> 00032 00033 #include <WNS/Exception.hpp> 00034 #include <WNS/Assure.hpp> 00035 00036 #include <sstream> 00037 #include <iostream> 00038 00039 using namespace wns::pyconfig; 00040 00041 long Parser::serial = 0; 00042 00043 Parser::Parser() : 00044 View(), 00045 module_name() 00046 { 00047 this->initPython(); 00048 } // Parser 00049 00050 00051 Parser::Parser(const std::string& filename, const std::string& pathname) : 00052 View(), 00053 module_name() 00054 { 00055 this->initPython(); 00056 this->appendPath(pathname); 00057 this->load(filename); 00058 } // Parser 00059 00060 00061 Parser::~Parser() 00062 { 00063 } // ~Parser 00064 00065 00066 void 00067 Parser::load(const std::string &filename) 00068 { 00069 assure(PyErr_Occurred() == false, "Dirty python context!"); 00070 00071 FILE* fp = fopen(filename.c_str(), "r"); 00072 if(fp == NULL) 00073 { 00074 throw Exception(std::string("couldn't load ") 00075 + filename 00076 + ": " 00077 + strerror(errno)); 00078 } 00079 00080 PyObject* o = PyRun_File(fp, 00081 filename.c_str(), 00082 Py_file_input, 00083 this->dict.obj_, 00084 this->dict.obj_); 00085 fclose(fp); 00086 if(o == NULL) 00087 { 00088 std::cerr << "wns::pyconfig::Parser says:\n" 00089 << "Couldn't load config file '" << filename << "'.\n"; 00090 00091 showdown("Couldn't load config file '" + filename + "'"); 00092 } 00093 00094 Py_DECREF(o); 00095 this->viewExpression = filename; 00096 } // load 00097 00098 00099 void 00100 Parser::loadString(const std::string &s) 00101 { 00102 assure(PyErr_Occurred() == false, "Dirty python context!"); 00103 00104 PyObject* o = PyRun_String(s.c_str(), 00105 Py_file_input, 00106 this->dict.obj_, 00107 this->dict.obj_); 00108 if(o == NULL) 00109 { 00110 std::cerr << "wns::pyconfig::Parser says:\n" 00111 << "Couldn't load config from string:\n" 00112 << s 00113 << "\n"; 00114 00115 showdown("Couldn't load from string."); 00116 } 00117 Py_DECREF(o); 00118 00119 this->viewExpression = "<string>"; 00120 } // loadString 00121 00122 00123 void 00124 Parser::initPython() 00125 { 00126 std::stringstream ss; 00127 ss << Parser::serial++; 00128 ss >> this->module_name; 00129 00130 PyObject* module = PyImport_AddModule(const_cast<char*>(this->module_name.c_str())); 00131 if(module == NULL) 00132 { 00133 throw Exception("couldn't open configfile"); // FIXME(fds) 00134 } 00135 00136 this->dict = Object(PyModule_GetDict(module)); 00137 this->dict.incref(); 00138 PyDict_SetItemString(this->dict.obj_, "__builtins__", PyImport_ImportModule("__builtin__")); 00139 } // initPython 00140 00141 00142 void 00143 Parser::appendPath(const std::string& pathname) 00144 { 00145 assure(PyErr_Occurred() == false, "Dirty python context!"); 00146 00147 std::stringstream ss; 00148 ss << "import sys\n" 00149 << "sys.path.append('" << pathname << "')\n"; 00150 if(-1 == PyRun_SimpleString(ss.str().c_str())) 00151 { 00152 std::cerr << "wns::pyconfig::Parser says:\n" 00153 << "Couldn't add path '" + pathname + "' to sys.path.\n"; 00154 00155 showdown("Couldn't add path."); 00156 } 00157 } // addPath 00158 00159 00160 View 00161 Parser::fromString(const std::string& s) 00162 { 00163 return helper::createViewFromString(s); 00164 } // fromString 00165 00166
1.5.5