11from __future__ import annotations
22
33import logging
4- import os
54from abc import abstractmethod
65from typing import TYPE_CHECKING
76
8- from infrahub_sdk .repository import GitRepoManager
9-
107from .exceptions import UninitializedError
8+ from .operation import InfrahubOperation
119
1210if TYPE_CHECKING :
1311 from .client import InfrahubClient
1412 from .context import RequestContext
1513 from .node import InfrahubNode
16- from .store import NodeStore
1714
1815
19- class InfrahubGenerator :
16+ class InfrahubGenerator ( InfrahubOperation ) :
2017 """Infrahub Generator class"""
2118
2219 def __init__ (
2320 self ,
2421 query : str ,
2522 client : InfrahubClient ,
2623 infrahub_node : type [InfrahubNode ],
27- branch : str | None = None ,
24+ branch : str = "" ,
2825 root_directory : str = "" ,
2926 generator_instance : str = "" ,
3027 params : dict | None = None ,
@@ -33,35 +30,21 @@ def __init__(
3330 request_context : RequestContext | None = None ,
3431 ) -> None :
3532 self .query = query
36- self .branch = branch
37- self .git : GitRepoManager | None = None
33+
34+ super ().__init__ (
35+ client = client ,
36+ infrahub_node = infrahub_node ,
37+ convert_query_response = convert_query_response ,
38+ branch = branch ,
39+ root_directory = root_directory ,
40+ )
41+
3842 self .params = params or {}
39- self .root_directory = root_directory or os .getcwd ()
4043 self .generator_instance = generator_instance
41- self ._init_client = client .clone (branch = self .branch_name )
4244 self ._client : InfrahubClient | None = None
43- self ._nodes : list [InfrahubNode ] = []
44- self ._related_nodes : list [InfrahubNode ] = []
45- self .infrahub_node = infrahub_node
46- self .convert_query_response = convert_query_response
4745 self .logger = logger if logger else logging .getLogger ("infrahub.tasks" )
4846 self .request_context = request_context
4947
50- @property
51- def store (self ) -> NodeStore :
52- """The store will be populated with nodes based on the query during the collection of data if activated"""
53- return self ._init_client .store
54-
55- @property
56- def nodes (self ) -> list [InfrahubNode ]:
57- """Returns nodes collected and parsed during the data collection process if this feature is enables"""
58- return self ._nodes
59-
60- @property
61- def related_nodes (self ) -> list [InfrahubNode ]:
62- """Returns nodes collected and parsed during the data collection process if this feature is enables"""
63- return self ._related_nodes
64-
6548 @property
6649 def subscribers (self ) -> list [str ] | None :
6750 if self .generator_instance :
@@ -78,20 +61,6 @@ def client(self) -> InfrahubClient:
7861 def client (self , value : InfrahubClient ) -> None :
7962 self ._client = value
8063
81- @property
82- def branch_name (self ) -> str :
83- """Return the name of the current git branch."""
84-
85- if self .branch :
86- return self .branch
87-
88- if not self .git :
89- self .git = GitRepoManager (self .root_directory )
90-
91- self .branch = str (self .git .active_branch )
92-
93- return self .branch
94-
9564 async def collect_data (self ) -> dict :
9665 """Query the result of the GraphQL Query defined in self.query and return the result"""
9766
@@ -117,27 +86,6 @@ async def run(self, identifier: str, data: dict | None = None) -> None:
11786 ) as self .client :
11887 await self .generate (data = unpacked )
11988
120- async def process_nodes (self , data : dict ) -> None :
121- if not self .convert_query_response :
122- return
123-
124- await self ._init_client .schema .all (branch = self .branch_name )
125-
126- for kind in data :
127- if kind in self ._init_client .schema .cache [self .branch_name ].nodes .keys ():
128- for result in data [kind ].get ("edges" , []):
129- node = await self .infrahub_node .from_graphql (
130- client = self ._init_client , branch = self .branch_name , data = result
131- )
132- self ._nodes .append (node )
133- await node ._process_relationships (
134- node_data = result , branch = self .branch_name , related_nodes = self ._related_nodes
135- )
136-
137- for node in self ._nodes + self ._related_nodes :
138- if node .id :
139- self ._init_client .store .set (node = node )
140-
14189 @abstractmethod
14290 async def generate (self , data : dict ) -> None :
14391 """Code to run the generator
0 commit comments