CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch1/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 #include "CondCore/Utilities/interface/PayLoadInspector.h"
00020 
00021 #include <set>
00022 #include <vector>
00023 #include <string>
00024 #include <iostream>
00025 
00026 using namespace boost::python;
00027 
00028 namespace {
00029 
00030   // decode token
00031 //   std::string classID(std::string const & token) {
00032 //     static std::string const clid("CLID=");
00033 //     std::string::size_type s = token.find(clid) + clid.size();
00034 //     std::string::size_type e = token.find(']',s);
00035 //     return token.substr(s,e-s);
00036 //   }
00037 
00038 //   // find and return
00039 //   boost::shared_ptr<cond::ClassInfo> pyInfo(std::string const & token) {
00040 //     //    topinit();    
00041 //     static std::string const prefix = cond::idCategories::pythonIDCategory + "/";
00042 //     std::string pluginName = prefix + classID(token);
00043 //     std::cout << "############# pluginName: " << pluginName << std::endl;
00044 //     return boost::shared_ptr<cond::ClassInfo>(cond::ClassInfoFactory::get()->create(pluginName));
00045 //   }
00046   
00047 //   std::string moduleNameByTag(cond::CondDB & db, std::string const & tag) {
00048 //     //topinit();    
00049 //     std::cout << "****************** tag: " << tag << std::endl;
00050 //     cond::IOVProxy iov = db.iov(tag);
00051 //     if (0==iov.size()) return std::string();
00052     
00053 //     std::cout << "****************** resource: " << pyInfo(iov.begin()->token())->resource() << std::endl;
00054 //     return pyInfo(iov.begin()->token())->resource();
00055 //   }
00056 
00057 //   std::string moduleNameByToken(std::string const & token) {
00058 //     //topinit();   
00059 //     std::cout << "****************** token: " << token << std::endl;
00060 //     if (token.empty()) return std::string();
00061 //     std::cout << "****************** resource: " << pyInfo(token)->resource() << std::endl;
00062 //     return pyInfo(token)->resource();
00063 //   }
00064   
00065 //   std::string moduleName(cond::CondDB & db, std::string const & ss) {
00066 //     //topinit();    
00067 //     //assume tags never start with '['
00068 //     /*if (ss[0]=='[')*/ return moduleNameByToken(ss);
00069 //     //return  moduleNameByTag(db,ss);
00070 //   }
00071   
00072   std::vector<std::string> payloadContainers(cond::IOVProxy & iov) {
00073     //topinit();
00074     std::vector<std::string> v_classes;
00075     v_classes.insert(v_classes.end(),iov.payloadClasses().begin(),iov.payloadClasses().end());
00076     return v_classes;
00077   }
00078 
00079   std::vector<std::string> payloadModules(cond::CondDB & db, std::string const & tag) {
00080     //topinit();
00081     std::string const prefix = cond::idCategories::pythonIDCategory + "/";
00082     cond::IOVProxy iov = db.iov(tag);
00083     std::vector<std::string> v_modules;
00084     std::set<std::string>::const_iterator sBegin = iov.payloadClasses().begin();
00085     std::set<std::string>::const_iterator sEnd = iov.payloadClasses().end();
00086     for(std::set<std::string>::const_iterator s = sBegin; s != sEnd; ++s) {
00087       boost::shared_ptr<cond::ClassInfo> cInfo(cond::ClassInfoFactory::get()->create(prefix + (*s)));
00088       v_modules.push_back(cInfo->resource());
00089     }
00090     return v_modules;
00091   }
00092 
00093 
00094 //  exceptionTranslator(const edm::Exception & e)
00095 //  {
00096 //    PyErr_SetString(PyExc_RuntimeError, e.what());
00097 //  }
00098   void exceptionTranslator(const std::exception & e)
00099   {
00100     PyErr_SetString(PyExc_RuntimeError, e.what());
00101   }
00102 
00103 
00104     boost::python::tuple
00105     getLogState(cond::LogDBEntry& l)
00106     {
00107       return boost::python::make_tuple(
00108                                        l.logId,
00109                                        l.destinationDB,
00110                                        l.provenance,
00111                                        l.usertext,
00112                                        l.iovtag,
00113                                        l.iovtimetype,
00114                                        l.payloadIdx,
00115                                        l.payloadClass,
00116                                        l.payloadToken,
00117                                        l.exectime,
00118                                        l.execmessage
00119                                        );
00120     }
00121  
00122     boost::python::tuple
00123     getTagState(cond::TagMetadata& l)
00124     {
00125       return boost::python::make_tuple(
00126                                        l.tag,
00127                                        l.pfn,
00128                                        l.recordname,
00129                                        l.labelname,            
00130                                        l.objectname
00131                                        );
00132     }
00133 
00134 
00135   void append2VS(std::vector<std::string> & v, std::string s) {
00136     v.push_back(s);
00137   }
00138 
00139   boost::python::tuple unpackTime(cond::Time_t iValue) {
00140     cond::UnpackedTime l = cond::time::unpack(iValue);
00141     return boost::python::make_tuple(l.first,l.second);
00142   }
00143 
00144 }
00145 
00146 BOOST_PYTHON_MODULE(pluginCondDBPyInterface) {
00147 
00148   def("append2VS",&append2VS);
00149 
00150   def("unpackTime",&unpackTime);
00151 
00152   class_<cond::LogDBEntry>("LogDBEntry")
00153     .def("getState",getLogState)
00154     .def_readonly("logId",  &cond::LogDBEntry::logId)
00155     .def_readonly("destinationDB",   &cond::LogDBEntry::destinationDB)   
00156     .def_readonly("provenance",  &cond::LogDBEntry::provenance)
00157     .def_readonly("usertext",  &cond::LogDBEntry::usertext)
00158     .def_readonly("iovtag",  &cond::LogDBEntry::iovtag)
00159     .def_readonly("iovtimetype",   &cond::LogDBEntry::iovtimetype)
00160     .def_readonly("payloadIdx",   &cond::LogDBEntry::payloadIdx)
00161     .def_readonly("payloadClass",   &cond::LogDBEntry::payloadClass)
00162     .def_readonly("payloadToken",   &cond::LogDBEntry::payloadToken)
00163     .def_readonly("exectime",   &cond::LogDBEntry::exectime)
00164     .def_readonly("execmessage",  &cond::LogDBEntry::execmessage)
00165     ;
00166   
00167   class_<cond::TagMetadata>("TagEntry")
00168     .def("getState",getTagState)
00169     .def_readonly("tag", &cond::TagMetadata::tag)
00170     .def_readonly("pfn", &cond::TagMetadata::pfn)
00171     .def_readonly("record", &cond::TagMetadata::recordname)
00172     .def_readonly("label", &cond::TagMetadata::labelname)
00173     .def_readonly("object", &cond::TagMetadata::objectname)
00174     ;
00175   
00176   class_<cond::GlobalTag >("GlobalTag", init<>())
00177     .def("size", &cond::GlobalTag::size)
00178     .add_property("elements",  boost::python::range( &cond::GlobalTag::begin,  &cond::GlobalTag::end))
00179     ;
00180 
00181   class_<std::vector<std::string> >("VString")
00182     .def(vector_indexing_suite<std::vector<std::string> >())
00183     ;
00184 
00185   class_<std::vector<float> >("VFloat")
00186     .def(vector_indexing_suite<std::vector<float> >())
00187     ;
00188 
00189   class_<std::vector<int> >("VInt")
00190     .def(vector_indexing_suite<std::vector<int> >())
00191     ;
00192 
00193   enum_<cond::TimeType>("timetype")
00194     .value("runnumber",cond::runnumber)
00195     .value("timestamp",cond::timestamp)
00196     .value("lumiid",cond::lumiid)
00197     .value("hash",cond::hash)
00198     .value("userid",cond::userid)
00199     ;
00200   
00201   class_<cond::IOVElementProxy>("IOVElement", init<>())
00202     .def(init<cond::Time_t, cond::Time_t, std::string>())
00203     .def("since", &cond::IOVElementProxy::since)
00204     .def("till", &cond::IOVElementProxy::till)
00205     .def("payloadToken", &cond::IOVElementProxy::token, return_value_policy<copy_const_reference>())
00206     ;
00207   
00208   class_<cond::IOVRange>("IOVRange", init<>())
00209     .def("front", &cond::IOVRange::front)
00210     .def("back", &cond::IOVRange::back)
00211     .def("size", &cond::IOVRange::size)
00212     .add_property("elements", boost::python::range(&cond::IOVRange::begin, &cond::IOVRange::end))
00213     ;
00214   
00215   class_<cond::IOVProxy>("IOV", init<>())
00216     .def("token", &cond::IOVProxy::token, return_value_policy<copy_const_reference>())
00217     .def("head", &cond::IOVProxy::head)
00218     .def("tail", &cond::IOVProxy::tail)
00219     .def("range", &cond::IOVProxy::range)
00220     .def("rangeHead", &cond::IOVProxy::rangeHead)
00221     .def("rangeTail", &cond::IOVProxy::rangeTail)
00222     .def("size", &cond::IOVProxy::size)
00223     .def("timetype", &cond::IOVProxy::timetype)
00224     .def("firstSince", &cond::IOVProxy::firstSince)
00225     .def("lastTill", &cond::IOVProxy::lastTill)
00226     .def("payloadClasses", payloadContainers)
00227     .def("comment", &cond::IOVProxy::comment)
00228     .def("revision",&cond::IOVProxy::revision)
00229     .def("timestamp",&cond::IOVProxy::timestamp)
00230     .add_property("elements", boost::python::range(&cond::IOVProxy::begin, &cond::IOVProxy::end))
00231     ;
00232   
00233   class_<cond::FWIncantation>("FWIncantation", init<>());
00234   
00235   class_<cond::CondDB>("CondDB", init<>())
00236     .def("allTags", &cond::CondDB::allTags)
00237     .def("iov", &cond::CondDB::iov)
00238     .def("iovToken", &cond::CondDB::iovToken)
00239     .def("iovWithLib", &cond::CondDB::iovWithLib)
00240     .def("payLoad", &cond::CondDB::payLoad)
00241     .def("payloadModules",payloadModules)
00242     .def("lastLogEntry", &cond::CondDB::lastLogEntry)
00243     .def("lastLogEntryOK", &cond::CondDB::lastLogEntryOK)
00244     .def("startTransaction", &cond::CondDB::startTransaction)
00245     .def("startReadOnlyTransaction", &cond::CondDB::startReadOnlyTransaction)
00246     .def("commitTransaction", &cond::CondDB::commitTransaction)
00247     .def("closeSession", &cond::CondDB::closeSession)
00248     ;
00249   
00250 
00251   class_<cond::RDBMS>("RDBMS", init<>())
00252     .def(init<std::string>())
00253     .def(init<std::string, bool>())
00254     .def(init<std::string, std::string>())
00255     .def("setLogger",&cond::RDBMS::setLogger)
00256     .def("getDB", &cond::RDBMS::getDB)
00257     .def("getReadOnlyDB", &cond::RDBMS::getReadOnlyDB)
00258     .def("globalTag",  &cond::RDBMS::globalTag, return_value_policy<copy_const_reference>())
00259     ;
00260 
00261 //  register_exception_translator<edm::Exception>(exceptionTranslator);
00262   register_exception_translator<std::exception>(exceptionTranslator);
00263 
00264 
00265 }