@@ -145,6 +145,19 @@ def resolve_duplicate():
145145 return "i"
146146 return None
147147
148+ def resolve_policy_reject ():
149+ print ("Your options are" )
150+ print (" force entry write to the server (F)" )
151+ print (" ignore, do not apply any changes (I)" )
152+ print (" abort without changes (A)" )
153+ while True :
154+ action = input ("Your choice [f/I/a]: " ).lower ()
155+ if action == "f" or action == "i" or action == "a" :
156+ return action
157+ if not action or action == "" :
158+ return "i"
159+ return None
160+
148161
149162def update_local_bib (key , new_entry ):
150163 for (idx , entry ) in enumerate (bib_database .entries ):
@@ -158,8 +171,12 @@ def update_remote_bib(key, new_entry):
158171 if "success" in response .json () and not response .json ()["success" ]:
159172 show_error (response .json ())
160173
161- def add_remote_bib (key , entry ):
162- response = requests .post (server + "entry/%s" % key , json = {"entry" : entry , "token" : token })
174+ def add_remote_bib (key , entry , force = False ):
175+ if force :
176+ # do not rely on boolean encoding of `force`
177+ response = requests .post (server + "entry/%s" % key , json = {"entry" : entry , "token" : token , "force" : "true" })
178+ else :
179+ response = requests .post (server + "entry/%s" % key , json = {"entry" : entry , "token" : token })
163180 if "success" in response .json () and not response .json ()["success" ]:
164181 show_error (response .json ())
165182
@@ -270,7 +287,18 @@ def show_error(obj):
270287 response = requests .post (server + "update" , json = {"entries" : bib_database .entries , "token" : token })
271288 result = response .json ()
272289 if not result ["success" ]:
273- if result ["reason" ] == "duplicate" :
290+ if result ["reason" ] == "policy" :
291+ #print(result["entries"])
292+ for entry in result ["entries" ]:
293+ print ("\n [!] Server policy rejected entry %s. Reason: %s" % (entry ["ID" ], entry ["reason" ]))
294+ action = resolve_policy_reject ()
295+ if action == "i" :
296+ pass
297+ elif action == "a" :
298+ sys .exit (1 )
299+ elif action == "f" :
300+ add_remote_bib (entry ["ID" ], entry_by_key (entry ["ID" ]), force = True )
301+ elif result ["reason" ] == "duplicate" :
274302 #print(result["entries"])
275303 for dup in result ["entries" ]:
276304 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