55import re
66from configparser import ConfigParser
77
8+ from tomllib import load as toml_load
9+ from tomli_w import dump as toml_dump
10+
811from countess .core .pipeline import PipelineGraph , PipelineNode
912from countess .core .plugins import load_plugin
1013
1114logger = logging .getLogger (__name__ )
1215
1316
14- def read_config_dict (name : str , base_dir : str , config_dict : dict ) -> PipelineNode :
17+ def read_config_meta (name : str , config_dict : dict ) -> PipelineNode :
1518 if "_module" in config_dict :
1619 module_name = config_dict ["_module" ]
1720 class_name = config_dict ["_class" ]
@@ -37,7 +40,7 @@ def read_config_dict(name: str, base_dir: str, config_dict: dict) -> PipelineNod
3740
3841 # XXX check version and hash_digest and emit warnings.
3942
40- node = PipelineNode (
43+ return PipelineNode (
4144 name = name ,
4245 uuid = config_dict .get ("_uuid" ),
4346 plugin = plugin ,
@@ -46,11 +49,33 @@ def read_config_dict(name: str, base_dir: str, config_dict: dict) -> PipelineNod
4649 sort_column = int (sort [0 ]),
4750 sort_descending = bool (int (sort [1 ])),
4851 )
52+
53+
54+ def read_config_toml (filename : str ) -> PipelineGraph :
55+ base_dir = os .path .dirname (filename )
56+ pipeline_graph = PipelineGraph ()
57+ nodes_by_name : dict [str , PipelineNode ] = {}
58+ with open (filename , "rb" ) as fh :
59+ doc = toml_load (fh )
60+ for name , config_dict in doc .items ():
61+ node = read_config_meta (name , config_dict )
62+ node .plugin .set_config ({
63+ k : v
64+ for k , v in config_dict .items ()
65+ if not k .startswith ("_" )
66+ }, base_dir )
67+ for key , val in config_dict .items ():
68+ if key .startswith ("_parent." ):
69+ node .add_parent (nodes_by_name [val ])
70+ pipeline_graph .nodes .append (node )
71+ nodes_by_name [name ] = node
72+
73+ def read_config_dict (name : str , base_dir : str , config_dict : dict ) -> PipelineNode :
74+ node = read_config_meta (name , config_dict )
4975 for key , val in config_dict .items ():
5076 if not key .startswith ("_" ):
5177 node .set_config (key , ast .literal_eval (val ), base_dir )
52- return node
53-
78+ return node
5479
5580def read_config (
5681 filenames : list [str ],
@@ -110,31 +135,53 @@ def write_config_node_string(node: PipelineNode, base_dir: str = ""):
110135 return buf .getvalue ()
111136
112137
113- def write_config_node (node : PipelineNode , cp : ConfigParser , base_dir : str ):
114- cp .add_section (node .name )
138+ def get_config_meta (node : PipelineNode ) -> dict [str , str ]:
139+ meta = { "_uuid" : node .uuid }
140+ if node .sort_column :
141+ desc = 1 if node .sort_descending else 0
142+ meta ["_sort" ] = "%d %d" % (node .sort_column , desc )
115143 if node .plugin :
116- cp [node .name ].update (
117- {
118- "_uuid" : node .uuid ,
119- "_module" : node .plugin .__module__ ,
120- "_class" : node .plugin .__class__ .__name__ ,
121- "_version" : node .plugin .version ,
122- "_hash" : node .plugin .hash (),
123- "_sort" : "%d %d" % (node .sort_column , 1 if node .sort_descending else 0 ),
124- }
125- )
144+ meta .update ({
145+ "_module" : node .plugin .__module__ ,
146+ "_class" : node .plugin .__class__ .__name__ ,
147+ "_version" : node .plugin .version ,
148+ "_hash" : node .plugin .hash (),
149+ })
126150 if node .position :
127151 xx , yy = node .position
128- cp [ node . name ] ["_position" ] = "%d %d" % (xx * 1000 , yy * 1000 )
152+ meta ["_position" ] = "%d %d" % (xx * 1000 , yy * 1000 )
129153 if node .notes :
130- cp [ node . name ] ["_notes" ] = node .notes
154+ meta ["_notes" ] = node .notes
131155 for n , parent in enumerate (node .parent_nodes ):
132- cp [node .name ][f"_parent.{ n } " ] = parent .name
156+ meta [f"_parent.{ n } " ] = parent .name
157+ return meta
158+
159+
160+ def write_config_node (node : PipelineNode , cp : ConfigParser , base_dir : str ):
161+ cp .add_section (node .name )
162+ if node .plugin :
163+ cp [node .name ].update (get_config_meta (node ))
133164 if node .plugin :
134165 node .load_config ()
135166 for k , v in node .plugin .get_parameters ("" , base_dir ):
136167 cp [node .name ][k ] = repr (v )
137168
169+ def make_config_toml_node (node : PipelineNode , base_dir : str = "" ):
170+ tab = get_config_meta (node )
171+ node .load_config ()
172+ tab .update (node .plugin .get_config (base_dir ))
173+ return tab
174+
175+ def make_config_toml (pipeline_graph : PipelineGraph , base_dir : str = "" ):
176+ return {
177+ node .name : make_config_toml_node (node , base_dir )
178+ for node in pipeline_graph .traverse_nodes ()
179+ }
180+
181+ def write_config_toml (pipeline_graph : PipelineGraph , filename : str ):
182+ base_dir = os .path .dirname (filename )
183+ with open (filename , "wb" ) as fh :
184+ toml_dump (make_config_toml (pipeline_graph , base_dir ), fh )
138185
139186def export_config_graphviz (pipeline_graph : PipelineGraph , filename : str ):
140187 with open (filename , "w" , encoding = "utf-8" ) as fh :
0 commit comments