1
1
from hmac import new
2
- from math import log
2
+ from math import e , log
3
3
from typing import Iterable
4
4
import flask
5
5
from requests import get
6
6
7
7
from backend .common .backend_exceptions import require_permissions
8
8
9
9
from backend .common import connect , database
10
- from backend .endpoints .linked_documents import LinkType , db_id_to_path , get_linked_documents , make_document , path_to_db_id
10
+ from backend .endpoints .linked_documents import (
11
+ LinkType ,
12
+ db_id_to_path ,
13
+ get_linked_documents ,
14
+ make_document ,
15
+ path_to_db_id ,
16
+ )
11
17
from onshape_api .api .api_base import Api
12
18
from onshape_api .endpoints .permissions import Permission
13
19
@@ -163,47 +169,59 @@ def get_linked_parents(db, instance):
163
169
164
170
return linked_parents
165
171
166
- unvisited_nodes = [curr_instance ]
172
+ unvisited_nodes = []
167
173
168
174
sorted_list = []
169
175
170
- while unvisited_nodes :
171
- curr_node = unvisited_nodes .pop (0 )
172
- sorted_list .append (curr_node )
173
- curr_node_parents = get_linked_parents (db , curr_node )
176
+ route = []
177
+ route .append (curr_instance )
174
178
175
- for parent in curr_node_parents :
176
- if parent not in unvisited_nodes :
177
- unvisited_nodes .append (parent )
178
-
179
- trimmed_list = [] #remove duplicates
179
+ curr_parents = []
180
180
181
- for instance in reversed ( sorted_list ) :
182
- if instance not in trimmed_list :
183
- trimmed_list . insert ( 0 , instance )
181
+ while route :
182
+ print ( f"Route: { route } " )
183
+ print ( f"Unvisited Nodes: { unvisited_nodes } " )
184
184
185
- with open ("backend/endpoints/logfile.txt" , "a" ) as log_file :
185
+ curr_parents = get_linked_parents (db , route [- 1 ])
186
+
187
+ for parent in sorted_list :
188
+ if parent in curr_parents :
189
+ curr_parents .remove (parent )
190
+
191
+ if not curr_parents :
192
+ sorted_list .append (route .pop ())
193
+
194
+ for parent in curr_parents :
195
+ if parent in unvisited_nodes :
196
+ unvisited_nodes .remove (parent )
186
197
187
- log_file .write ("curr_instance begin\n " )
188
- log_file .write (f"{ curr_instance } \n " )
189
- log_file .write ("curr_instance end\n \n " )
198
+ unvisited_nodes .extend (curr_parents )
190
199
191
- log_file .write ("trimmed_list begin\n " )
192
- for node in trimmed_list :
193
- log_file .write (f"{ documents .get_document (api , node )["name" ]} \n " )
194
- log_file .write ("trimmed_list end\n \n " )
200
+ if curr_parents :
201
+ if unvisited_nodes :
202
+ if unvisited_nodes [- 1 ] in route :
203
+ raise Exception ("Cycle detected" )
204
+ route .append (unvisited_nodes .pop ())
195
205
206
+ sorted_list .reverse ()
196
207
197
- for instance in trimmed_list :
198
- require_permissions (api , instance , Permission .WRITE , Permission .LINK )
208
+ with open ("backend/endpoints/logfile.txt" , "a" ) as log_file :
209
+
210
+ log_file .write ("sorted_list begin\n " )
211
+ for node in sorted_list :
212
+ log_file .write (f"{ documents .get_document (api , node )['name' ]} \n " )
213
+ log_file .write ("sorted_list end\n \n " )
214
+
215
+ for instance in sorted_list :
216
+ require_permissions (api , instance , Permission .WRITE , Permission .LINK )
199
217
200
218
versions .create_version (api , curr_instance , name , description )
201
219
202
220
updated_references = 0
203
- for update_instance in trimmed_list :
221
+ for update_instance in sorted_list :
204
222
205
223
updated_references += do_update_references (
206
- api , update_instance , [doc .document_id for doc in trimmed_list ]
224
+ api , update_instance , [doc .document_id for doc in sorted_list ]
207
225
)
208
226
versions .create_version (api , update_instance , name , description )
209
227
@@ -213,4 +231,4 @@ def get_linked_parents(db, instance):
213
231
log_file .write (f"{ updated_references } \n " )
214
232
log_file .write ("updated_references end\n \n " )
215
233
216
- return {"updatedReferences" : updated_references }
234
+ return {"updatedReferences" : updated_references }
0 commit comments