The Gaudi Framework  v33r1 (b1225454)
JobOptionsSvc.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2020 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 // ============================================================================
12 // Local:
13 // ============================================================================
14 #include "JobOptionsSvc.h"
15 #include "Analyzer.h"
16 #include "Catalog.h"
17 #include "Messages.h"
18 #include "Node.h"
19 #include "PragmaOptions.h"
20 #include "PythonConfig.h"
21 #include "Units.h"
22 // ============================================================================
23 // Gaudi:
24 // ============================================================================
25 #include "GaudiKernel/MsgStream.h"
26 #include "GaudiKernel/System.h"
27 // ============================================================================
29 // ============================================================================
30 // Namespace aliases:
31 // ============================================================================
32 namespace gp = Gaudi::Parsers;
33 // ============================================================================
35  if ( System::isEnvSet( "JOBOPTSEARCHPATH" ) ) m_dir_search_path = System::getEnv( "JOBOPTSEARCHPATH" );
36  if ( System::isEnvSet( "JOBOPTSDUMPFILE" ) ) m_dump = System::getEnv( "JOBOPTSDUMPFILE" );
37 }
38 // ============================================================================
40  // Call base class initializer
42  // Read the job options if needed
43  if ( sc ) {
44  if ( m_source_type == "NONE" ) {
45  return sc;
46  } else if ( m_source_type == "PYTHON" ) {
47  PythonConfig conf( this );
49  } else {
51  }
52  }
53  return sc;
54 }
55 
56 // ============================================================================
58  if ( !m_dump.empty() ) { dump( m_dump, m_svc_catalog ); }
59  return StatusCode::SUCCESS;
60 }
61 
62 // ============================================================================
64  const Gaudi::Details::PropertyBase& property ) {
65  auto p = std::make_unique<Gaudi::Property<std::string>>( property.name(), "" );
66  return property.load( *p ) ? m_svc_catalog.addProperty( client, p.release() ) : StatusCode::FAILURE;
67 }
68 // ============================================================================
70  return m_svc_catalog.removeProperty( client, name );
71 }
72 // ============================================================================
74  return m_svc_catalog.getProperties( client );
75 }
76 // ============================================================================
78  const auto* props = m_svc_catalog.getProperties( client );
79  if ( !props ) { return StatusCode::SUCCESS; }
80 
81  bool fail = false;
82  for ( const auto& cur : *props ) {
83  StatusCode sc = myInt->setProperty( *cur );
84  if ( sc.isFailure() ) {
85  error() << "Unable to set the property '" << cur->name() << "'"
86  << " of '" << client << "'. "
87  << "Check option and algorithm names, type and bounds." << endmsg;
88  fail = true;
89  }
90  }
92 }
93 
96 
97 template <typename C>
98 void JobOptionsSvc::dump( const std::string& file, const C& catalog ) const {
99  std::ofstream out( file, std::ios_base::out | std::ios_base::trunc );
100  if ( !out ) {
101  error() << "Unable to open dump-file \"" + file + "\"" << endmsg;
102  return; // RETURN
103  }
104  info() << "Properties are dumped into \"" + file + "\"" << endmsg;
105  // perform the actual dump:
106  out << catalog;
107 }
108 
109 void JobOptionsSvc::fillServiceCatalog( const gp::Catalog& catalog ) {
110  for ( const auto& client : catalog ) {
111  for ( const auto& current : client.second ) {
112  if ( auto sc = addPropertyToCatalogue(
113  client.first, Gaudi::Property<std::string>{current.NameInClient(), current.ValueAsString()} );
114  !sc )
115  throw GaudiException( "Failed to fill the catalog", name(), sc );
116  }
117  }
118 }
119 
121  std::string search_path = path;
122  if ( search_path.empty() && !m_dir_search_path.empty() ) { search_path = m_dir_search_path; }
123  //
124  if ( msgLevel( MSG::DEBUG ) )
125  debug() << "Reading options from the file "
126  << "'" << file << "'" << endmsg;
127  gp::Messages messages( msgStream() );
128  gp::Catalog catalog;
129  gp::Units units;
130  gp::PragmaOptions pragma;
131  gp::Node ast;
132  StatusCode sc = gp::ReadOptions( file, path, &messages, &catalog, &units, &pragma, &ast ) ? StatusCode::SUCCESS
134 
135  // --------------------------------------------------------------------------
136  if ( sc.isSuccess() ) {
137  if ( pragma.IsPrintOptions() ) { info() << "Print options" << std::endl << catalog << endmsg; }
138  if ( pragma.IsPrintTree() ) { info() << "Print tree:" << std::endl << ast.ToString() << endmsg; }
139  if ( pragma.HasDumpFile() ) dump( pragma.dumpFile(), catalog );
140  info() << "Job options successfully read in from " << file << endmsg;
141  fillServiceCatalog( catalog );
142  } else {
143  fatal() << "Job options errors." << endmsg;
144  }
145  // ----------------------------------------------------------------------------
146  return sc;
147 }
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
Definition: System.cpp:379
StatusCode initialize() override
Definition: Service.cpp:70
Gaudi::Property< std::string > m_source_path
Definition: JobOptionsSvc.h:82
T empty(T... args)
void fillServiceCatalog(const Gaudi::Parsers::Catalog &catalog)
Define general base for Gaudi exception.
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:35
std::vector< std::string > getClients() const override
Get the list of clients.
const PropertiesT * getProperties(const std::string &client) const
Definition: SvcCatalog.cpp:69
Implementation of property with value of concrete type.
Definition: Property.h:370
Gaudi::Property< std::string > m_pythonAction
Definition: JobOptionsSvc.h:85
SvcCatalog m_svc_catalog
Definition: JobOptionsSvc.h:88
T endl(T... args)
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
StatusCode addProperty(const std::string &client, const Gaudi::Details::PropertyBase *property)
Definition: SvcCatalog.cpp:46
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
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:387
StatusCode start() override
StatusCode evaluateConfig(const std::string &filename, const std::string &preAction, const std::string &postAction)
StatusCode removeProperty(const std::string &client, const std::string &name)
Definition: SvcCatalog.cpp:57
Gaudi::Property< std::string > m_dump
Definition: JobOptionsSvc.h:84
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
void dump(const std::string &file, const C &catalog) const
dump properties catalog to file
STL class.
#define DECLARE_COMPONENT(type)
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:284
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.
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
Gaudi::Property< std::string > m_pythonParams
Definition: JobOptionsSvc.h:86
Gaudi::Property< std::string > m_dir_search_path
Definition: JobOptionsSvc.h:83
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:42
MsgStream & msgStream() const
Return an uninitialized MsgStream.
bool isSuccess() const
Definition: StatusCode.h:365
std::vector< std::string > getClients() const
Definition: SvcCatalog.cpp:73
GAUDI_API bool isEnvSet(const char *var)
Check if an environment variable is set or not.
Definition: System.cpp:399
STL class.
StatusCode readOptions(const std::string &file, const std::string &path="") override
look for file 'file' into search path 'path' 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.
StatusCode initialize() override
constexpr static const auto FAILURE
Definition: StatusCode.h:101
Gaudi::Property< std::string > m_source_type
Definition: JobOptionsSvc.h:81
JobOptionsSvc(const std::string &name, ISvcLocator *svc)
const std::vector< Gaudi::Details::PropertyBase * > & getProperties() const override
get all properties
bool isFailure() const
Definition: StatusCode.h:145
StatusCode addPropertyToCatalogue(const std::string &client, const Gaudi::Details::PropertyBase &property) override
Add a property into the JobOptions catalog.
The IProperty is the basic interface for all components which have properties that can be set or get.
Definition: IProperty.h:30
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202