3030#
3131# =================================================================
3232
33+ import hashlib
3334import json
3435import logging
3536import os
4950LOGGER = logging .getLogger (__name__ )
5051
5152
52- def load_records (context , database , table , xml_dirpath , recursive = False , force_update = False ):
53+ def load_records (context , database , table , xml_dirpath , recursive = False , hashidentifier = 'NEVER' , source = 'local' , force_update = False ):
5354 """Load metadata records from directory of files to database"""
5455
5556 repo = repository .Repository (database , context , table = table )
@@ -99,9 +100,16 @@ def load_records(context, database, table, xml_dirpath, recursive=False, force_u
99100 LOGGER .info ('Inserting %s %s into database %s, table %s ....' ,
100101 rec .typename , rec .identifier , database , table )
101102
103+ # hash identifier to prevent 404 when using it as parameter
104+ if hashidentifier .upper () == 'ALWAYS' or (hashidentifier .upper () == 'DOUBLESLASH' and rec .identifier .contains ('//' )): #noqa
105+ rec .identifier = hashlib .md5 (rec .identifier .encode ()).hexdigest ()
106+
102107 # TODO: do this as CSW Harvest
103108 try :
104- repo .insert (rec , 'local' , util .get_today_and_now ())
109+
110+ if source != 'local' :
111+ rec .mdsource = source
112+ repo .insert (rec , source , util .get_today_and_now ())
105113 loaded_files .add (recfile )
106114 LOGGER .info ('Inserted %s' , recfile )
107115 except Exception as err :
@@ -417,9 +425,13 @@ def cli_setup_repository(ctx, config, verbosity):
417425 help = 'File or directory path to metadata records' ,
418426 type = click .Path (exists = True , resolve_path = True , file_okay = True ))
419427@click .option ('--recursive' , '-r' , is_flag = True ,
420- default = False , help = 'Bypass confirmation' )
428+ default = False , help = 'Recurse into subfolders' )
429+ @click .option ('--hashidentifier' , '-h' , required = False ,
430+ default = 'NEVER' , help = 'MD5 Hash of the identifier, values NEVER|ALWAYS|DOUBLESLASH' ) # noqa
431+ @click .option ('--source' , '-s' , required = False ,
432+ default = 'local' , help = 'The source of the record' )
421433@CLI_OPTION_YES
422- def cli_load_records (ctx , config , path , recursive , yes , verbosity ):
434+ def cli_load_records (ctx , config , path , recursive , hashidentifier , source , yes , verbosity ):
423435 """Load metadata records from directory or file into repository"""
424436
425437 with open (config , encoding = 'utf8' ) as fh :
@@ -433,6 +445,8 @@ def cli_load_records(ctx, config, path, recursive, yes, verbosity):
433445 cfg ['repository' ]['table' ],
434446 path ,
435447 recursive ,
448+ hashidentifier ,
449+ source ,
436450 yes
437451 )
438452
0 commit comments