def write_toml(self, data, path=None): """Writes the given data structure out as TOML.""" if path is None: path = self.pipfile_location try: formatted_data = contoml.dumps(data).rstrip() except Exception: for section in ("packages", "dev-packages"): for package in data.get(section, {}): # Convert things to inline tables — fancy :) if hasattr(data[section][package], "keys"): _data = data[section][package] data[section][package] = toml.TomlDecoder( ).get_empty_inline_table() data[section][package].update(_data) formatted_data = toml.dumps(data).rstrip() if (vistir.compat.Path(path).absolute() == vistir.compat.Path( self.pipfile_location).absolute()): newlines = self._pipfile_newlines else: newlines = DEFAULT_NEWLINES formatted_data = cleanup_toml(formatted_data) with io.open(path, "w", newline=newlines) as f: f.write(formatted_data) # pipfile is mutated! self.clear_pipfile_cache()
def test_dict_decoder(): class TestDict(dict): pass test_dict_decoder = toml.TomlDecoder(TestDict) assert isinstance(toml.loads(TEST_STR, decoder=test_dict_decoder), TestDict)
def main(out, template): zense = PyPicoZenseManager(0) decoder = toml.TomlDecoder(_dict=OrderedDict) encoder = toml.TomlEncoder(_dict=OrderedDict) toml.TomlEncoder = encoder dict_toml = toml.load(open(template), _dict=OrderedDict, decoder=decoder) dict_toml["Camera0"]["serial_no"] = zense.getSerialNumber().decode() params = zense.getCameraParameter() dict_toml["Camera0"]["fx"] = params[0] dict_toml["Camera0"]["fy"] = params[1] dict_toml["Camera0"]["cx"] = params[2] dict_toml["Camera0"]["cy"] = params[3] dict_toml["Camera0_Factory"]["fx"] = params[0] dict_toml["Camera0_Factory"]["fy"] = params[1] dict_toml["Camera0_Factory"]["cx"] = params[2] dict_toml["Camera0_Factory"]["cy"] = params[3] dict_toml["Camera0_Factory"]["p1"] = params[4] dict_toml["Camera0_Factory"]["p2"] = params[5] dict_toml["Camera0_Factory"]["k1"] = params[6] dict_toml["Camera0_Factory"]["k2"] = params[7] dict_toml["Camera0_Factory"]["k3"] = params[8] dict_toml["Camera0_Factory"]["k4"] = params[9] dict_toml["Camera0_Factory"]["k5"] = params[10] dict_toml["Camera0_Factory"]["k6"] = params[11] with open(out, "w") as f: toml.encoder.dump(dict_toml, f) print("generated") del zense
def _parse_pipfile(self, contents): # If any outline tables are present... if ("[packages." in contents) or ("[dev-packages." in contents): data = toml.loads(contents) # Convert all outline tables to inline tables. for section in ("packages", "dev-packages"): for package in data.get(section, {}): # Convert things to inline tables — fancy :) if hasattr(data[section][package], "keys"): _data = data[section][package] data[section][package] = toml.TomlDecoder( ).get_empty_inline_table() data[section][package].update(_data) toml_encoder = toml.TomlEncoder(preserve=True) # We lose comments here, but it's for the best.) try: return contoml.loads(toml.dumps(data, encoder=toml_encoder)) except RuntimeError: return toml.loads(toml.dumps(data, encoder=toml_encoder)) else: # Fallback to toml parser, for large files. try: return contoml.loads(contents) except Exception: return toml.loads(contents)
def convert_toml_table(section): for package, value in section.items(): if hasattr(value, "keys") and not isinstance( value, toml.decoder.InlineTableDict): table = toml.TomlDecoder().get_empty_inline_table() table.update(value) section[package] = table
def main(toml_path, toml_template_path, yaml_path, fill_see3cam_id): decoder = toml.TomlDecoder(_dict=OrderedDict) encoder = toml.TomlEncoder(_dict=OrderedDict) f_yaml = open(yaml_path, "r") data = yaml.load(f_yaml) toml.TomlEncoder = encoder dict_toml = toml.load(open(toml_template_path), _dict=OrderedDict, decoder=decoder) if fill_see3cam_id: dict_toml["Rgb"]["device_id"] = get_see3cam_device_id() dict_toml["Rgb"]["width"] = data["cam0"]["resolution"][0] dict_toml["Rgb"]["height"] = data["cam0"]["resolution"][1] dict_toml["Rgb"]["fx"] = data["cam0"]["intrinsics"][0] dict_toml["Rgb"]["fy"] = data["cam0"]["intrinsics"][1] dict_toml["Rgb"]["cx"] = data["cam0"]["intrinsics"][2] dict_toml["Rgb"]["cy"] = data["cam0"]["intrinsics"][3] dict_toml["Rgb"]["k1"] = data["cam0"]["distortion_coeffs"][0] dict_toml["Rgb"]["k2"] = data["cam0"]["distortion_coeffs"][1] dict_toml["Rgb"]["k3"] = data["cam0"]["distortion_coeffs"][2] dict_toml["Rgb"]["k4"] = data["cam0"]["distortion_coeffs"][3] dict_toml["Rgb"]["k5"] = 0 dict_toml["Rgb"]["k6"] = 0 with open(toml_path, "w") as f: toml.encoder.dump(dict_toml, f) print("generated")
def get_camera_parameter(toml_path): toml_dict = toml.load(open(toml_path)) toml_decoder = toml.TomlDecoder(_dict=OrderedDict) intrinsic = IntrinsicParameter() intrinsic.set_intrinsic_parameter( *[toml_dict["Rgb"][elem] for elem in ["fx", "fy", "cx", "cy"]]) dist_coeffs = [toml_dict["Rgb"][elem] for elem in ["k1", "k2", "k3", "k4"]] return intrinsic, dist_coeffs
def convert_toml_outline_tables(parsed): """Converts all outline tables to inline tables.""" if isinstance(parsed, tomlkit.container.Container): empty_inline_table = tomlkit.inline_table else: empty_inline_table = toml.TomlDecoder().get_empty_inline_table for section in ("packages", "dev-packages"): table_data = parsed.get(section, {}) for package, value in table_data.items(): if hasattr(value, "keys") and not isinstance( value, (tomlkit.items.InlineTable, toml.decoder.InlineTableDict)): table = empty_inline_table() table.update(value) table_data[package] = table return parsed
def main(out): zense = PyPicoZenseManager(0) decoder = toml.TomlDecoder(_dict=OrderedDict) encoder = toml.TomlEncoder(_dict=OrderedDict) toml.TomlEncoder = encoder dict_toml = toml.load(open('template.toml'), _dict=OrderedDict, decoder=decoder) dict_toml["Camera0"]["serial_no"] = zense.getSerialNumber() intrinsic_depth_params = zense.getCameraParameter() intrinsic_rgb_params = zense.getRGBCameraParameter() intrinsic_elems = [ "fx", "fy", "cx", "cy", "p1", "p2", "k1", "k2", "k3", "k4", "k5", "k6" ] for i, _elem in enumerate(intrinsic_elems): dict_toml["Camera0_Factory"][_elem] = intrinsic_depth_params[i] for i, _elem in enumerate(intrinsic_elems): dict_toml["Camera0_RGB_Factory"][_elem] = intrinsic_rgb_params[i] ext_params = zense.getExtrinsicParameter() _rotation_ext = ext_params[0] _translation_ext = ext_params[1] rotation_extrinsic_elems = [ "r11", "r12", "r13", "r21", "r22", "r23", "r31", "r32", "r33" ] translation_extrinsic_elems = ["tx", "ty", "tz"] for i, _elem in enumerate(rotation_extrinsic_elems): dict_toml["Camera0_Extrinsic_Factory"][_elem] = _rotation_ext[i] for i, _elem in enumerate(translation_extrinsic_elems): dict_toml["Camera0_Extrinsic_Factory"][_elem] = _translation_ext[i] with open(out, "w") as f: toml.encoder.dump(dict_toml, f) print("generated") del zense
def loads( s: str, dict_: Type[_M] = dict, # type: ignore[assignment] decoder: Union[Type[toml.TomlDecoder], toml.TomlDecoder] = toml.TomlDecoder, ) -> _M: r""" Parse the given string as TOML. :param s: :param dict\_: The class of the returned data. :param decoder: The :class:`toml.TomlEncoder` to use for constructing the output string. :returns: A mapping containing the ``TOML`` data. .. versionchanged:: 0.5.0 * The default value for ``decoder`` changed from :py:obj:`None` to :class:`toml.TomlDecoder` Explicitly passing ``decoder=``\ :py:obj:`None` is deprecated and support will be removed in 1.0.0 * Instead, pass a decoder class or, if you use the ``dict_`` option, an instance of the decoder class for ``dict_``. """ if decoder is None: warnings.warn( "Passing decoder=None to 'dom_toml.loads' is deprecated since 0.5.0 and support will be removed in 1.0.0", DeprecationWarning, ) decoder = toml.TomlDecoder(dict_) elif isinstance(decoder, type): if dict_ is dict: decoder = decoder() else: # TODO: deprecate this behaviour and the dict_ option in favour of passing an instance of the encoder. decoder = decoder(dict_) return toml.loads( # type: ignore[return-value] s, decoder=decoder, )
def main(): cfg_template_path = str(Path(CFG_DIR_PATH, "realsense_toml_template.toml")) cfg_output_path = str(Path(CFG_DIR_PATH, "realsense.toml")) decoder = toml.TomlDecoder(_dict=OrderedDict) encoder = toml.TomlEncoder(_dict=OrderedDict) toml.TomlEncoder = encoder dict_toml = toml.load(open(cfg_template_path), _dict=OrderedDict, decoder=decoder) rs_mng = RealSenseManager() intrinsic_depth = rs_mng.intrinsic_depth intrinsic_color = rs_mng.intrinsic_color intrinsic_ir_left = rs_mng.intrinsic_ir_left intrinsic_ir_right = rs_mng.intrinsic_ir_right translation_l2r = rs_mng.translation_ir_left2right translation_l2c = rs_mng.translation_ir_left2color rotation_dcm_l2r = rs_mng.rotation_dcm_ir_left2right rotation_dcm_l2c = rs_mng.rotation_dcm_ir_left2color set_intrinsics(dict_toml, "RGB_Intrinsics", intrinsic_color) set_intrinsics(dict_toml, "Depth_Intrinsics", intrinsic_depth) set_intrinsics(dict_toml, "IR_Left_Intrinsics", intrinsic_ir_left) set_intrinsics(dict_toml, "IR_Right_Intrinsics", intrinsic_ir_right) set_translation(dict_toml, "IR_L2R_Translation", translation_l2r) set_translation(dict_toml, "IR_L2C_Translation", translation_l2c) set_rotation(dict_toml, "IR_L2R_Rotation", rotation_dcm_l2r) set_rotation(dict_toml, "IR_L2C_Rotation", rotation_dcm_l2c) with open(cfg_output_path, "w") as f: toml.encoder.dump(dict_toml, f) print("generated") del rs_mng
def main(out): zense = PyPicoZenseManager(0) decoder = toml.TomlDecoder(_dict=OrderedDict) encoder = toml.TomlEncoder(_dict=OrderedDict) toml.TomlEncoder = encoder dict_toml = toml.load(open('template.toml'), _dict=OrderedDict, decoder=decoder) dict_toml["Camera0"]["serial_no"] = zense.getSerialNumber() params = zense.getCameraParameter() intrinsic_elems = ["fx", "fy", "cx", "cy", \ "p1", "p2", "k1", "k2", \ "k3", "k4", "k5", "k6"] for i, _elem in enumerate(intrinsic_elems): dict_toml["Camera0_Factory"][_elem] = params[i] with open(out, "w") as f: toml.encoder.dump(dict_toml, f) print("generated") del zense
def __init__(self): self.decoder = toml.TomlDecoder()
__version__ = '0.1.2' import toml import os import re RE_ENV_VAR = r'\$([A-Z_][A-Z0-9_]+)' decoder = toml.TomlDecoder() def env_replace(x): env_var = x.groups()[0] p = os.environ.get(env_var, '') return p def process(item): iter_ = None if isinstance(item, dict): iter_ = item.items() elif isinstance(item, list): iter_ = enumerate(item) for i, val in iter_: if isinstance(val, (dict, list)): process(val) elif isinstance(val, str): if re.match(RE_ENV_VAR, val): r = re.sub(RE_ENV_VAR, env_replace, val) # Try to first load the value from the environment variable
def decode_toml_value(s): try: return toml.TomlDecoder().load_value(s)[0] except Exception: return s