def update_serializer(self, *args, **kwargs): if 'delimiter' in kwargs: self._explicit_delimiter = True if self._explicit_delimiter and ('format_str' in kwargs): info = serialize.format2table(kwargs['format_str']) info['delimiter'] = self.delimiter info.setdefault('comment', '') info.setdefault('newline', '') kwargs['format_str'] = serialize.table2format(**info) # Transform scalar into array for table old_typedef = kwargs.get('datatype', {}) if old_typedef.get('type', 'array') != 'array': old_typedef = kwargs.pop('datatype') if old_typedef['type'] == 'object': names = self.get_field_names() if not names: names = list(old_typedef['properties'].keys()) assert (len(old_typedef['properties']) == len(names)) new_typedef = {'type': 'array', 'items': []} for n in names: new_typedef['items'].append( dict(old_typedef['properties'][n], title=n)) else: new_typedef = {'type': 'array', 'items': [old_typedef]} kwargs['datatype'] = new_typedef out = super(AsciiTableSerialize, self).update_serializer(*args, **kwargs) self.initialized = (self.typedef != self.default_datatype) self.update_format_str() self.update_field_names() self.update_field_units() return out
def test_format2table(): r"""Test getting table information from a format string.""" out = { 'delimiter': b'\t', 'newline': b'\n', 'comment': b'# ', 'fmts': ["%5s", "%ld", "%lf", "%g%+gj"] } out['fmts'] = [backwards.as_bytes(f) for f in out['fmts']] sfmt = out['fmts'][0] sout = dict(**out) sout['fmts'] = [sfmt] del sout['newline'], sout['comment'] fmt = b'# %5s\t%ld\t%lf\t%g%+gj\n' fmt2 = b'# %5s\t\t%ld\t%lf\t%g%+gj\n' assert_equal(dict(fmts=[]), serialize.format2table('hello')) assert_equal(sout, serialize.format2table(sfmt)) assert_equal(fmt, serialize.table2format(**out)) assert_equal(out, serialize.format2table(fmt)) assert_equal(fmt, serialize.table2format(fmts=out['fmts'])) assert_equal(out, serialize.format2table(fmt2)) assert_raises(RuntimeError, serialize.format2table, "%5s,%ld\t%g\n")
def test_format2table(): r"""Test getting table information from a format string.""" out = { 'delimiter': b'\t', 'newline': b'\n', 'comment': b'# ', 'fmts': ["%5s", "%ld", "%lf", "%g%+gj"] } out['fmts'] = [f.encode("utf-8") for f in out['fmts']] sfmt = out['fmts'][0] sout = dict(**out) sout['fmts'] = [sfmt] del sout['newline'], sout['comment'] fmt = b'# %5s\t%ld\t%lf\t%g%+gj\n' fmt2 = b'# %5s\t\t%ld\t%lf\t%g%+gj\n' assert (dict(fmts=[]) == serialize.format2table('hello')) assert (sout == serialize.format2table(sfmt)) assert (fmt == serialize.table2format(**out)) assert (out == serialize.format2table(fmt)) assert (fmt == serialize.table2format(fmts=out['fmts'])) assert (out == serialize.format2table(fmt2)) with pytest.raises(RuntimeError): serialize.format2table("%5s,%ld\t%g\n")
def transform_datatype(self, datatype): r"""Determine the datatype that will result from applying the transform to the supplied datatype. Args: datatype (dict): Datatype to transform. Returns: dict: Transformed datatype. """ if (((datatype.get('type', None) == 'array') and isinstance(datatype.get('items', None), list))): order = datatype.get('field_names', [x.get('title', 'f%d' % i) for i, x in enumerate(datatype['items'])]) if self.as_single: datatype = copy.deepcopy(datatype['items'][ order.index(self.selected[0])]) datatype['title'] = self.selected[0] else: datatype = copy.deepcopy(datatype) datatype['items'] = [datatype['items'][order.index(k)] for k in self.selected] for i, k in enumerate(self.selected): datatype['items'][i]['title'] = k if 'field_names' in datatype: datatype['field_names'] = copy.deepcopy(self.selected) if 'format_str' in datatype: info = serialize.format2table(datatype['format_str']) info['fmts'] = [info['fmts'][order.index(k)] for k in self.selected] datatype['format_str'] = serialize.table2format(**info) elif (((datatype.get('type', None) == 'array') and isinstance(datatype.get('items', None), dict) and self.as_single)): datatype = copy.deepcopy(datatype['items']) elif datatype.get('type', None) == 'object': if self.as_single: datatype = copy.deepcopy(datatype['properties'][self.selected[0]]) else: datatype = copy.deepcopy(datatype) datatype['properties'] = {k: datatype['properties'][k] for k in self.selected} return datatype
def update_format_str(self): r"""Update the format string based on the type definition.""" # Get format information from precision etc. if (self.format_str is None) and self.initialized: assert(self.typedef['type'] == 'array') fmts = [] if isinstance(self.typedef['items'], dict): # pragma: debug idtype = definition2dtype(self.typedef['items']) ifmt = serialize.nptype2cformat(idtype, asbytes=True) # fmts = [ifmt for x in msg] raise Exception("Variable number of items not yet supported.") elif isinstance(self.typedef['items'], list): for x in self.typedef['items']: idtype = definition2dtype(x) ifmt = serialize.nptype2cformat(idtype, asbytes=True) fmts.append(ifmt) if fmts: self.format_str = serialize.table2format( fmts=fmts, delimiter=self.delimiter, newline=self.newline, comment=b'')