CASM  1.1.0
A Clusters Approach to Statistical Mechanics
import.cc
Go to the documentation of this file.
1 #include "casm/app/import.hh"
2 
4 #include "casm/casm_io/Log.hh"
6 #include "casm/clex/PrimClex.hh"
9 
10 // need to add specializations here
12 
13 namespace CASM {
14 
15 namespace Completer {
17 
18 const std::vector<fs::path> &ImportOption::pos_vec() const { return m_pos_vec; }
19 
20 const fs::path &ImportOption::batch_path() const { return m_batch_path; }
21 
24 
25  m_desc.add_options()
26 
27  ("pos,p",
28  po::value<std::vector<fs::path> >(&m_pos_vec)
29  ->multitoken()
30  ->value_name(ArgHandler::path()),
31  "Path(s) to structure(s) being imported (multiple allowed, but no "
32  "wild-card matching)")
33 
34  ("batch,b",
35  po::value<fs::path>(&m_batch_path)->value_name(ArgHandler::path()),
36  "Path to batch file, which should list one structure file path per "
37  "line "
38  "(can be used in combination with --pos)")
39 
40  ("data,d",
41  "Attempt to extract calculation data from the enclosing "
42  "directory of the structure files, if it is available")
43 
44  ("copy-additional-files",
45  "Recursively copy other files from the same directory as "
46  "the properties.calc.json file.");
47 
50  bool required = false;
51  add_settings_suboption(required);
52  add_input_suboption(required);
53 
54  return;
55 }
56 } // namespace Completer
57 
58 // -- class ImportCommandImplBase --------------------------------------------
59 
62  public:
64 
66 
67  virtual int help() const;
68 
69  virtual int desc() const;
70 
71  virtual int run() const;
72 
73  protected:
75 };
76 
78  : m_cmd(cmd) {}
79 
81  log() << std::endl << m_cmd.opt().desc() << std::endl;
83  return 0;
84 }
85 
86 int ImportCommandImplBase::desc() const { // -- custom --
87  help();
88 
89  log() << "Import structures or calculation data specified by --pos or "
90  "--batch. \n\n"
91 
92  "Structures are mapped to the closest matching configuration "
93  "consistent \n"
94  "with the primitive crystal structure. If a JSON file is specified, "
95  "it \n"
96  "will be interpreted as a 'properties.calc.json' file.\n\n"
97 
98  "For complete import options description for a particular config "
99  "type, use\n"
100  "--desc along with '--type <typename>'.\n\n";
101 
102  return 0;
103 }
104 
106  err_log() << "ERROR: No --type\n";
108  return ERR_INVALID_ARG;
109 }
110 
111 // -- template<typename DataObject> class ImportCommandImpl -----------------
112 
119 template <typename DataObject>
121  public:
123 
124  int help() const override;
125 
126  int desc() const override;
127 
128  int run() const override;
129 };
130 
131 template <typename DataObject>
133  log()
134  << std::endl
135  << m_cmd.opt().desc() << "\n\n"
136 
137  << "For complete options description for a particular config type, use:\n"
138  " casm "
139  << ImportCommand::name << " --desc --type "
140  << traits<DataObject>::short_name << "\n\n";
141  return 0;
142 }
143 
144 template <typename DataObject>
146  log() << DB::Import<DataObject>::desc << std::endl;
147  return 0;
148 }
149 
150 template <typename DataObject>
152  return DB::Import<DataObject>::run(m_cmd.primclex(), m_cmd.input(),
153  m_cmd.opt());
154 }
155 
156 // -- class ImportCommand ----------------------------------------------------
157 
158 const std::string ImportCommand::name = "import";
159 
162  : APICommand<Completer::ImportOption>(_args, _opt) {}
163 
165 
167  if (!count("pos") && !count("batch")) {
168  err_log() << "Error in 'casm import'. "
169  "Use --pos or --batch to specify structures to be imported"
170  << std::endl;
171  return ERR_INVALID_ARG;
172  }
173 
174  return 0;
175 }
176 
177 int ImportCommand::help() const { return impl().help(); }
178 
179 int ImportCommand::desc() const { return impl().desc(); }
180 
181 int ImportCommand::run() const { return impl().run(); }
182 
184  if (!m_impl) {
185  if (vm().count("type")) {
186  if (!opt().configtype_opts().count(opt().configtype())) {
187  std::stringstream msg;
188  msg << "--type " << opt().configtype() << " is not allowed for 'casm "
189  << name << "'.";
190  print_names(err_log());
191  throw CASM::runtime_error(msg.str(), ERR_INVALID_ARG);
192  }
193 
195  opt().configtype(), DB::ConstructImpl<ImportCommand>(m_impl, *this));
196  } else {
197  m_impl = notstd::make_unique<ImportCommandImplBase>(*this);
198  }
199  }
200  return *m_impl;
201 }
202 
203 void ImportCommand::print_names(std::ostream &sout) const {
204  sout << "The allowed types are:\n";
205 
206  for (const auto &configtype : opt().configtype_opts()) {
207  sout << " " << configtype << std::endl;
208  }
209 }
210 
212  if (count("settings")) {
213  return jsonParser{opt().settings_path()};
214  } else if (count("input")) {
215  return jsonParser::parse(opt().input_str());
216  }
217  // use defaults
218  return jsonParser();
219 }
220 
221 } // namespace CASM
#define ERR_INVALID_ARG
Definition: errors.hh:7
const OptionType & opt() const
Definition: APICommand.hh:61
const po::variables_map & vm() const
Definition: APICommand.hh:59
static std::string path()
Get value_type string for path completion.
Definition: Handlers.cc:56
std::string configtype() const
Definition: Handlers.cc:530
const std::vector< fs::path > & pos_vec() const
Definition: import.cc:18
std::vector< fs::path > m_pos_vec
Definition: Handlers.hh:977
const fs::path & batch_path() const
Definition: import.cc:20
void initialize() override
Fill in the options descriptions accordingly.
Definition: import.cc:22
const fs::path settings_path() const
Returns the path corresponding to add_settings_suboption.
Definition: Handlers.cc:364
void add_input_suboption(bool required=true)
Definition: Handlers.cc:610
const po::options_description & desc()
Get the program options, filled with the initialized values.
Definition: Handlers.cc:321
void add_settings_suboption(bool required=true)
Definition: Handlers.cc:588
void add_help_suboption()
Add a plain –help and –desc suboptions.
Definition: Handlers.cc:561
po::options_description m_desc
Definition: Handlers.hh:260
void add_configtype_suboption(std::string _default, std::set< std::string > _configtype_opts)
Add –type suboption (default, set of short_name of allowed ConfigTypes)
Definition: Handlers.cc:511
int run() const override
Definition: import.cc:181
std::unique_ptr< ImportCommandImplBase > m_impl
Definition: import.hh:73
void print_names(std::ostream &sout) const
Definition: import.cc:203
static const std::string name
Definition: import.hh:50
ImportCommandImplBase & impl() const
Definition: import.cc:183
int desc() const override
Definition: import.cc:179
jsonParser input() const
Definition: import.cc:211
ImportCommand(const CommandArgs &_args, Completer::ImportOption &_opt)
Definition: import.cc:160
int vm_count_check() const override
Definition: import.cc:166
int help() const override
Definition: import.cc:177
Defaults used if DataObject type doesn't matter or not given.
Definition: import.cc:61
virtual int desc() const
Definition: import.cc:86
virtual int run() const
Definition: import.cc:105
virtual int help() const
Definition: import.cc:80
const ImportCommand & m_cmd
Definition: import.cc:74
ImportCommandImplBase(const ImportCommand &cmd)
Definition: import.cc:77
virtual ~ImportCommandImplBase()
Definition: import.cc:65
ImportCommandImpl(const ImportCommand &cmd)
Definition: import.cc:122
int run() const override
Definition: import.cc:151
int desc() const override
Definition: import.cc:145
int help() const override
Definition: import.cc:132
static jsonParser parse(const std::string &str)
Construct a jsonParser from a string containing JSON data.
Definition: jsonParser.hh:382
void for_config_type_short(std::string short_name, F f)
const std::set< std::string > & config_types_short()
std::set of all QueryTraits<ConfigType>::short_name
Main CASM namespace.
Definition: APICommand.hh:8
Log & log()
Definition: Log.hh:424
Log & err_log()
Definition: Log.hh:426
Data structure holding basic CASM command info.