Пример #1
0
//=============================================================================
// METHOD    : SPELLwsListDataHandler::read()
//=============================================================================
void SPELLwsListDataHandler::read()
{
	// Load the number of items
	unsigned int numItems = getStorage()->loadLong();

	// Create a list
	PyObject* listObject = PyList_New(numItems);

	for( unsigned int index = 0; index < numItems; index++)
	{
		// Load the item code
		SPELLwsData::Code code = loadDataCode();
		// Create an appropriate handler
		SPELLwsDataHandler* handler = SPELLwsDataHandlerFactory::createDataHandler(code);
		handler->setStorage(getStorage());
		// Read the data
		handler->read();
		// Add the item to the list
		PyList_SetItem(listObject, index, handler->getObject());
		delete handler;
	}

	// Set it as associated object
	setObject( listObject );
}
//=============================================================================
// METHOD    : SPELLwsDictDataHandler::read()
//=============================================================================
void SPELLwsDictDataHandler::read()
{
	DEBUG("[DDH] Loading dictionary items");
	// Load the number of items
	int numItems = getStorage()->loadLong();
	DEBUG("[DDH] Number of items " + ISTR(numItems));

	if (numItems == -1)
	{
		setObject(NULL);
		return;
	}

	// Create a dictionary
	PyObject* dictObject = PyDict_New();

	for( unsigned int index = 0; index < (unsigned) numItems; index++)
	{
		// We know that first an Object comes, as the key. So make the handler directly.
		SPELLwsObjectDataHandler keyHandler(NULL);
		keyHandler.setStorage(getStorage());
		DEBUG("		[DDH] Loading key");
		keyHandler.read();
		PyObject* key = keyHandler.getObject();
		DEBUG("		[DDH] Loaded key " + PYREPR(key));

		// Load the item code
		DEBUG("		[DDH] Loading data code");
		SPELLwsData::Code code = loadDataCode();
		DEBUG("		[DDH] Loaded data code " + SPELLwsData::codeStr(code));

		if (code == SPELLwsData::DATA_CUSTOM_TYPE )
		{
			std::string msg = "WARNING! warm start not supported for custom Python types (" + PYREPR(key) + ")";
			LOG_WARN(msg);
			SPELLexecutor::instance().getCIF().warning(msg);
			getStorage()->loadObject(); // Load none
			PyDict_SetItem(dictObject, key, Py_None);
		}
		else
		{
			// Create an appropriate handler
			SPELLwsDataHandler* handler = SPELLwsDataHandlerFactory::createDataHandler(code);
			handler->setStorage(getStorage());
			try
			{
				// Read the data
				DEBUG("		[DDH] Loading value");
				handler->read();
				DEBUG("		[DDH] Value loaded " + PYREPR(handler->getObject()));
				// Add the item to the dictionary
				PyDict_SetItem(dictObject, key, handler->getObject());
			}
			catch(SPELLcoreException& ex)
			{
				std::string msg = "Failed to recover dictionary item " + PYREPR(key) + ": " + ex.what();
				LOG_WARN(msg);
				SPELLexecutor::instance().getCIF().warning(msg);
				PyDict_SetItem(dictObject, key, Py_None);
			}
			delete handler;
			DEBUG("     [DDH] ");
		}
	}

	DEBUG("[DDH] Dictionary loaded");

	// Set it as associated object
	setObject( dictObject );
}