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 #include "PythonConfig.h"
12 // ============================================================================
13 // Gaudi:
14 // ============================================================================
15 #include "GaudiKernel/IProperty.h"
16 #include "GaudiKernel/Property.h"
17 
18 #include "GaudiKernel/MsgStream.h"
19 #include "GaudiKernel/System.h"
20 // ============================================================================
22 // ============================================================================
23 // Namespace aliases:
24 // ============================================================================
25 namespace gp = Gaudi::Parsers;
26 // ============================================================================
28  base_class(name,svc)
29 {
30  m_pmgr.addRef(); // make sure the refCount doesn't go to zero too soon...
31  std::string tmp ;
32  tmp = System::getEnv ( "JOBOPTSEARCHPATH" ) ;
33  if ( !tmp.empty() && ("UNKNOWN" != tmp) ) { m_dir_search_path = tmp ; }
34  tmp = System::getEnv ( "JOBOPTSDUMPFILE" ) ;
35  if ( !tmp.empty() && ("UNKNOWN" != tmp) ) { m_dump = tmp ; }
36 
37  m_pmgr.declareProperty( "TYPE" , m_source_type ) ;
38  m_pmgr.declareProperty( "PATH" , m_source_path ) ;
39  m_pmgr.declareProperty( "SEARCHPATH" , m_dir_search_path ) ;
40  m_pmgr.declareProperty( "DUMPFILE" , m_dump ) ;
41  m_pmgr.declareProperty( "PYTHONACTION" , m_pythonAction ) ;
42  m_pmgr.declareProperty( "PYTHONPARAMS" , m_pythonParams ) ;
43 }
44 // ============================================================================
46 {
47  return m_pmgr.setProperty( p );
48 }
49 // ============================================================================
51 {
52  return m_pmgr.getProperty( p );
53 }
54 // ============================================================================
56 {
57  // Call base class initializer
59  // Read the job options if needed
60  if (sc) {
61  if (m_source_type == "NONE") {
62  return sc;
63  } else if (m_source_type == "PYTHON") {
64  PythonConfig conf(this);
66  } else {
68  }
69  }
70  return sc;
71 }
72 
73 // ============================================================================
75 ( const std::string& client,
76  const Property& property )
77 {
78  std::unique_ptr<Property> p { new StringProperty ( property.name(), "" ) } ;
79  return property.load( *p ) ? m_svc_catalog.addProperty( client , p.release() )
81 }
82 // ============================================================================
85 ( const std::string& client,
86  const std::string& name )
87 {
88  return m_svc_catalog.removeProperty(client,name);
89 }
90 // ============================================================================
92 JobOptionsSvc::getProperties( const std::string& client) const
93 {
94  return m_svc_catalog.getProperties(client);
95 }
96 // ============================================================================
97 StatusCode JobOptionsSvc::setMyProperties( const std::string& client,
98  IProperty* myInt )
99 {
100  const auto* props = m_svc_catalog.getProperties(client);
101  if ( !props ){ return StatusCode::SUCCESS; }
102 
103  bool fail = false;
104  for ( const auto& cur : *props )
105  {
106  StatusCode sc = myInt->setProperty ( *cur ) ;
107  if ( sc.isFailure() )
108  {
109  MsgStream my_log( msgSvc(), name() );
110  my_log
111  << MSG::ERROR
112  << "Unable to set the property '" << cur->name() << "'"
113  << " of '" << client << "'. "
114  << "Check option and algorithm names, type and bounds."
115  << endmsg;
116  fail = true;
117  }
118  }
119  return fail ? StatusCode::FAILURE : StatusCode::SUCCESS ;
120 }
121 
123 std::vector<std::string> JobOptionsSvc::getClients() const {
124  return m_svc_catalog.getClients();
125 }
126 
127 
128 void JobOptionsSvc::dump (const std::string& file,
129  const gp::Catalog& catalog) const {
130  MsgStream log ( msgSvc() , name() ) ;
131  std::ofstream out( file , std::ios_base::out | std::ios_base::trunc );
132  if ( !out ) {
133  log << MSG::ERROR << "Unable to open dump-file \""+file+"\"" << endmsg ;
134  return ; // RETURN
135  }
136  log << MSG::INFO << "Properties are dumped into \""+file+"\"" << endmsg ;
137  // perform the actual dump:
138  out << catalog;
139 }
140 
141 void JobOptionsSvc::fillServiceCatalog(const gp::Catalog& catalog) {
142  for (const auto& client : catalog) {
143  for (const auto& current : client.second ) {
144  addPropertyToCatalogue ( client.first ,
145  StringProperty{ current.NameInClient(),
146  current.ValueAsString() } );
147  }
148  }
149 }
150 
152  const std::string& path) {
153  std::string search_path = path;
154  if ( search_path.empty() && !m_dir_search_path.empty() )
155  { search_path = m_dir_search_path ; }
156  //
157  MsgStream my_log( msgSvc(), name() );
158  if (UNLIKELY(outputLevel() <= MSG::DEBUG))
159  my_log << MSG::DEBUG // debug
160  << "Reading options from the file "
161  << "'" << file << "'" << endmsg;
162  gp::Messages messages(my_log);
163  gp::Catalog catalog;
164  gp::Units units;
165  gp::PragmaOptions pragma;
166  gp::Node ast;
167  StatusCode sc = gp::ReadOptions(file, path, &messages, &catalog, &units,
168  &pragma, &ast);
169 
170  // --------------------------------------------------------------------------
171  if ( sc.isSuccess() )
172  {
173  if (pragma.IsPrintOptions()) {
174  my_log << MSG::INFO << "Print options" << std::endl << catalog
175  << endmsg;
176  }
177  if (pragma.IsPrintTree()) {
178  my_log << MSG::INFO << "Print tree:" << std::endl << ast.ToString()
179  << endmsg;
180  }
181  if (pragma.HasDumpFile()) dump(pragma.dumpFile(), catalog);
182  my_log << MSG::INFO
183  << "Job options successfully read in from " << file << endmsg;
184  fillServiceCatalog(catalog);
185  } else {
186  my_log << MSG::FATAL << "Job options errors."<< endmsg;
187  }
188  // ----------------------------------------------------------------------------
189  return sc;
190 }
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
StatusCode initialize() override
Definition: Service.cpp:63
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
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
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
std::string m_pythonAction
Definition: JobOptionsSvc.h:78
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:76
SvcCatalog m_svc_catalog
Definition: JobOptionsSvc.h:80
list path
Definition: __init__.py:15
STL namespace.
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
StatusCode evaluateConfig(const std::string &filename, const std::string &preAction, const std::string &postAction)
Definition: PythonConfig.cpp:8
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
StatusCode getProperty(Property *p) const override
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
SimpleProperty< std::string > StringProperty
Definition: Property.h:718
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 setProperty(const Property &p) override
IProperty implementation (needed for initialisation)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
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:617
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
#define UNLIKELY(x)
Definition: Kernel.h:126
std::string m_pythonParams
Definition: JobOptionsSvc.h:79
bool load(Property &dest) const override
set value for another property
Definition: Property.h:283
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
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