@@ -1052,7 +1052,36 @@ def needs_capability(self, capability: str) -> None:
10521052 )
10531053
10541054
1055- class PulpRemoteContext (PulpEntityContext ):
1055+ class PulpMasterContext (PulpEntityContext ):
1056+ TYPE_REGISTRY : t .Final [t .ClassVar [t .Dict [str , t .Type ["t.Self" ]]]]
1057+
1058+ def __init_subclass__ (cls , ** kwargs : t .Any ) -> None :
1059+ super ().__init_subclass__ (** kwargs )
1060+ if not hasattr (cls , "RESOURCE_TYPE" ):
1061+ cls .TYPE_REGISTRY = {}
1062+ elif hasattr (cls , "PLUGIN" ):
1063+ cls .TYPE_REGISTRY [f"{ cls .PLUGIN } :{ cls .RESOURCE_TYPE } " ] = cls
1064+
1065+ def detail_context (self , pulp_href : str ) -> "t.Self" :
1066+ """
1067+ Provide a detail context for a matching href.
1068+ """
1069+ m = re .search (self .HREF_PATTERN , pulp_href )
1070+ if m is None :
1071+ raise PulpException (f"'{ pulp_href } ' is not an href for { self .ENTITY } ." )
1072+ plugin = m .group ("plugin" )
1073+ resource_type = m .group ("resource_type" )
1074+ try :
1075+ detail_class = self .TYPE_REGISTRY [f"{ plugin } :{ resource_type } " ]
1076+ except KeyError :
1077+ raise PulpException (
1078+ f"{ self .ENTITY } with plugin '{ plugin } ' and"
1079+ f"resource type '{ resource_type } ' is unknown."
1080+ )
1081+ return detail_class (self .pulp_ctx , pulp_href = pulp_href )
1082+
1083+
1084+ class PulpRemoteContext (PulpMasterContext ):
10561085 """
10571086 Base class for remote contexts.
10581087 """
@@ -1078,27 +1107,15 @@ class PulpRemoteContext(PulpEntityContext):
10781107 "sock_read_timeout" ,
10791108 "rate_limit" ,
10801109 }
1081- TYPE_REGISTRY : t .Final [t .Dict [str , t .Type ["PulpRemoteContext" ]]] = {}
1082-
1083- def __init_subclass__ (cls , ** kwargs : t .Any ) -> None :
1084- super ().__init_subclass__ (** kwargs )
1085- if hasattr (cls , "PLUGIN" ) and hasattr (cls , "RESOURCE_TYPE" ):
1086- cls .TYPE_REGISTRY [f"{ cls .PLUGIN } :{ cls .RESOURCE_TYPE } " ] = cls
10871110
10881111
1089- class PulpPublicationContext (PulpEntityContext ):
1112+ class PulpPublicationContext (PulpMasterContext ):
10901113 """Base class for publication contexts."""
10911114
10921115 ENTITY = _ ("publication" )
10931116 ENTITIES = _ ("publications" )
10941117 ID_PREFIX = "publications"
10951118 HREF_PATTERN = r"publications/(?P<plugin>[\w\-_]+)/(?P<resource_type>[\w\-_]+)/"
1096- TYPE_REGISTRY : t .Final [t .Dict [str , t .Type ["PulpPublicationContext" ]]] = {}
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
11021119
11031120 def list (self , limit : int , offset : int , parameters : t .Dict [str , t .Any ]) -> t .List [t .Any ]:
11041121 if parameters .get ("repository" ) is not None :
@@ -1108,20 +1125,14 @@ def list(self, limit: int, offset: int, parameters: t.Dict[str, t.Any]) -> t.Lis
11081125 return super ().list (limit , offset , parameters )
11091126
11101127
1111- class PulpDistributionContext (PulpEntityContext ):
1128+ class PulpDistributionContext (PulpMasterContext ):
11121129 """Base class for distribution contexts."""
11131130
11141131 ENTITY = _ ("distribution" )
11151132 ENTITIES = _ ("distributions" )
11161133 ID_PREFIX = "distributions"
11171134 HREF_PATTERN = r"distributions/(?P<plugin>[\w\-_]+)/(?P<resource_type>[\w\-_]+)/"
11181135 NULLABLES = {"content_guard" , "publication" , "remote" , "repository" , "repository_version" }
1119- TYPE_REGISTRY : t .Final [t .Dict [str , t .Type ["PulpDistributionContext" ]]] = {}
1120-
1121- def __init_subclass__ (cls , ** kwargs : t .Any ) -> None :
1122- super ().__init_subclass__ (** kwargs )
1123- if hasattr (cls , "PLUGIN" ) and hasattr (cls , "RESOURCE_TYPE" ):
1124- cls .TYPE_REGISTRY [f"{ cls .PLUGIN } :{ cls .RESOURCE_TYPE } " ] = cls
11251136
11261137
11271138class PulpRepositoryVersionContext (PulpEntityContext ):
@@ -1180,7 +1191,7 @@ def repair(self) -> t.Any:
11801191 return self .call ("repair" , parameters = {self .HREF : self .pulp_href }, body = {})
11811192
11821193
1183- class PulpRepositoryContext (PulpEntityContext ):
1194+ class PulpRepositoryContext (PulpMasterContext ):
11841195 """Base class for repository contexts."""
11851196
11861197 ENTITY = _ ("repository" )
@@ -1189,12 +1200,6 @@ class PulpRepositoryContext(PulpEntityContext):
11891200 ID_PREFIX = "repositories"
11901201 VERSION_CONTEXT : t .ClassVar [t .Type [PulpRepositoryVersionContext ]] = PulpRepositoryVersionContext
11911202 NULLABLES = {"description" , "retain_repo_versions" }
1192- TYPE_REGISTRY : t .Final [t .Dict [str , t .Type ["PulpRepositoryContext" ]]] = {}
1193-
1194- def __init_subclass__ (cls , ** kwargs : t .Any ) -> None :
1195- super ().__init_subclass__ (** kwargs )
1196- if hasattr (cls , "PLUGIN" ) and hasattr (cls , "RESOURCE_TYPE" ):
1197- cls .TYPE_REGISTRY [f"{ cls .PLUGIN } :{ cls .RESOURCE_TYPE } " ] = cls
11981203
11991204 def get_version_context (
12001205 self ,
@@ -1301,18 +1306,13 @@ def reclaim(
13011306 return self .call ("reclaim_space_reclaim" , body = body )
13021307
13031308
1304- class PulpContentContext (PulpEntityContext ):
1309+ class PulpContentContext (PulpMasterContext ):
13051310 """Base class for content contexts."""
13061311
13071312 ENTITY = _ ("content" )
13081313 ENTITIES = _ ("content" )
1314+ HREF_PATTERN = r"content/(?P<plugin>[\w\-_]+)/(?P<resource_type>[\w\-_]+)/"
13091315 ID_PREFIX = "content"
1310- TYPE_REGISTRY : t .Final [t .Dict [str , t .Type ["PulpContentContext" ]]] = {}
1311-
1312- def __init_subclass__ (cls , ** kwargs : t .Any ) -> None :
1313- super ().__init_subclass__ (** kwargs )
1314- if hasattr (cls , "PLUGIN" ) and hasattr (cls , "RESOURCE_TYPE" ):
1315- cls .TYPE_REGISTRY [f"{ cls .PLUGIN } :{ cls .RESOURCE_TYPE } " ] = cls
13161316
13171317 def upload (
13181318 self ,
@@ -1353,38 +1353,26 @@ def upload(
13531353 return self .create (body = body )
13541354
13551355
1356- class PulpACSContext (PulpEntityContext ):
1356+ class PulpACSContext (PulpMasterContext ):
13571357 """Base class for ACS contexts."""
13581358
13591359 ENTITY = _ ("ACS" )
13601360 ENTITIES = _ ("ACSes" )
13611361 HREF_PATTERN = r"acs/(?P<plugin>[\w\-_]+)/(?P<resource_type>[\w\-_]+)/"
13621362 ID_PREFIX = "acs"
1363- TYPE_REGISTRY : t .Final [t .Dict [str , t .Type ["PulpACSContext" ]]] = {}
1364-
1365- def __init_subclass__ (cls , ** kwargs : t .Any ) -> None :
1366- super ().__init_subclass__ (** kwargs )
1367- if hasattr (cls , "PLUGIN" ) and hasattr (cls , "RESOURCE_TYPE" ):
1368- cls .TYPE_REGISTRY [f"{ cls .PLUGIN } :{ cls .RESOURCE_TYPE } " ] = cls
13691363
13701364 def refresh (self , href : t .Optional [str ] = None ) -> t .Any :
13711365 return self .call ("refresh" , parameters = {self .HREF : href or self .pulp_href })
13721366
13731367
1374- class PulpContentGuardContext (PulpEntityContext ):
1368+ class PulpContentGuardContext (PulpMasterContext ):
13751369 """Base class for content guard contexts."""
13761370
13771371 ENTITY = "content guard"
13781372 ENTITIES = "content guards"
13791373 ID_PREFIX = "contentguards"
13801374 HREF_PATTERN = r"contentguards/(?P<plugin>[\w\-_]+)/(?P<resource_type>[\w\-_]+)/"
13811375 NULLABLES = {"description" }
1382- TYPE_REGISTRY : t .Final [t .Dict [str , t .Type ["PulpContentGuardContext" ]]] = {}
1383-
1384- def __init_subclass__ (cls , ** kwargs : t .Any ) -> None :
1385- super ().__init_subclass__ (** kwargs )
1386- if hasattr (cls , "PLUGIN" ) and hasattr (cls , "RESOURCE_TYPE" ):
1387- cls .TYPE_REGISTRY [f"{ cls .PLUGIN } :{ cls .RESOURCE_TYPE } " ] = cls
13881376
13891377
13901378EntityFieldDefinition = t .Union [None , str , PulpEntityContext ]
0 commit comments