File: FileGenerator.cpp

package info (click to toggle)
yade 2026.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 34,448 kB
  • sloc: cpp: 97,645; python: 52,173; sh: 677; makefile: 162
file content (71 lines) | stat: -rw-r--r-- 2,861 bytes parent folder | download | duplicates (3)
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
/*************************************************************************
*  This program is free software; it is licensed under the terms of the  *
*  GNU General Public License v2 or later. See file LICENSE for details. *
*************************************************************************/

#include <lib/pyutil/gil.hpp>
#include <lib/serialization/ObjectIO.hpp>
#include <core/Omega.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

#include "FileGenerator.hpp"

namespace yade { // Cannot have #include directive inside.

CREATE_LOGGER(FileGenerator);

bool FileGenerator::generate(std::string& /*msg*/) { throw invalid_argument("Calling abstract FileGenerator::generate() does not make sense."); }

bool FileGenerator::generateAndSave(const string& outputFileName, string& message)
{
	bool status;
	message                      = "";
	boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
	try {
		status = generate(message); // will modify message
	} catch (std::exception& e) {
		LOG_FATAL("Unhandled exception: " << typeid(e).name() << " : " << e.what());
		//abort(); // use abort, since we may want to inspect core
		message = message + "Unhandled exception: " + typeid(e).name() + " : " + e.what();
		return false;
	}
	// generation wasn't successful
	if (status == false) return false;

	else {
		boost::posix_time::ptime         now2           = boost::posix_time::second_clock::local_time();
		boost::posix_time::time_duration generationTime = now2 - now; // generation time, without save time
		try {
			yade::ObjectIO::save(outputFileName, "scene", scene);
		} catch (const std::runtime_error& e) {
			message += std::string("File " + outputFileName + " cannot be saved: " + e.what());
			return false;
		}
		boost::posix_time::ptime         now3     = boost::posix_time::second_clock::local_time();
		boost::posix_time::time_duration saveTime = now3 - now2; // save time
		message                                   = std::string(
                                  "File " + outputFileName + " generated successfully."
                                  + "\ngeneration time: " + boost::posix_time::to_simple_string(generationTime)
                                  + "\nsave time: " + boost::posix_time::to_simple_string(saveTime) + "\n\n")
		        + message;
		return true;
	}
}

void FileGenerator::pyGenerate(const string& out)
{
	string message;
	bool   ret = generateAndSave(out, message);
	LOG_INFO((ret ? "SUCCESS:\n" : "FAILURE:\n") << message);
	if (ret == false) throw runtime_error(getClassName() + " reported error: " + message);
}
void FileGenerator::pyLoad()
{
	string xml(Omega::instance().tmpFilename() + ".xml.bz2");
	// LOG_DEBUG("Using temp file "<<xml);
	pyGenerate(xml);
	//this is ugly hack, yes...
	pyRunString("yade.wrapper.Omega().load('" + xml + "')");
}

} // namespace yade