1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
/*
* This source file is part of libRocket, the HTML/CSS Interface Middleware
*
* For the latest information, see http://www.librocket.com
*
* Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include "precompiled.h"
#include "ElementDocumentWrapper.h"
#include "../../../Include/Rocket/Core/Stream.h"
#include "../../../Include/Rocket/Core/Python/Utilities.h"
namespace Rocket {
namespace Core {
namespace Python {
ElementDocumentWrapper::ElementDocumentWrapper(PyObject* self, const char* tag) : ElementWrapper< ElementDocument >(self, tag)
{
Rocket::Core::String module_id(32, "document_%x", this);
// Create a new module
module = PyModule_New(module_id.CString());
module_namespace = PyModule_GetDict(module);
// Merging main into the module
PyObject* builtins = PyImport_AddModule("__main__");
PyObject* builtins_dict = PyModule_GetDict( builtins);
PyDict_Merge(module_namespace, builtins_dict, 0);
// Insert the document global
PyDict_SetItemString(module_namespace, "document", self);
}
ElementDocumentWrapper::~ElementDocumentWrapper()
{
// Release the module
Py_DECREF(module);
}
void ElementDocumentWrapper::LoadScript(Rocket::Core::Stream* stream, const Rocket::Core::String& source_name)
{
// If theres a source, check if the code is already loaded and just reuse it
if (!source_name.Empty())
{
Rocket::Core::String module_name = Rocket::Core::String(source_name).Replace("/", "_");
module_name = module_name.Replace("\\", "_");
module_name = module_name.Replace(".py", "");
PyObject* modules = PyImport_GetModuleDict();
PyObject* merge_module = PyDict_GetItemString(modules, module_name.CString());
#ifdef ROCKET_DEBUG
// In debug builds, force module to NULL so that scripts are always reloaded
merge_module = NULL;
#else
if (merge_module)
{
Py_INCREF(merge_module);
}
#endif
if (!merge_module)
{
// Compile the code as a python module
Rocket::Core::String source_buffer;
PreprocessCode(source_buffer, stream);
PyObject* code = Py_CompileString(source_buffer.CString(), source_name.CString(), Py_file_input);
if (code)
{
merge_module = PyImport_ExecCodeModule((char*)module_name.CString(), code);
Py_DECREF(code);
}
}
if (merge_module)
{
PyObject* dict = PyModule_GetDict(merge_module);
PyDict_Merge(module_namespace, dict, 0);
Py_DECREF(merge_module);
}
else
{
Rocket::Core::Python::Utilities::PrintError();
}
}
else
{
// Compile directly onto the python module
Rocket::Core::String source_buffer;
PreprocessCode(source_buffer, stream);
PyObject* result = PyRun_String(source_buffer.CString(), Py_file_input, module_namespace, module_namespace);
if ( !result )
{
Rocket::Core::Python::Utilities::PrintError();
}
else
{
Py_DECREF(result);
}
}
}
PyObject* ElementDocumentWrapper::GetModuleNamespace()
{
return module_namespace;
}
void ElementDocumentWrapper::PreprocessCode(Rocket::Core::String &code, Rocket::Core::Stream *stream)
{
// Load in the script
Rocket::Core::String buffer;
stream->Read(buffer, stream->Length());
// Strip comments and build up code
code = "";
size_t i = 0;
size_t line_start = 0;
enum ParseState { START, COMMENT, DATA };
ParseState state = START;
while (i < buffer.Length())
{
// Python doesn't like \r's, strip them
if (buffer[i] == '\r')
{
buffer.Erase(i, 1);
// Make sure we get out if there are no characters left
if (i == buffer.Length())
continue;
}
switch (state)
{
case START:
{
// Check for the start of comments or non whitespace data
if (buffer[i] == '#')
state = COMMENT;
else if (!Rocket::Core::StringUtilities::IsWhitespace(buffer[i]))
state = DATA;
}
break;
default:
{
// If we've hit the end of the line, process as required
if (buffer[i] == '\n')
{
if (state == DATA)
code += buffer.Substring(line_start, i - line_start + 1);
state = START;
line_start = i + 1;
}
}
break;
}
i++;
}
}
}
}
}
|