@@ -1067,7 +1067,36 @@ def needs_capability(self, capability: str) -> None:
10671067 )
10681068
10691069
1070- class PulpRemoteContext (PulpEntityContext ):
1070+ class PulpMasterContext (PulpEntityContext ):
1071+ TYPE_REGISTRY : t .ClassVar [t .Dict [str , t .Type [t .Self ]]]
1072+
1073+ def __init_subclass__ (cls , ** kwargs : t .Any ) -> None :
1074+ super ().__init_subclass__ (** kwargs )
1075+ if not hasattr (cls , "RESOURCE_TYPE" ):
1076+ cls .TYPE_REGISTRY = {}
1077+ elif hasattr (cls , "PLUGIN" ):
1078+ cls .TYPE_REGISTRY [f"{ cls .PLUGIN } :{ cls .RESOURCE_TYPE } " ] = cls
1079+
1080+ def detail_context (self , pulp_href : str ) -> t .Self :
1081+ """
1082+ Provide a detail context for a matching href.
1083+ """
1084+ m = re .search (self .HREF_PATTERN , pulp_href )
1085+ if m is None :
1086+ raise PulpException (f"'{ pulp_href } ' is not an href for { self .ENTITY } ." )
1087+ plugin = m .group ("plugin" )
1088+ resource_type = m .group ("resource_type" )
1089+ try :
1090+ detail_class = self .TYPE_REGISTRY [f"{ plugin } :{ resource_type } " ]
1091+ except KeyError :
1092+ raise PulpException (
1093+ f"{ self .ENTITY } with plugin '{ plugin } ' and"
1094+ f"resource type '{ resource_type } ' is unknown."
1095+ )
1096+ return detail_class (self .pulp_ctx , pulp_href = pulp_href )
1097+
1098+
1099+ class PulpRemoteContext (PulpMasterContext ):
10711100 """
10721101 Base class for remote contexts.
10731102 """
@@ -1093,27 +1122,15 @@ class PulpRemoteContext(PulpEntityContext):
10931122 "sock_read_timeout" ,
10941123 "rate_limit" ,
10951124 }
1096- TYPE_REGISTRY : t .Dict [str , t .Type ["PulpRemoteContext" ]] = {}
1097-
1098- def __init_subclass__ (cls , ** kwargs : t .Any ) -> None :
1099- super ().__init_subclass__ (** kwargs )
1100- if hasattr (cls , "PLUGIN" ) and hasattr (cls , "RESOURCE_TYPE" ):
1101- cls .TYPE_REGISTRY [f"{ cls .PLUGIN } :{ cls .RESOURCE_TYPE } " ] = cls
11021125
11031126
1104- class PulpPublicationContext (PulpEntityContext ):
1127+ class PulpPublicationContext (PulpMasterContext ):
11051128 """Base class for publication contexts."""
11061129
11071130 ENTITY = _ ("publication" )
11081131 ENTITIES = _ ("publications" )
11091132 ID_PREFIX = "publications"
11101133 HREF_PATTERN = r"publications/(?P<plugin>[\w\-_]+)/(?P<resource_type>[\w\-_]+)/"
1111- TYPE_REGISTRY : t .Dict [str , t .Type ["PulpPublicationContext" ]] = {}
1112-
1113- def __init_subclass__ (cls , ** kwargs : t .Any ) -> None :
1114- super ().__init_subclass__ (** kwargs )
1115- if hasattr (cls , "PLUGIN" ) and hasattr (cls , "RESOURCE_TYPE" ):
1116- cls .TYPE_REGISTRY [f"{ cls .PLUGIN } :{ cls .RESOURCE_TYPE } " ] = cls
11171134
11181135 def list (self , limit : int , offset : int , parameters : t .Dict [str , t .Any ]) -> t .List [t .Any ]:
11191136 if parameters .get ("repository" ) is not None :
@@ -1123,20 +1140,14 @@ def list(self, limit: int, offset: int, parameters: t.Dict[str, t.Any]) -> t.Lis
11231140 return super ().list (limit , offset , parameters )
11241141
11251142
1126- class PulpDistributionContext (PulpEntityContext ):
1143+ class PulpDistributionContext (PulpMasterContext ):
11271144 """Base class for distribution contexts."""
11281145
11291146 ENTITY = _ ("distribution" )
11301147 ENTITIES = _ ("distributions" )
11311148 ID_PREFIX = "distributions"
11321149 HREF_PATTERN = r"distributions/(?P<plugin>[\w\-_]+)/(?P<resource_type>[\w\-_]+)/"
11331150 NULLABLES = {"content_guard" , "publication" , "remote" , "repository" , "repository_version" }
1134- TYPE_REGISTRY : t .Dict [str , t .Type ["PulpDistributionContext" ]] = {}
1135-
1136- def __init_subclass__ (cls , ** kwargs : t .Any ) -> None :
1137- super ().__init_subclass__ (** kwargs )
1138- if hasattr (cls , "PLUGIN" ) and hasattr (cls , "RESOURCE_TYPE" ):
1139- cls .TYPE_REGISTRY [f"{ cls .PLUGIN } :{ cls .RESOURCE_TYPE } " ] = cls
11401151
11411152
11421153class PulpRepositoryVersionContext (PulpEntityContext ):
@@ -1198,7 +1209,7 @@ def repair(self, href: t.Optional[str] = None) -> t.Any:
11981209 return self .call ("repair" , parameters = {self .HREF : href or self .pulp_href }, body = {})
11991210
12001211
1201- class PulpRepositoryContext (PulpEntityContext ):
1212+ class PulpRepositoryContext (PulpMasterContext ):
12021213 """Base class for repository contexts."""
12031214
12041215 ENTITY = _ ("repository" )
@@ -1207,12 +1218,6 @@ class PulpRepositoryContext(PulpEntityContext):
12071218 ID_PREFIX = "repositories"
12081219 VERSION_CONTEXT : t .ClassVar [t .Type [PulpRepositoryVersionContext ]] = PulpRepositoryVersionContext
12091220 NULLABLES = {"description" , "retain_repo_versions" }
1210- TYPE_REGISTRY : t .Dict [str , t .Type ["PulpRepositoryContext" ]] = {}
1211-
1212- def __init_subclass__ (cls , ** kwargs : t .Any ) -> None :
1213- super ().__init_subclass__ (** kwargs )
1214- if hasattr (cls , "PLUGIN" ) and hasattr (cls , "RESOURCE_TYPE" ):
1215- cls .TYPE_REGISTRY [f"{ cls .PLUGIN } :{ cls .RESOURCE_TYPE } " ] = cls
12161221
12171222 def get_version_context (
12181223 self ,
@@ -1324,18 +1329,13 @@ def reclaim(
13241329 return self .call ("reclaim_space_reclaim" , body = body )
13251330
13261331
1327- class PulpContentContext (PulpEntityContext ):
1332+ class PulpContentContext (PulpMasterContext ):
13281333 """Base class for content contexts."""
13291334
13301335 ENTITY = _ ("content" )
13311336 ENTITIES = _ ("content" )
1337+ HREF_PATTERN = r"content/(?P<plugin>[\w\-_]+)/(?P<resource_type>[\w\-_]+)/"
13321338 ID_PREFIX = "content"
1333- TYPE_REGISTRY : t .Dict [str , t .Type ["PulpContentContext" ]] = {}
1334-
1335- def __init_subclass__ (cls , ** kwargs : t .Any ) -> None :
1336- super ().__init_subclass__ (** kwargs )
1337- if hasattr (cls , "PLUGIN" ) and hasattr (cls , "RESOURCE_TYPE" ):
1338- cls .TYPE_REGISTRY [f"{ cls .PLUGIN } :{ cls .RESOURCE_TYPE } " ] = cls
13391339
13401340 def upload (
13411341 self ,
@@ -1376,38 +1376,26 @@ def upload(
13761376 return self .create (body = body )
13771377
13781378
1379- class PulpACSContext (PulpEntityContext ):
1379+ class PulpACSContext (PulpMasterContext ):
13801380 """Base class for ACS contexts."""
13811381
13821382 ENTITY = _ ("ACS" )
13831383 ENTITIES = _ ("ACSes" )
13841384 HREF_PATTERN = r"acs/(?P<plugin>[\w\-_]+)/(?P<resource_type>[\w\-_]+)/"
13851385 ID_PREFIX = "acs"
1386- TYPE_REGISTRY : t .Dict [str , t .Type ["PulpACSContext" ]] = {}
1387-
1388- def __init_subclass__ (cls , ** kwargs : t .Any ) -> None :
1389- super ().__init_subclass__ (** kwargs )
1390- if hasattr (cls , "PLUGIN" ) and hasattr (cls , "RESOURCE_TYPE" ):
1391- cls .TYPE_REGISTRY [f"{ cls .PLUGIN } :{ cls .RESOURCE_TYPE } " ] = cls
13921386
13931387 def refresh (self , href : t .Optional [str ] = None ) -> t .Any :
13941388 return self .call ("refresh" , parameters = {self .HREF : href or self .pulp_href })
13951389
13961390
1397- class PulpContentGuardContext (PulpEntityContext ):
1391+ class PulpContentGuardContext (PulpMasterContext ):
13981392 """Base class for content guard contexts."""
13991393
14001394 ENTITY = "content guard"
14011395 ENTITIES = "content guards"
14021396 ID_PREFIX = "contentguards"
14031397 HREF_PATTERN = r"contentguards/(?P<plugin>[\w\-_]+)/(?P<resource_type>[\w\-_]+)/"
14041398 NULLABLES = {"description" }
1405- TYPE_REGISTRY : t .Dict [str , t .Type ["PulpContentGuardContext" ]] = {}
1406-
1407- def __init_subclass__ (cls , ** kwargs : t .Any ) -> None :
1408- super ().__init_subclass__ (** kwargs )
1409- if hasattr (cls , "PLUGIN" ) and hasattr (cls , "RESOURCE_TYPE" ):
1410- cls .TYPE_REGISTRY [f"{ cls .PLUGIN } :{ cls .RESOURCE_TYPE } " ] = cls
14111399
14121400
14131401EntityFieldDefinition = t .Union [None , str , PulpEntityContext ]
0 commit comments