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 // ============================================================================
93 {
94  return m_svc_catalog.getProperties(client);
95 }
96 // ============================================================================
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  error()
110  << "Unable to set the property '" << cur->name() << "'"
111  << " of '" << client << "'. "
112  << "Check option and algorithm names, type and bounds."
113  << endmsg;
114  fail = true;
115  }
116  }
117  return fail ? StatusCode::FAILURE : StatusCode::SUCCESS ;
118 }
119 
122  return m_svc_catalog.getClients();
123 }
124 
125 
127  const gp::Catalog& catalog) const {
128  std::ofstream out( file , std::ios_base::out | std::ios_base::trunc );
129  if ( !out ) {
130  error() << "Unable to open dump-file \""+file+"\"" << endmsg ;
131  return ; // RETURN
132  }
133  info() << "Properties are dumped into \""+file+"\"" << endmsg ;
134  // perform the actual dump:
135  out << catalog;
136 }
137 
138 void JobOptionsSvc::fillServiceCatalog(const gp::Catalog& catalog) {
139  for (const auto& client : catalog) {
140  for (const auto& current : client.second ) {
141  addPropertyToCatalogue ( client.first ,
142  StringProperty{ current.NameInClient(),
143  current.ValueAsString() } );
144  }
145  }
146 }
147 
149  const std::string& path) {
150  std::string search_path = path;
151  if ( search_path.empty() && !m_dir_search_path.empty() )
152  { search_path = m_dir_search_path ; }
153  //
154  if (msgLevel(MSG::DEBUG))
155  debug() << "Reading options from the file "
156  << "'" << file << "'" << endmsg;
157  gp::Messages messages(msgStream());
158  gp::Catalog catalog;
159  gp::Units units;
160  gp::PragmaOptions pragma;
161  gp::Node ast;
162  StatusCode sc = gp::ReadOptions(file, path, &messages, &catalog, &units,
163  &pragma, &ast);
164 
165  // --------------------------------------------------------------------------
166  if ( sc.isSuccess() )
167  {
168  if (pragma.IsPrintOptions()) {
169  info() << "Print options" << std::endl << catalog
170  << endmsg;
171  }
172  if (pragma.IsPrintTree()) {
173  info() << "Print tree:" << std::endl << ast.ToString()
174  << endmsg;
175  }
176  if (pragma.HasDumpFile()) dump(pragma.dumpFile(), catalog);
177  info() << "Job options successfully read in from " << file << endmsg;
178  fillServiceCatalog(catalog);
179  } else {
180  fatal() << "Job options errors."<< endmsg;
181  }
182  // ----------------------------------------------------------------------------
183  return sc;
184 }
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
Definition: System.cpp:617
StatusCode initialize() override
Definition: Service.cpp:68
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
std::vector< std::string > getClients() const override
Get the list of clients.
std::string m_source_type
Definition: JobOptionsSvc.h:77
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
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:80
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:76
SvcCatalog m_svc_catalog
Definition: JobOptionsSvc.h:82
T endl(T...args)
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
SimpleProperty< std::string > StringProperty
Definition: Property.h:718
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
std::vector< std::string > getClients() const
Definition: SvcCatalog.cpp:67
std::string m_dir_search_path
Definition: JobOptionsSvc.h:78
STL class.
StatusCode getProperty(Property *p) const override
get the property
const std::vector< Property * > & getProperties() const override
Definition: Service.cpp:354
void dump(const std::string &file, const Gaudi::Parsers::Catalog &catalog) const
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
STL class.
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
virtual StatusCode setProperty(const Property &p)=0
Set the property by property.
list file
Definition: ana.py:160
GAUDI_API std::string path(const AIDA::IBaseHistogram *aida)
get the path in THS for AIDA histogram
Property base class allowing Property* collections to be "homogeneous".
Definition: Property.h:38
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 '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.
MsgStream & msgStream() const
Return an uninitialized MsgStream.
StatusCode addPropertyToCatalogue(const std::string &client, const Property &property) override
Add a property into the JobOptions catalog.
StatusCode initialize() override
std::string m_pythonParams
Definition: JobOptionsSvc.h:81
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:75
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:21
MSG::Level msgLevel() const
get the output level from the embedded MsgStream
std::string m_source_path
Definition: JobOptionsSvc.h:76
Helper functions to set/get the application return code.
Definition: __init__.py:1
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244