def saver(cm): output = utils.verify_filename(filename) if output['dir']: os.makedirs(output['path'], exist_ok=True) input_filename = os.path.splitext(os.path.basename(cm.path))[0] output['path'] = os.path.join( output['path'], '{f}.{ext}'.format(f=input_filename, ext='json')) else: os.makedirs(os.path.dirname(output['path']), exist_ok=True) try: if "metadata" in cm.j: cm.j["metadata"]["fileIdentifier"] = os.path.basename( output['path']) except: pass utils.print_cmd_status("Saving CityJSON to a file %s" % output['path']) try: fo = click.open_file(output['path'], mode='w') if textures: cm.copy_textures(textures, output['path']) if indent: json_str = json.dumps(cm.j, indent="\t") fo.write(json_str) else: json_str = json.dumps(cm.j, separators=(',', ':')) fo.write(json_str) except IOError as e: raise click.ClickException('Invalid output file: %s \n%s' % (output['path'], e))
def exporter(cm): output = utils.verify_filename(filename) if output['dir']: os.makedirs(output['path'], exist_ok=True) input_filename = os.path.splitext(os.path.basename(cm.path))[0] output['path'] = os.path.join( output['path'], '{f}.{ext}'.format(f=input_filename, ext=format)) else: os.makedirs(os.path.dirname(output['path']), exist_ok=True) if format.lower() == 'obj': utils.print_cmd_status("Exporting CityJSON to OBJ (%s)" % (output['path'])) try: with click.open_file(output['path'], mode='w') as fo: re = cm.export2obj() fo.write(re.getvalue()) except IOError as e: raise click.ClickException('Invalid output file: "%s".\n%s' % (output['path'], e)) elif format.lower() == 'stl': utils.print_cmd_status("Exporting CityJSON to STL (%s)" % (output['path'])) try: with click.open_file(output['path'], mode='w') as fo: re = cm.export2stl() fo.write(re.getvalue()) except IOError as e: raise click.ClickException('Invalid output file: "%s".\n%s' % (output['path'], e)) elif format.lower() == 'glb': fname = os.path.splitext(os.path.basename(output['path']))[0] bufferbin = "{}.glb".format(fname) binfile = os.path.join(os.path.dirname(output['path']), bufferbin) utils.print_cmd_status("Exporting CityJSON to glb %s" % binfile) glb = cm.export2gltf() # TODO B: how many buffer can there be in the 'buffers'? try: glb.seek(0) with click.open_file(binfile, mode='wb') as bo: bo.write(glb.getvalue()) except IOError as e: raise click.ClickException('Invalid output file: "%s".\n%s' % (binfile, e)) elif format.lower() == 'b3dm': fname = os.path.splitext(os.path.basename(output['path']))[0] b3dmbin = "{}.b3dm".format(fname) binfile = os.path.join(os.path.dirname(output['path']), b3dmbin) b3dm = cm.export2b3dm() utils.print_cmd_status("Exporting CityJSON to b3dm %s" % binfile) utils.print_cmd_warning( "Although the conversion works, the output is probably incorrect." ) try: b3dm.seek(0) with click.open_file(binfile, mode='wb') as bo: bo.write(b3dm.getvalue()) except IOError as e: raise click.ClickException('Invalid output file: "%s".\n%s' % (binfile, e))
def test_verify_filenames_invalid(self, invalid_path): with pytest.raises(ClickException) as e: utils.verify_filename(invalid_path[0])
def test_verify_filenames_valid(self, valid_path): res = utils.verify_filename(valid_path[0]) assert res['dir'] == valid_path[1]