99import sys
1010
1111# Import schema.org libraries
12- if not os .getcwd () in sys .path :
12+ if os .getcwd () not in sys .path :
1313 sys .path .insert (1 , os .getcwd ())
1414
1515import software .scripts .buildtermlist as buildtermlist
@@ -134,15 +134,16 @@ def buildTermCatList(terms, checkCat=False):
134134 return termcat , termcount
135135
136136
137- VISITLIST = []
137+ class UnknownTermError (LookupError ):
138+ """Raised when there is no definition for a given term."""
138139
139140
140141class listingNode :
141- def __init__ (self , term , depth = 0 , title = "" , parent = None ):
142- global VISITLIST
142+ def __init__ (self , term , depth = 0 , title = "" , parent = None , visit_set = None ):
143143 termdesc = sdotermsource .SdoTermSource .getTerm (term )
144- if parent is None :
145- VISITLIST = []
144+ if not termdesc :
145+ raise UnknownTermError (f"No description for term { term } " )
146+ visit_set = visit_set or set ()
146147 self .repeat = False
147148 self .subs = []
148149 self .parent = parent
@@ -152,22 +153,36 @@ def __init__(self, term, depth=0, title="", parent=None):
152153 self .depth = depth
153154 self .retired = termdesc .retired
154155 self .pending = termdesc .pending
155- if self .id not in VISITLIST :
156- VISITLIST . append (self .id )
156+ if self .id not in visit_set :
157+ visit_set . add (self .id )
157158 if termdesc .termType == sdoterm .SdoTermType .ENUMERATION :
158159 for enum in sorted (termdesc .enumerationMembers .ids ):
159- self .subs .append (listingNode (enum , depth = depth + 1 , parent = self ))
160+ try :
161+ self .subs .append (
162+ listingNode (
163+ enum , depth = depth + 1 , parent = self , visit_set = visit_set
164+ )
165+ )
166+ except UnknownTermError as e :
167+ log .warning (
168+ f"Error while building enumeration node { enum } for { term } : { e } "
169+ )
160170 for sub in sorted (termdesc .subs .ids ):
161- self .subs .append (listingNode (sub , depth = depth + 1 , parent = self ))
162-
171+ try :
172+ self .subs .append (
173+ listingNode (
174+ sub , depth = depth + 1 , parent = self , visit_set = visit_set
175+ )
176+ )
177+ except UnknownTermError as e :
178+ log .warning (
179+ f"Error while building child node { sub } for { term } : { e } "
180+ )
163181 else : # Visited this node before so don't parse children
164182 self .repeat = True
165183
166184
167- def jsonldtree (page ):
168- global VISITLIST
169- VISITLIST = []
170-
185+ def jsonldtree (page ) -> str :
171186 term = {}
172187 context = {}
173188 context ["rdfs" ] = "http://www.w3.org/2000/01/rdf-schema#"
@@ -176,38 +191,49 @@ def jsonldtree(page):
176191 context ["description" ] = "rdfs:comment"
177192 context ["children" ] = {"@reverse" : "rdfs:subClassOf" }
178193 term ["@context" ] = context
179- data = _jsonldtree ("Thing" , term )
180- return json .dumps (data , indent = 3 )
181-
182-
183- def _jsonldtree (tid : str , term = None ):
184- termdesc = sdotermsource .SdoTermSource .getTerm (tid )
185- if not term :
186- term = {}
187- term ["@type" ] = "rdfs:Class"
188- term ["@id" ] = "schema:" + termdesc .id
189- term ["name" ] = termdesc .label
190- if termdesc .supers :
191- sups = ["schema:" + sup for sup in termdesc .supers .ids ]
192- if len (sups ) == 1 :
193- term ["rdfs:subClassOf" ] = sups [0 ]
194- else :
195- term ["rdfs:subClassOf" ] = sups
196- term ["description" ] = textutils .ShortenOnSentence (
197- textutils .StripHtmlTags (termdesc .comment )
198- )
199- if termdesc .pending :
200- term ["pending" ] = True
201- if termdesc .retired :
202- term ["attic" ] = True
203- if tid not in VISITLIST :
204- VISITLIST .append (tid )
205- if termdesc .subs :
206- subs = []
207- for sub in termdesc .subs .ids :
208- subs .append (_jsonldtree (tid = sub ))
209- term ["children" ] = subs
210- return term
194+ data = _jsonldtree ("Thing" , visitset = set (), term = term )
195+ return json .dumps (data , indent = 2 )
196+
197+
198+ def _jsonldtree (tid : str , visitset : set , term : dict | None = None ) -> dict :
199+ try :
200+ termdesc = sdotermsource .SdoTermSource .getTerm (tid )
201+ if not termdesc :
202+ raise UnknownTermError (f"No description for term { term } " )
203+ if not term :
204+ term = {}
205+ term ["@type" ] = "rdfs:Class"
206+ term ["@id" ] = "schema:" + termdesc .id
207+ term ["name" ] = termdesc .label
208+ if termdesc .supers :
209+ sups = ["schema:" + sup for sup in termdesc .supers .ids ]
210+ if len (sups ) == 1 :
211+ term ["rdfs:subClassOf" ] = sups [0 ]
212+ else :
213+ term ["rdfs:subClassOf" ] = sups
214+ term ["description" ] = textutils .ShortenOnSentence (
215+ textutils .StripHtmlTags (termdesc .comment )
216+ )
217+ if termdesc .pending :
218+ term ["pending" ] = True
219+ if termdesc .retired :
220+ term ["attic" ] = True
221+ if tid not in visitset :
222+ visitset .add (tid )
223+ if termdesc .subs :
224+ subs = []
225+ for sub in termdesc .subs .ids :
226+ try :
227+ subs .append (_jsonldtree (tid = sub , visitset = visitset ))
228+ except UnknownTermError as e :
229+ log .warning (
230+ f"Error while building listing node for { term } : { e } "
231+ )
232+ term ["children" ] = subs
233+ return term
234+ except Exception as e :
235+ e .add_note (f"while building JSON tree for id:{ tid } -{ term } " )
236+ raise
211237
212238
213239listings = None
0 commit comments