@@ -273,7 +273,7 @@ def resolve_json_id(
273
273
except KeyError :
274
274
raise UnresolvedIdError ("cannot resolve id: {}" .format (json_id ))
275
275
276
- def import_directory (self , datadir : str ) -> typing .Dict [str , typing .Dict ]:
276
+ def import_directory (self , datadir : str , allow_duplicates = False ) -> typing .Dict [str , typing .Dict ]:
277
277
"""import a JSON directory into the database"""
278
278
279
279
def json_stream () -> typing .Iterator [_JsonDict ]:
@@ -282,7 +282,7 @@ def json_stream() -> typing.Iterator[_JsonDict]:
282
282
with open (fname ) as f :
283
283
yield json .load (f )
284
284
285
- return self .import_data (json_stream ())
285
+ return self .import_data (json_stream (), allow_duplicates )
286
286
287
287
def _prepare_imports (
288
288
self , dicts : typing .Iterable [_JsonDict ]
@@ -309,7 +309,7 @@ def _prepare_imports(
309
309
self .duplicates [json_id ] = seen_hashes [objhash ]
310
310
311
311
def import_data (
312
- self , data_items : typing .Iterable [_JsonDict ]
312
+ self , data_items : typing .Iterable [_JsonDict ], allow_duplicates = False
313
313
) -> typing .Dict [str , typing .Dict ]:
314
314
"""import a bunch of dicts together"""
315
315
# keep counts of all actions
@@ -322,7 +322,7 @@ def import_data(
322
322
}
323
323
324
324
for json_id , data in self ._prepare_imports (data_items ):
325
- obj_id , what = self .import_item (data )
325
+ obj_id , what = self .import_item (data , allow_duplicates )
326
326
if not obj_id or not what :
327
327
"Skipping data because it did not have an associated ID or type"
328
328
continue
@@ -341,7 +341,7 @@ def import_data(
341
341
342
342
return {self ._type : record }
343
343
344
- def import_item (self , data : _JsonDict ) -> typing .Tuple [_ID , str ]:
344
+ def import_item (self , data : _JsonDict , allow_duplicates = False ) -> typing .Tuple [_ID , str ]:
345
345
"""function used by import_data"""
346
346
what = "noop"
347
347
@@ -369,8 +369,12 @@ def import_item(self, data: _JsonDict) -> typing.Tuple[_ID, str]:
369
369
370
370
# obj existed, check if we need to do an update
371
371
if obj :
372
- if obj .id in self .json_to_db_id .values ():
372
+ # If --allow_duplicates flag is set on client CLI command
373
+ # then we ignore duplicates instead of raising an exception
374
+ if not allow_duplicates and obj .id in self .json_to_db_id .values ():
373
375
raise DuplicateItemError (data , obj , related .get ("sources" , []))
376
+ elif allow_duplicates and obj .id in self .json_to_db_id .values ():
377
+ self .logger .warning (f"Ignored a DuplicateItemError for { obj .id } " )
374
378
# check base object for changes
375
379
for key , value in data .items ():
376
380
if getattr (obj , key ) != value :
0 commit comments