@@ -117,7 +117,7 @@ def _create_record_iterator(self) -> Iterator[RDSModel]:
117
117
118
118
119
119
# TODO: this should inherit from ProgrammaticDescription in amundsen-common
120
- class DescriptionMetadata :
120
+ class DescriptionMetadata ( GraphSerializable ) :
121
121
DESCRIPTION_NODE_LABEL = DESCRIPTION_NODE_LABEL_VAL
122
122
PROGRAMMATIC_DESCRIPTION_NODE_LABEL = 'Programmatic_Description'
123
123
DESCRIPTION_KEY_FORMAT = '{description}'
@@ -132,7 +132,10 @@ class DescriptionMetadata:
132
132
133
133
def __init__ (self ,
134
134
text : Optional [str ],
135
- source : str = DEFAULT_SOURCE
135
+ source : str = DEFAULT_SOURCE ,
136
+ description_key : Optional [str ] = None ,
137
+ start_label : Optional [str ] = None , # Table, Column, Schema
138
+ start_key : Optional [str ] = None ,
136
139
):
137
140
"""
138
141
:param source: The unique source of what is populating this description.
@@ -146,17 +149,28 @@ def __init__(self,
146
149
else :
147
150
self .label = self .PROGRAMMATIC_DESCRIPTION_NODE_LABEL
148
151
152
+ self .start_label = start_label
153
+ self .start_key = start_key
154
+ self .description_key = description_key or self .get_description_default_key (start_key )
155
+
156
+ self ._node_iter = self ._create_node_iterator ()
157
+ self ._relation_iter = self ._create_relation_iterator ()
158
+
149
159
@staticmethod
150
160
def create_description_metadata (text : Union [None , str ],
151
- source : Optional [str ] = DEFAULT_SOURCE
161
+ source : Optional [str ] = DEFAULT_SOURCE ,
162
+ description_key : Optional [str ] = None ,
163
+ start_label : Optional [str ] = None , # Table, Column, Schema
164
+ start_key : Optional [str ] = None ,
152
165
) -> Optional ['DescriptionMetadata' ]:
153
166
# We do not want to create a node if there is no description text!
154
167
if text is None :
155
168
return None
156
- if not source :
157
- description_node = DescriptionMetadata (text = text , source = DescriptionMetadata .DEFAULT_SOURCE )
158
- else :
159
- description_node = DescriptionMetadata (text = text , source = source )
169
+ description_node = DescriptionMetadata (text = text ,
170
+ source = source or DescriptionMetadata .DEFAULT_SOURCE ,
171
+ description_key = description_key ,
172
+ start_label = start_label ,
173
+ start_key = start_key )
160
174
return description_node
161
175
162
176
def get_description_id (self ) -> str :
@@ -165,8 +179,8 @@ def get_description_id(self) -> str:
165
179
else :
166
180
return "_" + self .source + "_description"
167
181
168
- def __repr__ (self ) -> str :
169
- return f'DescriptionMetadata( { self . source !r } , { self .text !r } )'
182
+ def get_description_default_key (self , start_key : Optional [ str ] ) -> Optional [ str ] :
183
+ return f'{ start_key } / { self .get_description_id () } ' if start_key else None
170
184
171
185
def get_node (self , node_key : str ) -> GraphNode :
172
186
node = GraphNode (
@@ -179,7 +193,11 @@ def get_node(self, node_key: str) -> GraphNode:
179
193
)
180
194
return node
181
195
182
- def get_relation (self , start_node : str , start_key : Any , end_key : Any ) -> GraphRelationship :
196
+ def get_relation (self ,
197
+ start_node : str ,
198
+ start_key : str ,
199
+ end_key : str ,
200
+ ) -> GraphRelationship :
183
201
relationship = GraphRelationship (
184
202
start_label = start_node ,
185
203
start_key = start_key ,
@@ -191,6 +209,40 @@ def get_relation(self, start_node: str, start_key: Any, end_key: Any) -> GraphRe
191
209
)
192
210
return relationship
193
211
212
+ def create_next_node (self ) -> Optional [GraphNode ]:
213
+ # return the string representation of the data
214
+ try :
215
+ return next (self ._node_iter )
216
+ except StopIteration :
217
+ return None
218
+
219
+ def create_next_relation (self ) -> Optional [GraphRelationship ]:
220
+ try :
221
+ return next (self ._relation_iter )
222
+ except StopIteration :
223
+ return None
224
+
225
+ def _create_node_iterator (self ) -> Iterator [GraphNode ]:
226
+ if not self .description_key :
227
+ raise Exception ('Required description node key cannot be None' )
228
+ yield self .get_node (self .description_key )
229
+
230
+ def _create_relation_iterator (self ) -> Iterator [GraphRelationship ]:
231
+ if not self .start_label :
232
+ raise Exception ('Required relation start node label cannot be None' )
233
+ if not self .start_key :
234
+ raise Exception ('Required relation start key cannot be None' )
235
+ if not self .description_key :
236
+ raise Exception ('Required relation end key cannot be None' )
237
+ yield self .get_relation (
238
+ start_node = self .start_label ,
239
+ start_key = self .start_key ,
240
+ end_key = self .description_key
241
+ )
242
+
243
+ def __repr__ (self ) -> str :
244
+ return f'DescriptionMetadata({ self .source !r} , { self .text !r} )'
245
+
194
246
195
247
class ColumnMetadata :
196
248
COLUMN_NODE_LABEL = 'Column'
0 commit comments