Skip to content

Commit f72e1e0

Browse files
committed
Add getDefault methods
Add readDefaults methods for setting defaults from generic file Make readParam more generic Add tests for readDefaults and readParam Fix bugs in parseVar & add test tests for parseVar
1 parent 55178de commit f72e1e0

File tree

6 files changed

+458
-117
lines changed

6 files changed

+458
-117
lines changed

include/ValueSet.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ namespace ePhotosynthesis {
9898
}
9999
return (it != v.end());
100100
}
101+
inline bool __startswith(std::string const& value,
102+
std::string const& prefix) {
103+
if (prefix.size() > value.size()) return false;
104+
return std::equal(prefix.begin(), prefix.end(), value.begin());
105+
}
106+
inline bool __endswith(std::string const& value,
107+
std::string const& suffix) {
108+
if (suffix.size() > value.size()) return false;
109+
return std::equal(suffix.rbegin(), suffix.rend(), value.rbegin());
110+
}
101111
template<typename T>
102112
std::string __make_string(const T& x,
103113
typename std::enable_if<!std::is_same<T, const char[]>::value >::type* = 0)
@@ -600,6 +610,9 @@ namespace ePhotosynthesis {
600610
method(has, bool, \
601611
((const std::string&, name, ), \
602612
(const bool&, isGlymaID, false)), false, const) \
613+
/** \copydoc ValueSetBase::getAliasedName */ \
614+
method(getAliasedName, std::string, \
615+
((const std::string&, name, )), "", const) \
603616
/** \copydoc ValueSetBase::fromNameWithAliases */ \
604617
method(fromNameWithAliases, int, \
605618
((const std::string&, name, ), \
@@ -1921,6 +1934,15 @@ namespace ePhotosynthesis {
19211934
utils::enum_key2string(param_type) << "::" << getName(k);
19221935
return std::move(out).str();
19231936
}
1937+
/**
1938+
Get the name that an alias points to.
1939+
\param[in] name Alias to get name for.
1940+
\return Aliased key name.
1941+
*/
1942+
static std::string getAliasedName(const std::string& name) {
1943+
EnumType k = fromNameWithAliases(name);
1944+
return getName(k);
1945+
}
19241946
/**
19251947
Get the enum key corresponding to a name value also checking for
19261948
aliases.

include/Variables.hpp

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,15 @@ class Variables : public VALUE_SET_PARENT(Variables, Variables, MODULE_ALL, PARA
328328
\param[in] use_1st_match If true, the first variable matching the
329329
provided string will be returned. If false and there is more
330330
than one match, an error will be thrown.
331+
\param[in] allow_no_match If true, an empty string will be
332+
returned if there is not match.
331333
\returns Variable name extracted from k.
332334
*/
333335
static std::string parseVar(const std::string& k,
334336
MODULE& mod, PARAM_TYPE& pt,
335337
const bool& isGlymaID = false,
336-
const bool& use_1st_match=false);
338+
const bool& use_1st_match=false,
339+
const bool& allow_no_match=false);
337340
/**
338341
Check if a value set has a variable that matches the provided
339342
string.
@@ -375,6 +378,38 @@ class Variables : public VALUE_SET_PARENT(Variables, Variables, MODULE_ALL, PARA
375378
static int getKey(const MODULE& mod, const PARAM_TYPE& pt,
376379
const std::string& name,
377380
const bool& isGlymaID = false);
381+
382+
/**
383+
Get the default value for a variable in a value set.
384+
\param[in] mod Module associated with the value set that the
385+
variable is part of.
386+
\param[in] pt Parameter type associated with the value set that the
387+
variable is part of.
388+
\param[in] name String identifying the variable.
389+
\param[in] isGlymaID If true, name will be treated as a GlymaID.
390+
If false, name will be treated as the variable name.
391+
*/
392+
static double getDefault(const MODULE& mod, const PARAM_TYPE& pt,
393+
const std::string& name,
394+
const bool& isGlymaID = false);
395+
/**
396+
Get the default value for a variable in a value set.
397+
\param[in] mod Module associated with the value set that the
398+
variable is part of.
399+
\param[in] pt Parameter type associated with the value set that the
400+
variable is part of.
401+
\param[in] key Key identifying the variable.
402+
*/
403+
static double getDefault(const MODULE& mod, const PARAM_TYPE& pt,
404+
const int& key);
405+
/**
406+
Set the default value for a variable in a value set.
407+
\param[in] k String identifying the variable.
408+
\param[in] isGlymaID If true, name will be treated as a GlymaID.
409+
If false, name will be treated as the variable name.
410+
*/
411+
static double getDefault(const std::string& k,
412+
const bool& isGlymaID = false);
378413

379414
/**
380415
Set the default value for a variable in a value set.
@@ -570,16 +605,35 @@ class Variables : public VALUE_SET_PARENT(Variables, Variables, MODULE_ALL, PARA
570605
*/
571606
static void cleanupValueSet(const MODULE& mod, const PARAM_TYPE& pt,
572607
const bool noChildren = false);
573-
608+
private:
609+
static void _readParam(const std::string& fname,
610+
std::map<std::string, std::string>& inputs,
611+
Variables* theVars = nullptr,
612+
const std::string& context="_readParam");
613+
public:
614+
615+
/**
616+
Read default parameters from a file.
617+
\param[in] fname File to read.
618+
*/
619+
static void readDefaults(const std::string& fname);
620+
/**
621+
Read default parameters from a file, checking for duplicates
622+
\param[in] fname File to read.
623+
\param[in, out] inputs Map that read variables should be checked
624+
against for duplicates and copied into.
625+
*/
626+
static void readDefaults(const std::string& fname,
627+
std::map<std::string, std::string>& inputs);
574628
/**
575629
Read parameters from a file.
576-
\param[in] File to read.
630+
\param[in] fname File to read.
577631
*/
578632
void readParam(const std::string& fname);
579633
/**
580634
Read parameters from a file, checking for duplicates
581-
\param[in] File to read.
582-
\param[in, out] Map that read variables should be checked
635+
\param[in] fname File to read.
636+
\param[in, out] inputs Map that read variables should be checked
583637
against for duplicates and copied into.
584638
*/
585639
void readParam(const std::string& fname,

scripts/devtasks.py

Lines changed: 52 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ class SubTask:
9494
_drivers = ['trDynaPS', 'DynaPS', 'CM', 'EPS']
9595
_driver_map = {(i + 1): x for i, x in enumerate(_drivers)}
9696

97+
@classmethod
98+
def adjust_args(cls, args):
99+
pass
100+
97101
def __init__(self, args, dont_cleanup=False, **kwargs):
98102
self.current_args = args
99103
self.dont_cleanup = dont_cleanup
@@ -104,10 +108,6 @@ def __init__(self, args, dont_cleanup=False, **kwargs):
104108
def __del__(self):
105109
self.cleanup_files()
106110

107-
@classmethod
108-
def adjust_args(cls, args):
109-
pass
110-
111111
def cleanup_files(self):
112112
if self.dont_cleanup or self.current_args.dont_cleanup:
113113
return
@@ -170,29 +170,40 @@ def run_commands(self, args, cmds=None, output_file=None, **kwargs):
170170

171171
class BuildSubTask(SubTask):
172172

173+
@classmethod
174+
def adjust_args(cls, args):
175+
cls.prefix_path_args(args, ['build_dir', 'install_dir'])
176+
super(BuildSubTask, cls).adjust_args(args)
177+
173178
def __init__(self, args, config_args=None, build_args=None,
174179
install_args=None, build_kwargs=None, build_env=None,
175180
**kwargs):
176181
self.adjust_args(args)
182+
kwargs.setdefault('cwd', args.build_dir)
183+
super(BuildSubTask, self).__init__(args, **kwargs)
184+
185+
def run_commands(self, args, config_args=None, build_args=None,
186+
install_args=None, build_kwargs=None, build_env=None,
187+
**kwargs):
177188
if build_kwargs is None:
178189
build_kwargs = {}
179190
build_kwargs.setdefault('env', build_env)
180191
if not args.dont_build:
181192
build(args, config_args=config_args, build_args=build_args,
182193
install_args=install_args, **build_kwargs)
183-
kwargs.setdefault('cwd', args.build_dir)
184-
super(BuildSubTask, self).__init__(args, **kwargs)
185-
186-
@classmethod
187-
def adjust_args(cls, args):
188-
cls.prefix_path_args(args, ['build_dir', 'install_dir'])
194+
super(BuildSubTask, self).run_commands(args, **kwargs)
189195

190196

191197
class build(SubTask):
192198

199+
@classmethod
200+
def adjust_args(cls, args):
201+
BuildSubTask.adjust_args(args)
202+
super(build, cls).adjust_args(args)
203+
193204
def __init__(self, args, config_args=None, build_args=None,
194205
install_args=None, **kwargs):
195-
BuildSubTask.adjust_args(args)
206+
self.adjust_args(args)
196207
if config_args is None:
197208
config_args = []
198209
if build_args is None:
@@ -287,6 +298,12 @@ def __init__(self, args):
287298

288299
class test(BuildSubTask):
289300

301+
@classmethod
302+
def adjust_args(cls, args):
303+
if args.refresh_output:
304+
args.preserve_output = True
305+
super(test, cls).adjust_args(args)
306+
290307
def __init__(self, args, test_flags=None,
291308
config_args=None, build_args=None, **kwargs):
292309
self.adjust_args(args)
@@ -348,12 +365,6 @@ def __init__(self, args, test_flags=None,
348365
else:
349366
print(f"MISSING OUTPUT {fsrc}")
350367

351-
@classmethod
352-
def adjust_args(cls, args):
353-
if args.refresh_output:
354-
args.preserve_output = True
355-
super(test, cls).adjust_args(args)
356-
357368

358369
class ephoto(BuildSubTask):
359370

@@ -408,21 +419,6 @@ def iter_drivers(cls, func, args, suffix_paths=None, **kwargs):
408419

409420
class compare_matlab(BuildSubTask):
410421

411-
def __init__(self, args, config_args=None, build_args=None):
412-
self.adjust_args(args)
413-
if config_args is None:
414-
config_args = []
415-
if build_args is None:
416-
build_args = []
417-
cmds = []
418-
super(compare_matlab, self).__init__(
419-
args, cmds=cmds, config_args=config_args,
420-
build_args=build_args, cwd=_source_dir,
421-
)
422-
args.rebuild = False
423-
args.dont_build = True
424-
self.compare(args)
425-
426422
@classmethod
427423
def adjust_args(cls, args):
428424
args.make_equivalent_to_matlab = True
@@ -450,6 +446,21 @@ def adjust_args(cls, args):
450446
if not os.path.isdir(args.matlab_output_dir):
451447
os.mkdir(args.matlab_output_dir)
452448

449+
def __init__(self, args, config_args=None, build_args=None):
450+
self.adjust_args(args)
451+
if config_args is None:
452+
config_args = []
453+
if build_args is None:
454+
build_args = []
455+
cmds = []
456+
super(compare_matlab, self).__init__(
457+
args, cmds=cmds, config_args=config_args,
458+
build_args=build_args, cwd=_source_dir,
459+
)
460+
args.rebuild = False
461+
args.dont_build = True
462+
self.compare(args)
463+
453464
def compare(self, args):
454465
if args.driver == 0:
455466
ephoto.iter_drivers(
@@ -612,6 +623,15 @@ def check_output(cls, f1, f2, reltol=1.0e-05, abstol=1.0e-08):
612623

613624
class docs(BuildSubTask):
614625

626+
@classmethod
627+
def adjust_args(cls, args):
628+
args.with_asan = False
629+
args.build_type = 'Debug'
630+
args.dont_build = False
631+
args.dont_install = True
632+
args.only_python = False
633+
super(docs, cls).adjust_args(args)
634+
615635
def __init__(self, args, config_args=None, build_args=None,
616636
install_args=None):
617637
self.adjust_args(args)
@@ -630,15 +650,6 @@ def __init__(self, args, config_args=None, build_args=None,
630650
install_args=install_args,
631651
)
632652

633-
@classmethod
634-
def adjust_args(cls, args):
635-
args.with_asan = False
636-
args.build_type = 'Debug'
637-
args.dont_build = False
638-
args.dont_install = True
639-
args.only_python = False
640-
super(docs, cls).adjust_args(args)
641-
642653

643654
class coverage(BuildSubTask):
644655

0 commit comments

Comments
 (0)