CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/CondCore/Utilities/plugins/CondPyWrappers.cc

Go to the documentation of this file.
00001 // python wrappers for CondDB
00002 #include "CondCore/DBCommon/interface/Exception.h"
00003 #include "CondCore/Utilities/interface/CondPyInterface.h"
00004 #include "CondCore/IOVService/interface/IOVProxy.h"
00005 #include "CondCore/DBCommon/interface/LogDBEntry.h"
00006 #include "CondFormats/Common/interface/TimeConversions.h"
00007 
00008 
00009 #include "CondCore/DBCommon/interface/ClassID.h"
00010 #include "CondCore/DBCommon/interface/Exception.h"
00011 
00012 #include "FWCore/PluginManager/interface/PluginManager.h"
00013 #include "FWCore/PluginManager/interface/standard.h"
00014 #include "CondCore/TagCollection/interface/TagCollectionRetriever.h"
00015 
00016 #include <boost/python.hpp>
00017 #include <boost/python/suite/indexing/vector_indexing_suite.hpp>
00018 
00019 using namespace boost::python;
00020 
00021 #include<iostream>
00022 
00023 namespace {
00024   
00025   // decode token
00026   std::string classID(std::string const & token) {
00027     static std::string const clid("CLID=");
00028     std::string::size_type s = token.find(clid) + clid.size();
00029     std::string::size_type e = token.find(']',s);
00030     return token.substr(s,e-s);
00031 
00032   }
00033   // find and return
00034   boost::shared_ptr<cond::ClassInfo> pyInfo(std::string const & token) {
00035     //    topinit();    
00036     static std::string const prefix = cond::idCategories::pythonIDCategory + "/";
00037     std::string pluginName = prefix + classID(token);
00038     return boost::shared_ptr<cond::ClassInfo>(cond::ClassInfoFactory::get()->create(pluginName));
00039   }
00040   
00041   std::string moduleNameByTag(cond::CondDB & db, std::string const & tag) {
00042     //topinit();    
00043     cond::IOVProxy iov = db.iov(tag);
00044     if (0==iov.size()) return std::string();
00045     return pyInfo(iov.begin()->wrapperToken())->resource();
00046   }
00047 
00048   std::string moduleNameByToken(std::string const & token) {
00049     //topinit();    
00050     if (token.empty()) return std::string();
00051     return pyInfo(token)->resource();
00052   }
00053   
00054   std::string moduleName(cond::CondDB & db, std::string const & ss) {
00055     //topinit();    
00056     //assume tags never start with '['
00057     if (ss[0]=='[') return moduleNameByToken(ss);
00058     return  moduleNameByTag(db,ss);
00059   }
00060 
00061 
00062 //  exceptionTranslator(const edm::Exception & e)
00063 //  {
00064 //    PyErr_SetString(PyExc_RuntimeError, e.what());
00065 //  }
00066   void exceptionTranslator(const std::exception & e)
00067   {
00068     PyErr_SetString(PyExc_RuntimeError, e.what());
00069   }
00070 
00071 
00072     boost::python::tuple
00073     getLogState(cond::LogDBEntry& l)
00074     {
00075       return boost::python::make_tuple(
00076                                        l.logId,
00077                                        l.destinationDB,
00078                                        l.provenance,
00079                                        l.usertext,
00080                                        l.iovtag,
00081                                        l.iovtimetype,
00082                                        l.payloadIdx,
00083                                        l.payloadName,
00084                                        l.payloadToken,
00085                                        l.payloadContainer,
00086                                        l.exectime,
00087                                        l.execmessage
00088                                        );
00089     }
00090  
00091     boost::python::tuple
00092     getTagState(cond::TagMetadata& l)
00093     {
00094       return boost::python::make_tuple(
00095                                        l.tag,
00096                                        l.pfn,
00097                                        l.recordname,
00098                                        l.labelname,            
00099                                        l.objectname
00100                                        );
00101     }
00102 
00103 
00104   void append2VS(std::vector<std::string> & v, std::string s) {
00105     v.push_back(s);
00106   }
00107 
00108   boost::python::tuple unpackTime(cond::Time_t iValue) {
00109     cond::UnpackedTime l = cond::time::unpack(iValue);
00110     return boost::python::make_tuple(l.first,l.second);
00111   }
00112 
00113 }
00114 
00115 BOOST_PYTHON_MODULE(pluginCondDBPyInterface) {
00116 
00117   def("append2VS",&append2VS);
00118 
00119   def("unpackTime",&unpackTime);
00120 
00121   class_<cond::LogDBEntry>("LogDBEntry")
00122     .def("getState",getLogState)
00123     .def_readonly("logId",  &cond::LogDBEntry::logId)
00124     .def_readonly("destinationDB",   &cond::LogDBEntry::destinationDB)   
00125     .def_readonly("provenance",  &cond::LogDBEntry::provenance)
00126     .def_readonly("usertext",  &cond::LogDBEntry::usertext)
00127     .def_readonly("iovtag",  &cond::LogDBEntry::iovtag)
00128     .def_readonly("iovtimetype",   &cond::LogDBEntry::iovtimetype)
00129     .def_readonly("payloadIdx",   &cond::LogDBEntry::payloadIdx)
00130     .def_readonly("payloadName",   &cond::LogDBEntry::payloadName)
00131     .def_readonly("payloadToken",   &cond::LogDBEntry::payloadToken)
00132     .def_readonly("payloadContainer",   &cond::LogDBEntry::payloadContainer)
00133     .def_readonly("exectime",   &cond::LogDBEntry::exectime)
00134     .def_readonly("execmessage",  &cond::LogDBEntry::execmessage)
00135     ;
00136   
00137   class_<cond::TagMetadata>("TagEntry")
00138     .def("getState",getTagState)
00139     .def_readonly("tag", &cond::TagMetadata::tag)
00140     .def_readonly("pfn", &cond::TagMetadata::pfn)
00141     .def_readonly("record", &cond::TagMetadata::recordname)
00142     .def_readonly("label", &cond::TagMetadata::labelname)
00143     .def_readonly("object", &cond::TagMetadata::objectname)
00144     ;
00145   
00146   class_<cond::GlobalTag >("GlobalTag", init<>())
00147     .def("size", &cond::GlobalTag::size)
00148     .add_property("elements",  boost::python::range( &cond::GlobalTag::begin,  &cond::GlobalTag::end))
00149     ;
00150 
00151   class_<std::vector<std::string> >("VString")
00152     .def(vector_indexing_suite<std::vector<std::string> >())
00153     ;
00154 
00155   class_<std::vector<float> >("VFloat")
00156     .def(vector_indexing_suite<std::vector<float> >())
00157     ;
00158 
00159   class_<std::vector<int> >("VInt")
00160     .def(vector_indexing_suite<std::vector<int> >())
00161     ;
00162    
00163   class_<cond::IOVElementProxy>("IOVElement", init<>())
00164     .def("since", &cond::IOVElementProxy::since)
00165     .def("till", &cond::IOVElementProxy::till)
00166     .def("payloadToken", &cond::IOVElementProxy::wrapperToken, return_value_policy<copy_const_reference>())
00167     ;
00168   
00169   enum_<cond::TimeType>("timetype")
00170     .value("runnumber",cond::runnumber)
00171     .value("timestamp",cond::timestamp)
00172     .value("lumiid",cond::lumiid)
00173     .value("userid",cond::userid)
00174     ;
00175   
00176   class_<cond::IOVProxy>("IOV", init<>())
00177     .def("size", &cond::IOVProxy::size)
00178     .def("resetRange", &cond::IOVProxy::resetRange)
00179     .def("setRange", &cond::IOVProxy::setRange)
00180     .def("head", &cond::IOVProxy::head)
00181     .def("tail", &cond::IOVProxy::tail)
00182     .def("timetype", &cond::IOVProxy::timetype)
00183     .def("payloadContainerName", &cond::IOVProxy::payloadContainerName)
00184     .def("comment", &cond::IOVProxy::comment)
00185     .def("revision",&cond::IOVProxy::revision)
00186     .def("timestamp",&cond::IOVProxy::timestamp)
00187     .add_property("elements", boost::python::range( &cond::IOVProxy::begin,  &cond::IOVProxy::end))
00188     ;
00189   
00190   
00191   class_<cond::FWIncantation>("FWIncantation", init<>());
00192   
00193   class_<cond::CondDB>("CondDB", init<>())
00194     .def("allTags", &cond::CondDB::allTags)
00195     .def("iov", &cond::CondDB::iov)
00196     .def("iovWithLib", &cond::CondDB::iovWithLib)
00197     .def("payLoad", &cond::CondDB::payLoad)
00198     .def("moduleName",moduleName)
00199     .def("lastLogEntry", &cond::CondDB::lastLogEntry)
00200     .def("lastLogEntryOK", &cond::CondDB::lastLogEntryOK)
00201     ;
00202   
00203 
00204   class_<cond::RDBMS>("RDBMS", init<>())
00205     .def(init<std::string>())
00206     .def(init<std::string, std::string>())
00207     .def("setLogger",&cond::RDBMS::setLogger)
00208     .def("getDB", &cond::RDBMS::getDB)
00209     .def("getReadOnlyDB", &cond::RDBMS::getReadOnlyDB)
00210     .def("globalTag",  &cond::RDBMS::globalTag, return_value_policy<copy_const_reference>())
00211     ;
00212 
00213 
00214 //  register_exception_translator<edm::Exception>(exceptionTranslator);
00215   register_exception_translator<std::exception>(exceptionTranslator);
00216 
00217 
00218 }