Skip to content

Commit 57fa320

Browse files
committed
fix save to file func
1 parent a7df998 commit 57fa320

File tree

2 files changed

+51
-21
lines changed

2 files changed

+51
-21
lines changed

cultcargo/utils.py

+49-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import Dict, List, Union, Optional, Callable, Any
2-
from scabha.cargo import Parameter, _UNSET_DEFAULT, EmptyDictDefault, ParameterPolicies
2+
from scabha.cargo import Parameter, _UNSET_DEFAULT, EmptyListDefault
3+
from scabha.cargo import EmptyDictDefault, ParameterPolicies, Cargo
34
from scabha.basetypes import File, Directory
45
from scabha.validate import validate_parameters
56
from dataclasses import dataclass
@@ -10,22 +11,46 @@
1011
@dataclass
1112
class OldParameter:
1213
name: str
13-
dtype: str
14+
dtype: Any
1415
info: str
1516
default: Any = None
1617
required: bool = False
17-
choices: List[Any] = None
18-
io: str = None
19-
mapping: str = None
18+
choices: Optional[List[Any]] = None
19+
io: Optional[str] = ""
20+
mapping: Optional[str] = ""
2021
check_io: bool = False
2122
deprecated: bool = False
2223
positional: bool = False
2324

2425

2526
@dataclass
27+
class OldCab:
28+
task: str
29+
base: str
30+
version: List[str]
31+
binary: str = ""
32+
description: str = "<documentation>"
33+
prefix: str = "--"
34+
parameters: Optional[List[Dict]] = None
35+
tag: Optional[List[str]] = None
36+
junk: Optional[List[str]] = None
37+
msdir: bool = False
38+
wranglers: Optional[List[str]] = None
39+
2640
class SimpleCab:
27-
inputs: Dict[str, Any] = EmptyDictDefault()
28-
outputs: Dict[str, Any] = EmptyDictDefault()
41+
def __init__(self, oldfile: File):
42+
self.oldfile = oldfile
43+
cab_strct = OmegaConf.structured(OldCab)
44+
param_strct = OmegaConf.structured(OldParameter)
45+
_oldcab = OmegaConf.load(oldfile)
46+
self.oldcab = OmegaConf.merge(cab_strct,
47+
OmegaConf.load(oldfile))
48+
self.parameters = []
49+
for param in _oldcab.parameters:
50+
pardict = OmegaConf.merge(param_strct, param)
51+
self.parameters.append(pardict)
52+
53+
2954

3055
def __to_new_dtype(self, param:OldParameter) -> str:
3156
new_dtype = []
@@ -58,27 +83,31 @@ def __to_new_dtype(self, param:OldParameter) -> str:
5883
return "Union[" + ",".join(new_dtype) + "]"
5984
else:
6085
return new_dtype[0]
86+
6187

62-
def init_from_old_cab(self, oldcab_file: File):
88+
def to_new_params(self, set_inputs=True):
6389
"""AI is creating summary for init_from_old_cab
6490
6591
Args:
6692
oldcab (File): [description]
6793
"""
68-
oldcab = OmegaConf.load(oldcab_file)
69-
70-
self.inputs = {}
71-
self.outputs = {}
72-
for param in oldcab.parameters:
94+
param_struct = OmegaConf.structured(Parameter)
95+
params = {}
96+
for param in self.parameters:
7397
dtype = self.__to_new_dtype(param)
74-
oldparam = OldParameter(**param)
7598

76-
policies = ParameterPolicies(positional=oldparam.positional)
77-
78-
self.inputs[param.name] = Parameter(info=oldparam.info, dtype=dtype, policies=policies,
79-
nom_de_guerre=oldparam.mapping,
80-
must_exist=oldparam.check_io)
99+
policies = ParameterPolicies(positional=param.positional)
81100

101+
params[param.name] = OmegaConf.merge(param_struct,
102+
dict(info=param.info, dtype=dtype, policies=policies,
103+
nom_de_guerre=param.mapping,
104+
must_exist=param.check_io))
105+
if set_inputs:
106+
self.inputs = params
107+
self.outputs = {}
108+
return OmegaConf.create(params)
109+
110+
82111
def save(self, path: str):
83112
"""_summary_
84113
@@ -88,6 +117,7 @@ def save(self, path: str):
88117
Returns:
89118
_type_: _description_
90119
"""
120+
91121
outdict = OmegaConf.create({"inputs": self.inputs,
92122
"outputs": self.outputs,
93123
})

tests/simple_cab.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
oldcab = File(f"{testdir}/casa-mstransform.json")
77
newcab = f"{oldcab.BASENAME}.yaml"
8-
schema = utils.SimpleCab()
9-
schema.init_from_old_cab(oldcab)
8+
schema = utils.SimpleCab(oldcab)
9+
schema.to_new_params()
1010
schema.save(newcab)
1111

0 commit comments

Comments
 (0)