@@ -150,6 +150,19 @@ def resolve_duplicate():
150150 return "i"
151151 return None
152152
153+ def resolve_policy_reject ():
154+ print ("Your options are" )
155+ print (" force entry write to the server (F)" )
156+ print (" ignore, do not apply any changes (I)" )
157+ print (" abort without changes (A)" )
158+ while True :
159+ action = input ("Your choice [f/I/a]: " ).lower ()
160+ if action == "f" or action == "i" or action == "a" :
161+ return action
162+ if not action or action == "" :
163+ return "i"
164+ return None
165+
153166
154167def update_local_bib (key , new_entry ):
155168 for (idx , entry ) in enumerate (bib_database .entries ):
@@ -163,8 +176,12 @@ def update_remote_bib(key, new_entry):
163176 if "success" in response .json () and not response .json ()["success" ]:
164177 show_error (response .json ())
165178
166- def add_remote_bib (key , entry ):
167- response = requests .post (server + "entry/%s" % key , json = {"entry" : entry , "token" : token })
179+ def add_remote_bib (key , entry , force = False ):
180+ if force :
181+ # do not rely on boolean encoding of `force`
182+ response = requests .post (server + "entry/%s" % key , json = {"entry" : entry , "token" : token , "force" : "true" })
183+ else :
184+ response = requests .post (server + "entry/%s" % key , json = {"entry" : entry , "token" : token })
168185 if "success" in response .json () and not response .json ()["success" ]:
169186 show_error (response .json ())
170187
@@ -275,7 +292,18 @@ def show_error(obj):
275292 response = requests .post (server + "update" , json = {"entries" : bib_database .entries , "token" : token })
276293 result = response .json ()
277294 if not result ["success" ]:
278- if result ["reason" ] == "duplicate" :
295+ if result ["reason" ] == "policy" :
296+ #print(result["entries"])
297+ for entry in result ["entries" ]:
298+ print ("\n [!] Server policy rejected entry %s. Reason: %s" % (entry ["ID" ], entry ["reason" ]))
299+ action = resolve_policy_reject ()
300+ if action == "i" :
301+ pass
302+ elif action == "a" :
303+ sys .exit (1 )
304+ elif action == "f" :
305+ add_remote_bib (entry ["ID" ], entry_by_key (entry ["ID" ]), force = True )
306+ elif result ["reason" ] == "duplicate" :
279307 #print(result["entries"])
280308 for dup in result ["entries" ]:
281309 print ("\n [!] There is already a similar entry for %s on the server (%s) [Levenshtein %d]" % (dup [1 ], dup [2 ]["ID" ], dup [0 ]))
0 commit comments