JobOptionsSvc.cpp
Go to the documentation of this file.
1 // ============================================================================
2 // Local:
3 // ============================================================================
4 #include "JobOptionsSvc.h"
5 #include "Analyzer.h"
6 #include "Messages.h"
7 #include "Catalog.h"
8 #include "Units.h"
9 #include "PragmaOptions.h"
10 #include "Node.h"
11 // ============================================================================
12 // Gaudi:
13 // ============================================================================
14 #include "GaudiKernel/IProperty.h"
15 #include "GaudiKernel/Property.h"
16 
17 #include "GaudiKernel/MsgStream.h"
18 #include "GaudiKernel/System.h"
19 // ============================================================================
21 // ============================================================================
22 // Namespace aliases:
23 // ============================================================================
24 namespace gp = Gaudi::Parsers;
25 // ============================================================================
27  base_class(name,svc)
28 {
29  m_pmgr.addRef(); // make sure the refCount doesn't go to zero too soon...
30  std::string tmp ;
31  tmp = System::getEnv ( "JOBOPTSEARCHPATH" ) ;
32  if ( !tmp.empty() && ("UNKNOWN" != tmp) ) { m_dir_search_path = tmp ; }
33  tmp = System::getEnv ( "JOBOPTSDUMPFILE" ) ;
34  if ( !tmp.empty() && ("UNKNOWN" != tmp) ) { m_dump = tmp ; }
35 
36  m_pmgr.declareProperty( "TYPE" , m_source_type ) ;
37  m_pmgr.declareProperty( "PATH" , m_source_path ) ;
38  m_pmgr.declareProperty( "SEARCHPATH" , m_dir_search_path ) ;
39  m_pmgr.declareProperty( "DUMPFILE" , m_dump ) ;
40 }
41 // ============================================================================
43 {
44  return m_pmgr.setProperty( p );
45 }
46 // ============================================================================
48 {
49  return m_pmgr.getProperty( p );
50 }
51 // ============================================================================
53 {
54  // Call base class initializer
56  if( !sc.isSuccess() ) return sc;
57  // Read the job options if needed
58  if ( this->m_source_type == "NONE" ) {
60  } else {
62  }
63  return sc;
64 }
65 
66 // ============================================================================
68 ( const std::string& client,
69  const Property& property )
70 {
71  std::unique_ptr<Property> p { new StringProperty ( property.name(), "" ) } ;
72  if ( !property.load( *p ) ) { return StatusCode::FAILURE ; }
73  return m_svc_catalog.addProperty( client , p.release() );
74 }
75 // ============================================================================
78 ( const std::string& client,
79  const std::string& name )
80 {
81  return m_svc_catalog.removeProperty(client,name);
82 }
83 // ============================================================================
85 JobOptionsSvc::getProperties( const std::string& client) const
86 {
87  return m_svc_catalog.getProperties(client);
88 }
89 // ============================================================================
90 StatusCode JobOptionsSvc::setMyProperties( const std::string& client,
91  IProperty* myInt )
92 {
93  const SvcCatalog::PropertiesT* props =
95 
96  if ( !props ){ return StatusCode::SUCCESS; }
97 
98  bool fail = false;
99  for ( const auto& cur : *props )
100  {
101  StatusCode sc = myInt->setProperty ( *cur ) ;
102  if ( sc.isFailure() )
103  {
104  MsgStream my_log( this->msgSvc(), this->name() );
105  my_log
106  << MSG::ERROR
107  << "Unable to set the property '" << cur->name() << "'"
108  << " of '" << client << "'. "
109  << "Check option and algorithm names, type and bounds."
110  << endmsg;
111  fail = true;
112  }
113  }
114  return fail ? StatusCode::FAILURE : StatusCode::SUCCESS ;
115 }
116 
118 std::vector<std::string> JobOptionsSvc::getClients() const {
119  return m_svc_catalog.getClients();
120 }
121 
122 
123 void JobOptionsSvc::dump (const std::string& file,
124  const gp::Catalog& catalog) const {
125  MsgStream log ( msgSvc() , name() ) ;
126  std::ofstream out
127  ( file.c_str() , std::ios_base::out | std::ios_base::trunc ) ;
128  // perform the actual dumping
129  if ( !out ) {
130  log << MSG::ERROR << "Unable to open dump-file \""+file+"\"" << endmsg ;
131  return ; // RETURN
132  }
133  else {
134  log << MSG::INFO << "Properties are dumped into \""+file+"\"" << endmsg ;
135  }
136  // perform the actual dump:
137  out << catalog;
138 }
139 
140 void JobOptionsSvc::fillServiceCatalog(const gp::Catalog& catalog) {
141  for (const auto& client : catalog) {
142  for (const auto& current : client.second ) {
143  addPropertyToCatalogue ( client.first ,
144  StringProperty{ current.NameInClient(),
145  current.ValueAsString() } );
146  }
147  }
148 }
149 
151  const std::string& path) {
152  std::string search_path = path;
153  if ( search_path.empty() && !m_dir_search_path.empty() )
154  { search_path = m_dir_search_path ; }
155  //
156  MsgStream my_log( this->msgSvc(), this->name() );
157  if (UNLIKELY(outputLevel() <= MSG::DEBUG))
158  my_log << MSG::DEBUG // debug
159  << "Reading options from the file "
160  << "'" << file << "'" << endmsg;
161  gp::Messages messages(my_log);
162  gp::Catalog catalog;
163  gp::Units units;
164  gp::PragmaOptions pragma;
165  gp::Node ast;
166  StatusCode sc = gp::ReadOptions(file, path, &messages, &catalog, &units,
167  &pragma, &ast);
168 
169  // --------------------------------------------------------------------------
170  if ( sc.isSuccess() )
171  {
172  if (pragma.IsPrintOptions()) {
173  my_log << MSG::INFO << "Print options" << std::endl << catalog
174  << endmsg;
175  }
176  if (pragma.IsPrintTree()) {
177  my_log << MSG::INFO << "Print tree:" << std::endl << ast.ToString()
178  << endmsg;
179  }
180  if (pragma.HasDumpFile()) {
181  dump(pragma.dumpFile(), catalog);
182  }
183  my_log << MSG::INFO
184  << "Job options successfully read in from " << file << endmsg;
185  fillServiceCatalog(catalog);
186  }
187  else
188  {
189  my_log << MSG::FATAL << "Job options errors."<< endmsg;
190  }
191  // ----------------------------------------------------------------------------
192  return sc;
193 }
StatusCode setProperty(const Property &p)
IProperty implementation (needed for initialisation)
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
StatusCode initialize() override
Definition: Service.cpp:62
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
std::vector< std::string > getClients() const override
Get the list of clients.
std::string m_source_type
Definition: JobOptionsSvc.h:75
const PropertiesT * getProperties(const std::string &client) const
Definition: SvcCatalog.cpp:65
const std::string & name() const
property name
Definition: Property.h:45
StatusCode setProperty(const Property &p) override
set the property form another property
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:76
SvcCatalog m_svc_catalog
Definition: JobOptionsSvc.h:78
list path
Definition: __init__.py:15
STL namespace.
virtual bool load(Property &dest) const =0
export the property value to the destination
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:416
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
std::vector< std::string > getClients() const
Definition: SvcCatalog.cpp:67
std::string m_dir_search_path
Definition: JobOptionsSvc.h:76
StatusCode getProperty(Property *p) const override
get the property
void dump(const std::string &file, const Gaudi::Parsers::Catalog &catalog) const
StatusCode setMyProperties(const std::string &client, IProperty *me) override
Override default properties of the calling client.
StatusCode getProperty(Property *p) const
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
virtual StatusCode setProperty(const Property &p)=0
Set the property by property.
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
Definition: System.cpp:619
SimpleProperty< std::string > StringProperty
Definition: Property.h:718
list file
Definition: ana.py:160
Property base class allowing Property* collections to be "homogeneous".
Definition: Property.h:38
Base class used to extend a class implementing other interfaces.
Definition: extends.h:10
const std::vector< const Property * > * getProperties(const std::string &client) const override
Get the properties associated to a given client.
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 addPropertyToCatalogue(const std::string &client, const Property &property) override
Add a property into the JobOptions catalog.
StatusCode initialize() override
PropertyMgr m_pmgr
dump the content of Properties catalog to the predefined file
Definition: JobOptionsSvc.h:73
std::vector< const Property * > PropertiesT
Definition: JobOptionsSvc.h:22
#define UNLIKELY(x)
Definition: Kernel.h:126
std::vector< const Property * > PropertiesT
Definition: SvcCatalog.h:28
The IProperty is the basic interface for all components which have properties that can be set or get...
Definition: IProperty.h:21
std::string m_source_path
Definition: JobOptionsSvc.h:74
Helper functions to set/get the application return code.
Definition: __init__.py:1