The Gaudi Framework  v30r3 (a5ef0a68)
JobOptionsSvc.cpp
Go to the documentation of this file.
1 // ============================================================================
2 // Local:
3 // ============================================================================
4 #include "JobOptionsSvc.h"
5 #include "Analyzer.h"
6 #include "Catalog.h"
7 #include "Messages.h"
8 #include "Node.h"
9 #include "PragmaOptions.h"
10 #include "PythonConfig.h"
11 #include "Units.h"
12 // ============================================================================
13 // Gaudi:
14 // ============================================================================
15 #include "GaudiKernel/MsgStream.h"
16 #include "GaudiKernel/System.h"
17 // ============================================================================
19 // ============================================================================
20 // Namespace aliases:
21 // ============================================================================
22 namespace gp = Gaudi::Parsers;
23 // ============================================================================
24 JobOptionsSvc::JobOptionsSvc( const std::string& name, ISvcLocator* svc ) : base_class( name, svc )
25 {
26  if ( System::isEnvSet( "JOBOPTSEARCHPATH" ) ) m_dir_search_path = System::getEnv( "JOBOPTSEARCHPATH" );
27  if ( System::isEnvSet( "JOBOPTSDUMPFILE" ) ) m_dump = System::getEnv( "JOBOPTSDUMPFILE" );
28 }
29 // ============================================================================
31 {
32  // Call base class initializer
34  // Read the job options if needed
35  if ( sc ) {
36  if ( m_source_type == "NONE" ) {
37  return sc;
38  } else if ( m_source_type == "PYTHON" ) {
39  PythonConfig conf( this );
41  } else {
43  }
44  }
45  return sc;
46 }
47 
48 // ============================================================================
51 {
52  auto p = std::make_unique<Gaudi::Property<std::string>>( property.name(), "" );
53  return property.load( *p ) ? m_svc_catalog.addProperty( client, p.release() ) : StatusCode::FAILURE;
54 }
55 // ============================================================================
57 {
58  return m_svc_catalog.removeProperty( client, name );
59 }
60 // ============================================================================
62 {
63  return m_svc_catalog.getProperties( client );
64 }
65 // ============================================================================
67 {
68  const auto* props = m_svc_catalog.getProperties( client );
69  if ( !props ) {
70  return StatusCode::SUCCESS;
71  }
72 
73  bool fail = false;
74  for ( const auto& cur : *props ) {
75  StatusCode sc = myInt->setProperty( *cur );
76  if ( sc.isFailure() ) {
77  error() << "Unable to set the property '" << cur->name() << "'"
78  << " of '" << client << "'. "
79  << "Check option and algorithm names, type and bounds." << endmsg;
80  fail = true;
81  }
82  }
84 }
85 
88 
89 void JobOptionsSvc::dump( const std::string& file, const gp::Catalog& catalog ) const
90 {
91  std::ofstream out( file, std::ios_base::out | std::ios_base::trunc );
92  if ( !out ) {
93  error() << "Unable to open dump-file \"" + file + "\"" << endmsg;
94  return; // RETURN
95  }
96  info() << "Properties are dumped into \"" + file + "\"" << endmsg;
97  // perform the actual dump:
98  out << catalog;
99 }
100 
101 void JobOptionsSvc::fillServiceCatalog( const gp::Catalog& catalog )
102 {
103  for ( const auto& client : catalog ) {
104  for ( const auto& current : client.second ) {
105  addPropertyToCatalogue( client.first,
106  Gaudi::Property<std::string>{current.NameInClient(), current.ValueAsString()} );
107  }
108  }
109 }
110 
112 {
113  std::string search_path = path;
114  if ( search_path.empty() && !m_dir_search_path.empty() ) {
115  search_path = m_dir_search_path;
116  }
117  //
118  if ( msgLevel( MSG::DEBUG ) )
119  debug() << "Reading options from the file "
120  << "'" << file << "'" << endmsg;
121  gp::Messages messages( msgStream() );
122  gp::Catalog catalog;
123  gp::Units units;
124  gp::PragmaOptions pragma;
125  gp::Node ast;
126  StatusCode sc = gp::ReadOptions( file, path, &messages, &catalog, &units, &pragma, &ast ) ? StatusCode::SUCCESS
128 
129  // --------------------------------------------------------------------------
130  if ( sc.isSuccess() ) {
131  if ( pragma.IsPrintOptions() ) {
132  info() << "Print options" << std::endl << catalog << endmsg;
133  }
134  if ( pragma.IsPrintTree() ) {
135  info() << "Print tree:" << std::endl << ast.ToString() << endmsg;
136  }
137  if ( pragma.HasDumpFile() ) dump( pragma.dumpFile(), catalog );
138  info() << "Job options successfully read in from " << file << endmsg;
139  fillServiceCatalog( catalog );
140  } else {
141  fatal() << "Job options errors." << endmsg;
142  }
143  // ----------------------------------------------------------------------------
144  return sc;
145 }
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
Definition: System.cpp:411
constexpr static const auto FAILURE
Definition: StatusCode.h:88
StatusCode initialize() override
Definition: Service.cpp:63
Gaudi::Property< std::string > m_source_path
Definition: JobOptionsSvc.h:73
T empty(T...args)
void fillServiceCatalog(const Gaudi::Parsers::Catalog &catalog)
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:288
Gaudi::Details::PropertyBase * property(const std::string &name) const
Implementation of property with value of concrete type.
Definition: Property.h:381
Gaudi::Property< std::string > m_pythonAction
Definition: JobOptionsSvc.h:76
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
const PropertiesT * getProperties(const std::string &client) const
Definition: SvcCatalog.cpp:65
const std::vector< Gaudi::Details::PropertyBase * > & getProperties() const override
get all properties
bool isSuccess() const
Definition: StatusCode.h:287
SvcCatalog m_svc_catalog
Definition: JobOptionsSvc.h:79
T endl(T...args)
STL namespace.
StatusCode addProperty(const std::string &client, const Gaudi::Details::PropertyBase *property)
Definition: SvcCatalog.cpp:40
bool ReadOptions(const std::string &filename, const std::string &search_path, Messages *messages, Catalog *catalog, Units *units, PragmaOptions *pragma, Node *root)
Parse and analyze filename, save all messages and properties.
Definition: Analyzer.cpp:390
StatusCode evaluateConfig(const std::string &filename, const std::string &preAction, const std::string &postAction)
Definition: PythonConfig.cpp:8
bool isFailure() const
Definition: StatusCode.h:139
StatusCode removeProperty(const std::string &client, const std::string &name)
Definition: SvcCatalog.cpp:52
std::vector< std::string > getClients() const
Definition: SvcCatalog.cpp:70
STL class.
#define DECLARE_COMPONENT(type)
void dump(const std::string &file, const Gaudi::Parsers::Catalog &catalog) const
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
STL class.
virtual StatusCode setProperty(const Gaudi::Details::PropertyBase &p)=0
Set the property by property.
StatusCode setMyProperties(const std::string &client, IProperty *me) override
Override default properties of the calling client.
Gaudi::Property< std::string > m_pythonParams
Definition: JobOptionsSvc.h:77
Gaudi::Property< std::string > m_dir_search_path
Definition: JobOptionsSvc.h:74
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:51
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:32
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
GAUDI_API bool isEnvSet(const char *var)
Check if an environment variable is set or not.
Definition: System.cpp:433
STL class.
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
StatusCode readOptions(const std::string &file, const std::string &path="") override
look for file &#39;file&#39; into search path &#39;path&#39; and read it to update existing JobOptionsCatalogue ...
StatusCode removePropertyFromCatalogue(const std::string &client, const std::string &name) override
Remove a property from the JobOptions catalog.
MsgStream & msgStream() const
Return an uninitialized MsgStream.
StatusCode initialize() override
Gaudi::Property< std::string > m_source_type
dump the content of Properties catalog to the predefined file
Definition: JobOptionsSvc.h:72
StatusCode addPropertyToCatalogue(const std::string &client, const Gaudi::Details::PropertyBase &property) override
Add a property into the JobOptions catalog.
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
The IProperty is the basic interface for all components which have properties that can be set or get...
Definition: IProperty.h:20
Helper functions to set/get the application return code.
Definition: __init__.py:1
std::vector< std::string > getClients() const override
Get the list of clients.
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)