11import os
2+ import pathlib
23import sys
34
45import click
5- import kaptan
6+
7+ from tmuxp .config_reader import ConfigReader
68
79from .. import config
810from .utils import ConfigPath , _validate_choices , get_abs_path , tmuxp_echo
@@ -58,25 +60,22 @@ def command_import():
5860
5961
6062def import_config (configfile , importfunc ):
61- configparser = kaptan .Kaptan (handler = "yaml" )
62-
63- configparser .import_config (configfile )
64- newconfig = importfunc (configparser .get ())
65- configparser .import_config (newconfig )
63+ existing_config = ConfigReader ._from_file (pathlib .Path (configfile ))
64+ new_config = ConfigReader (importfunc (existing_config ))
6665
6766 config_format = click .prompt (
6867 "Convert to" , value_proc = _validate_choices (["yaml" , "json" ]), default = "yaml"
6968 )
7069
7170 if config_format == "yaml" :
72- newconfig = configparser . export ("yaml" , indent = 2 , default_flow_style = False )
71+ new_config = new_config . dump ("yaml" , indent = 2 , default_flow_style = False )
7372 elif config_format == "json" :
74- newconfig = configparser . export ("json" , indent = 2 )
73+ new_config = new_config . dump ("json" , indent = 2 )
7574 else :
7675 sys .exit ("Unknown config format." )
7776
7877 tmuxp_echo (
79- newconfig + "---------------------------------------------------------------"
78+ new_config + "---------------------------------------------------------------"
8079 "\n "
8180 "Configuration import does its best to convert files.\n "
8281 )
@@ -94,7 +93,7 @@ def import_config(configfile, importfunc):
9493 dest = dest_path
9594
9695 buf = open (dest , "w" )
97- buf .write (newconfig )
96+ buf .write (new_config )
9897 buf .close ()
9998
10099 tmuxp_echo ("Saved to %s." % dest )
@@ -138,12 +137,11 @@ def command_convert(confirmed, config):
138137 f"Unknown filetype: { ext } (valid: [.json, .yaml, .yml])"
139138 )
140139
141- configparser = kaptan .Kaptan ()
142- configparser .import_config (config )
140+ configparser = ConfigReader .from_file (config )
143141 newfile = config .replace (ext , ".%s" % to_filetype )
144142
145143 export_kwargs = {"default_flow_style" : False } if to_filetype == "yaml" else {}
146- newconfig = configparser .export ( to_filetype , indent = 2 , ** export_kwargs )
144+ new_config = configparser .dump ( format = to_filetype , indent = 2 , ** export_kwargs )
147145
148146 if not confirmed :
149147 if click .confirm (f"convert to <{ config } > to { to_filetype } ?" ):
@@ -152,7 +150,7 @@ def command_convert(confirmed, config):
152150
153151 if confirmed :
154152 buf = open (newfile , "w" )
155- buf .write (newconfig )
153+ buf .write (new_config )
156154 buf .close ()
157155 print ("New config saved to <%s>." % newfile )
158156
0 commit comments