@@ -38,9 +38,18 @@ class Component(ABC, MutableMapping):
3838
3939 @property
4040 def path (self ) -> Path :
41+ """Get the path to the component's input file."""
4142 return Path .cwd () / self .filename
4243
43- def _default_filename (self ) -> str :
44+ def default_filename (self ) -> str :
45+ """
46+ Generate a default filename for the component.
47+ By default, this is the component's name then
48+ the class name in lowercase, separated by dot.
49+
50+ Override this method in subclasses to provide
51+ a custom default filename.
52+ """
4453 name = self .name # type: ignore
4554 cls_name = self .__class__ .__name__ .lower ()
4655 return f"{ name } .{ cls_name } "
@@ -50,10 +59,6 @@ def __attrs_init_subclass__(cls):
5059 COMPONENTS [cls .__name__ .lower ()] = cls
5160 cls .dfn = cls .get_dfn ()
5261
53- def __attrs_post_init__ (self ):
54- if not self .filename :
55- self .filename = self ._default_filename ()
56-
5762 def __getitem__ (self , key ):
5863 return self .children [key ] # type: ignore
5964
@@ -89,12 +94,21 @@ def get_dfn(cls) -> Dfn:
8994 ** blocks ,
9095 )
9196
97+ def _preio (self , format : str ) -> None :
98+ """Place for any pre-IO setup"""
99+ if not self .filename :
100+ self .filename = self .default_filename ()
101+
92102 def load (self , format : str ) -> None :
103+ """Load the component from an input file."""
104+ self ._preio (format = format )
93105 self ._load (format = format )
94106 for child in self .children .values (): # type: ignore
95- child .load (format )
107+ child .load (format = format )
96108
97109 def write (self , format : str ) -> None :
110+ """Write the component to an input file."""
111+ self ._preio (format = format )
98112 self ._write (format = format )
99113 for child in self .children .values (): # type: ignore
100- child .write (format )
114+ child .write (format = format )
0 commit comments