def test_format_message(): r"""Test formatting message from a list or arguments and back.""" fmt = b'%5s\t%ld\t%lf\t%g%+gj\n' dtype = serialize.cformat2nptype(fmt) x_arr = np.ones(1, dtype) x_tup = [x_arr[n][0] for n in x_arr.dtype.names] flist = [fmt, "%ld"] alist = [tuple(x_tup), 0] for a, f in zip(alist, flist): msg = serialize.format_message(a, f) b = serialize.process_message(msg, f) if not isinstance(a, tuple): assert (b == (a, )) else: assert (b == a) # Formats with mixed types assert (serialize.format_message(b'hello', '%s') == 'hello') assert (serialize.format_message('hello', b'%s') == b'hello') # Errors with pytest.raises(RuntimeError): serialize.format_message((0, ), "%d %d") with pytest.raises(TypeError): serialize.process_message(0, "%d") with pytest.raises(ValueError): serialize.process_message(b'hello', "%d")
def func_deserialize(self, msg): r"""Deserialize a message. Args: msg: Message to be deserialized. Returns: obj: Deserialized message. """ if self.format_str is None: raise RuntimeError("Format string is not defined.") if self.as_array: out = serialize.table_to_array( msg, self.format_str, use_astropy=self.use_astropy, names=self.get_field_names(as_bytes=True)) out = self.datatype.coerce_type(out) else: out = list(serialize.process_message(msg, self.format_str)) field_units = self.get_field_units() if field_units is not None: out = [units.add_units(x, u) for x, u in zip(out, field_units)] return out
def test_format_message(): r"""Test formatting message from a list or arguments and back.""" fmt = b'%5s\t%ld\t%lf\t%g%+gj\n' dtype = serialize.cformat2nptype(fmt) x_arr = np.ones(1, dtype) x_tup = [x_arr[n][0] for n in x_arr.dtype.names] # x_tup[0] = backwards.as_str(x_tup[0]) flist = [fmt, "%ld"] alist = [tuple(x_tup), 0] for a, f in zip(alist, flist): msg = serialize.format_message(a, f) b = serialize.process_message(msg, f) if not isinstance(a, tuple): assert_equal(b, (a, )) else: assert_equal(b, a) # Errors assert_raises(RuntimeError, serialize.format_message, (0, ), "%d %d") assert_raises(TypeError, serialize.process_message, 0, "%d") assert_raises(ValueError, serialize.process_message, b'hello', "%d")
def register_example(example_dir): r"""Register an example based on the contents of the director. Args: example_dir (str): Full path to a directory potentially containing an example. Returns: tuple (list, dict, dict): A list of available languages, a dictionary mapping from language to YAML specification files for the example, and a dictionary mapping from language to source files for the example. """ # Check that the source directory and test exist example_base = os.path.basename(example_dir) srcdir = os.path.join(example_dir, 'src') testfile = os.path.join(_example_dir, 'tests', 'test_%s.py' % example_base) if not os.path.isfile(testfile): # pragma: no cover # TODO: Automate test creation if (((not tools.is_subprocess()) and (example_base not in ['sbml1', 'sbml2', 'sbml3']))): logging.error("Missing test file: %s" % testfile) if not os.path.isdir(srcdir): # pragma: no cover if not tools.is_subprocess(): logging.error("Missing source directory: %s" % srcdir) return {} # Determine which languages are present in the example lang_base = [] lang_avail = [] lang_search = None if example_base in ['rpcFib', 'maxMsg']: lang_search = example_base + 'Cli_%s.yml' lang_avail += ['all', 'all_nomatlab'] elif example_base.startswith('rpc_'): lang_search = 'client_%s.yml' elif example_base == 'root_to_shoot': lang_avail += ['all', 'all_nomatlab', 'c', 'python'] elif example_base == 'fakeplant': lang_avail += ['all', 'all_nomatlab', 'c', 'cpp', 'matlab', 'python'] elif example_base in ['types', 'transforms']: lang_avail += tools.get_supported_lang() for k in ['cmake', 'make', 'lpy', 'executable']: lang_avail.remove(k) elif example_base.startswith('sbml'): lang_avail = ['sbml'] elif example_base.startswith('osr'): lang_search = example_base + '_%s.yml' lang_base += ['osr'] else: lang_search = example_base + '_%s.yml' if lang_search is not None: for match in glob.glob(os.path.join(example_dir, lang_search % '*')): lang = serialize.process_message(os.path.basename(match), lang_search)[0] if lang == 'valgrind': continue lang_avail.append(lang.lower()) lang_avail = tuple(sorted(lang_avail)) lang_base = tuple(sorted(lang_base)) # Get YAML and source files for each language out_yml = {} out_src = {} src_is_abs = False for lang in lang_avail: yml_names = [] src_names = [] if example_base == 'rpcFib': if lang == 'all': lang_set = ('python', 'matlab', 'c') elif lang == 'all_nomatlab': lang_set = ('python', 'cpp', 'c') else: lang_set = (lang, lang, lang) yml_names = [ '%sCli_%s.yml' % (example_base, lang_set[0]), '%sCliPar_%s.yml' % (example_base, lang_set[1]), '%sSrv_%s.yml' % (example_base, lang_set[2]) ] src_names = [ '%sCli%s' % (example_base, ext_map[lang_set[0]]), '%sCliPar%s' % (example_base, ext_map[lang_set[1]]), '%sSrv%s' % (example_base, ext_map[lang_set[2]]) ] elif example_base == 'maxMsg': if lang == 'all': lang_set = ('python', 'matlab') elif lang == 'all_nomatlab': lang_set = ('python', 'c') else: lang_set = (lang, lang) yml_names = [ '%sCli_%s.yml' % (example_base, lang_set[0]), '%sSrv_%s.yml' % (example_base, lang_set[1]) ] src_names = [ '%sCli%s' % (example_base, ext_map[lang_set[0]]), '%sSrv%s' % (example_base, ext_map[lang_set[1]]) ] elif example_base.startswith('rpc_'): # TODO: Create server examples in other languages yml_names = ['server_python.yml', 'client_%s.yml' % lang] src_names = ['server.py', 'client%s' % ext_map[lang]] elif example_base == 'root_to_shoot': if lang.startswith('all'): yml_names = ['root.yml', 'shoot.yml', 'root_to_shoot.yml'] src_names = ['root.c', 'shoot.py'] elif lang == 'python': yml_names = ['shoot.yml', 'shoot_files.yml'] src_names = ['shoot.py'] elif lang == 'c': yml_names = ['root.yml', 'root_files.yml'] src_names = ['root.c'] elif example_base == 'fakeplant': if lang.startswith('all'): yml_names = [ 'canopy.yml', 'light.yml', 'photosynthesis.yml', 'fakeplant.yml' ] src_names = ['canopy.cpp', 'light.c', 'photosynthesis.py'] if lang == 'all_nomatlab': yml_names.append('growth_python.yml') src_names.append('growth.py') else: yml_names.append('growth.yml') src_names.append('growth.m') elif lang == 'python': yml_names = ['photosynthesis.yml'] src_names = ['photosynthesis.py'] elif lang == 'c': yml_names = ['light.yml', 'light_files.yml'] src_names = ['light.c'] elif lang == 'cpp': yml_names = ['canopy.yml', 'canopy_files.yml'] src_names = ['canopy.cpp'] elif lang == 'matlab': yml_names = ['growth.yml', 'growth_files.yml'] src_names = ['growth.m'] elif example_base in ['types', 'transforms']: yml_names = ['%s.yml' % example_base] src_names = ['src.py', 'dst.py'] elif example_base.startswith('sbml'): yml_names = ['%s.yml' % example_base] src_names = ['%s.xml' % example_base] else: src_is_abs = True yml_names = ['%s_%s.yml' % (example_base, lang)] if lang.startswith('all'): src_names = [] for lsrc in lang_avail: if lsrc.startswith('all'): continue elif (lang == 'all_nomatlab') and (lsrc == 'matlab'): continue src_names += sorted( glob.glob(os.path.join(srcdir, '*' + ext_map[lsrc]))) else: src_names = sorted( glob.glob(os.path.join(srcdir, '*' + ext_map.get(lang, '')))) for ilang_base in lang_base: src_names += sorted( glob.glob( os.path.join(srcdir, '*' + ext_map.get(ilang_base, '')))) out_yml[lang] = [os.path.join(example_dir, y) for y in yml_names] if src_is_abs: out_src[lang] = src_names else: out_src[lang] = [os.path.join(srcdir, s) for s in src_names] if len(out_yml[lang]) == 1: out_yml[lang] = out_yml[lang][0] if len(out_src[lang]) == 1: out_src[lang] = out_src[lang][0] return lang_base, lang_avail, out_yml, out_src