def test_get_formats_when_stream_changes(self): with open(os.path.join(test_resources, 'good.xml'), 'r') as f: _stream = seria.load(f) assert _stream.formats == (False, True, False) with open(os.path.join(test_resources, 'good.yaml'), 'r') as f: _stream = seria.load(f) assert _stream.formats == (False, False, True)
def test_yaml_to_xml_to_yaml(self): _source_fmt = 'yaml' _target_fmt = 'xml' with open(os.path.join(test_resources, 'good.%s' % _source_fmt), 'r') as _a: _a_seria = seria.load(_a) _b = StringIO() _b.write(_a_seria.dump(fmt=_target_fmt)) _b_seria = seria.load(_b) _c = StringIO() _c.write(_b_seria.dump(fmt=_source_fmt)) _a.seek(0) _c.seek(0) assert _a.read() == _c.read()
def test_xml_to_json_to_xml(self): _source_fmt = 'xml' _target_fmt = 'json' with open(os.path.join(test_resources, 'good.%s' % _source_fmt), 'r') as _a: _a_seria = seria.load(_a) _b = StringIO() _b.write(_a_seria.dump(fmt=_target_fmt)) _b_seria = seria.load(_b) _c = StringIO() _c.write(_b_seria.dump(fmt=_source_fmt, pretty=True, newl='\n', indent=' ')) _c.seek(0) _a.seek(0) assert _a.read() == _c.read()
def test_cli_yaml_to_xml_to_stdout(self): _input_fmt = 'yaml' _output_fmt = 'xml' result = runner.invoke(cli.cli, ['--%s' % (_output_fmt), "%s/good.%s" % (test_resources, _input_fmt)], '-') with open("%s/good.%s" % (test_resources, _input_fmt), 'rb') as f: _s = seria.load(f) _comp_val = _s.dump(_output_fmt) assert result.exit_code == 0 assert result.output == _comp_val
def _load_file(self, f): """Get values from config file""" try: with open(f, 'r') as _fo: _seria_in = seria.load(_fo) _y = _seria_in.dump('yaml') except IOError: raise FiggypyError("could not open configuration file") self.values.update(yaml.load(_y))
def test_xml_to_json_to_xml(self): _source_fmt = 'xml' _target_fmt = 'json' with open(os.path.join(test_resources, 'good.%s' % _source_fmt), 'r') as _a: _a_seria = seria.load(_a) _b = StringIO() _b.write(_a_seria.dump(fmt=_target_fmt)) _b_seria = seria.load(_b) _c = StringIO() _c.write( _b_seria.dump(fmt=_source_fmt, pretty=True, newl='\n', indent=' ')) _c.seek(0) _a.seek(0) assert _a.read() == _c.read()
def cli(out_fmt, input, output): """Converts text.""" _input = StringIO() for l in input: try: _input.write(str(l)) except TypeError: _input.write(bytes(l, 'utf-8')) _input = seria.load(_input) _out = (_input.dump(out_fmt)) output.write(_out)
def _get_cfg(self, f): """Get configuration from config file""" try: with open(f, 'r') as _fo: _seria_in = seria.load(_fo) _y = _seria_in.dump('yaml') except IOError: raise FiggypyError("could not open configuration file") _cfg = yaml.load(_y) self._post_load_process(_cfg, self._gpg_config) for k, v in _cfg.items(): setattr(self, k, v)
def main(): _serialized_obj = None args = docopt(docopt_args) if args['INPUT'] != '-': with open(args['INPUT'], 'rb') as f: _serialized_obj = seria.load(f) elif args['INPUT'] == '-': _serialized_obj = StringIO() for l in sys.stdin: try: _serialized_obj.write(str(l)) except TypeError: _serialized_obj.write(bytes(l, 'utf-8')) _serialized_obj = seria.load(_serialized_obj) if args['--json']: sys.stdout.write(_serialized_obj.dump('json')) if args['--xml']: sys.stdout.write(_serialized_obj.dump('xml')) if args['--yaml']: sys.stdout.write(_serialized_obj.dump('yaml'))
def _get_cfg(self, f): """Get configuration from config file""" try: with open(f, 'r') as _fo: try: _seria_in = seria.load(_fo) _y = _seria_in.dump('yaml') except Exception as e: raise except IOError: raise FiggyPyError("could not open configuration file") _cfg = yaml.load(_y) for k, v in _cfg.items(): setattr(self, k, v)
def test_input_must_be_flo(self): with pytest.raises(Serializer.Error): _ = seria.load("somestring")
def _convert_cm_to_json(filename): with open(filename) as f: s = seria.load(f) j = json.loads(s.dump('json')) return j
def test_format_property_access(self): with open(os.path.join(test_resources, 'good.xml'), 'r') as f: _stream = seria.load(f) assert _stream.is_json == False assert _stream.is_xml == True assert _stream.is_yaml == False