@@ -133,7 +133,7 @@ def __init__(
133133
134134 self .path = path
135135 self .relpath = relpath
136- self .root_path = Path ( str ( path ). removesuffix ( str ( relpath ))) if path else None
136+ self .root_path = self . _get_root_path ()
137137 self .mtime = mtime
138138 self .tmpl = tmpl
139139
@@ -152,37 +152,19 @@ def from_cache(
152152 return None
153153
154154 self = cls (name = cache ["name" ])
155- self .prefix = cache ["prefix" ]
156- self .url_prefix = cache ["url_prefix" ]
157- self .required = cache ["required" ]
158- self .optional = cache ["optional" ]
159- self .css = cache ["css" ]
160- self .js = cache ["js" ]
161- self .path = path
162- self .mtime = cache ["mtime" ]
163- self .tmpl = cache ["tmpl" ]
155+ for key in self .__slots__ :
156+ setattr (self , key , cache [key ])
164157
165- if self .tmpl and globals :
158+ if self .tmpl :
166159 # Create a copy of the globals dictionary to ensure thread safety
167160 globals_copy = {** self .tmpl .globals }
168- globals_copy .update (globals )
161+ globals_copy .update (globals or {} )
169162 self .tmpl .globals = globals_copy
170163
171164 return self
172165
173166 def serialize (self ) -> dict [str , t .Any ]:
174- return {
175- "name" : self .name ,
176- "prefix" : self .prefix ,
177- "url_prefix" : self .url_prefix ,
178- "required" : self .required ,
179- "optional" : self .optional ,
180- "css" : self .css ,
181- "js" : self .js ,
182- "path" : self .path ,
183- "mtime" : self .mtime ,
184- "tmpl" : self .tmpl ,
185- }
167+ return {k : getattr (self , k ) for k in self .__slots__ }
186168
187169 def load_metadata (self , source : str ) -> None :
188170 match = RX_META_HEADER .match (source )
@@ -279,3 +261,13 @@ def render(self, **kwargs):
279261
280262 def __repr__ (self ) -> str :
281263 return f'<Component "{ self .name } ">'
264+
265+ def _get_root_path (self ) -> Path | None :
266+ """Get the root path of the component."""
267+ if self .path is None or self .relpath is None :
268+ return None
269+ suffix = str (self .relpath .as_posix ())
270+ if self .url_prefix :
271+ suffix = f"{ self .url_prefix } { suffix } "
272+
273+ return Path (str (self .path ).removesuffix (str (suffix )))
0 commit comments